Windows Mobile開發之SQLite的使用

來源:互聯網
上載者:User

SQLite資料庫作為一個開源的資料庫,在IOS、Android等平台上得到了很廣泛的應用,在運行效率上比起微軟的SQLCE效率高很多,在網上有大家做效能比較。
參考部落格如下:
http://www.cnblogs.com/egmkang/archive/2009/07/12/1521997.html
WM上使用SQLite及安裝:
1.首先下載SQLite安裝程式,有的是exe安裝包,有的是ZIP壓縮包,有的大大反應說,zip包裡面的幾個重要檔案即可。
將解壓的BIN目錄下的CompareFramework目錄下的SQLite.Interop.066.DLL檔案放到WM裝置或者是WM模擬器的相應的專案檔夾中;
將BIN目錄下的System.Data.SQLite.DLL檔案載入到VS2008中,即添加引用即可。如果開發的是Windows Mobile項目時,VS2008應該添加引用的System.Data.SQLite.DLL檔案來源自BIN目錄下的CompareFramework目錄。
2.這樣還需要一個SQLite管理工具,管理工具有很多,我用的是SQLiteSpy_1.9.1。
3.具體使用SQLite程式如下:
代碼如下:

//將DataSet資料儲存到SQLite資料庫中//WM項目運行時所在的目錄            string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\Test.db3";            SQLiteConnection conn = new SQLiteConnection("Data Source=" + path);            conn.Open();            SQLiteCommand cmd = conn.CreateCommand();//插入操作使用事物            SQLiteTransaction tx = conn.BeginTransaction();            cmd.Transaction = tx;            try            {                foreach (DataRow row in ds.Tables["Users"].Rows)                {                    string sql = "insert into Users (name,age) values ('" + row[0] + "','" + row[1] + "')";                    cmd.CommandText = sql;                    cmd.ExecuteNonQuery();                }                tx.Commit();                MessageBox.Show("插入資料成功!!");            }            catch (System.Data.SQLite.SQLiteException E)            {                tx.Rollback();                MessageBox.Show("插入資料失敗!!");            }finally{conn.Close();}------------------------------------------------------------------------------//快速插入資料conn.Open();string sql1 = "insert into Users (name,age) values ('Hello',23)";SQLiteCommand cmd = new SQLiteCommand(sql1, conn);cmd.ExecuteNonQuery();----------------------------------------------------------------------------------            //讀取資料 並顯示try{            string sql2 = "select * from Users";            conn.Open();            SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql2, conn);            DataTable datatable = new DataTable();            adapter.Fill(datatable);            dataGrid1.DataSource = datatable;           }   catch(Exception e){Console.WriteLine(e.ToString());   }finally{conn.Close();}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.