最近研究C#想找一個攜帶型資料庫!可以把這個專案拷貝給別人的時候
照樣可以把資料庫帶過去不用安裝SQl Server 跟Mysql 實在很方便
可以參考以下網址 http://www.javaeye.com/topic/114055

經過本身的整理如下


1.下載 http://sourceforge.net/project/showfiles.php?group_id=132486&package_id=145568
2.將下載回來的檔案解壓縮後打開VS專案用參照的方式把該壓縮檔的bin目錄中的   System.Data.SQLite.DLL參考進來
3.執行下列程式如此就會在該專案執行目錄中自動建立一個叫database.db的資料庫起來



using System.Data.SQLite;  


namespace SQLITE
{
    public partial class Form1 : Form
    {
        private SQLiteConnection sqlite_conn;
        private SQLiteCommand sqlite_cmd;  


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            sqlite_conn = new SQLiteConnection("Data source=database.db");


            // Open
            sqlite_conn.Open();

            // 要下任何命令先取得該連結的執行命令物件
            sqlite_cmd = sqlite_conn.CreateCommand();

            // 要下的命令新增一個表
            sqlite_cmd.CommandText = "CREATE TABLE test (id integer primary key, text varchar(10));";

        
            sqlite_cmd.ExecuteNonQuery();

            // 插入一筆
            sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (1, '測試1');";


            sqlite_cmd.ExecuteNonQuery();

            // 查詢剛新增的表test
            sqlite_cmd.CommandText = "SELECT * FROM test";

            // 執行查詢塞入 sqlite_datareader
            SQLiteDataReader sqlite_datareader = sqlite_cmd.ExecuteReader();

            // 一筆一筆列出查詢的資料
            while (sqlite_datareader.Read())
            {
                // Print out the content of the text field:
                String s = sqlite_datareader["text"].ToString();
                MessageBox.Show(s);

            }

     //結束
            sqlite_conn.Close();

        }



SQLite真方便
還可以將Fiefox 外掛SQList管理的套件
直接作維護或檢視SQLite資料庫!真是太棒的
https://addons.mozilla.org/en-US/firefox/addon/5817

就連Firefox的內部儲存也是用SQLite 現在才知道有這種免錢又好用的DB


arrow
arrow
    全站熱搜

    ROACH 發表在 痞客邦 留言(1) 人氣()