分享一個基於.Net Compact Framework的Sqlite資料訪問組件(一)

來源:互聯網
上載者:User

 :http://download.csdn.net/source/3508105

最近做了寫移動領域的開發,需要用到Sqlite資料庫,在網上找了半天也沒找到一個合適的資料訪問組件,沒辦法,只好在已有的Linq4AnyDB資料訪問組件基礎上做一個新的,經過2周的努力,終有所成,現貢獻出來,供大家批判。

這個組件的使用非常簡單方便,支援最基本的增刪改查,支援條件刪除、條件修改以及Linq。為了吸引眼球,首先展示一下在項目中使用的便捷性,然後再詳細介紹這個組件的使用方法。

一、Com.Yuan.Mobile.SqliteDAC的開發中的便捷性:

在以下的示範中,我們使用大名鼎鼎的NorthWind資料庫作為我們的示範資料庫。

  1. 添加一條記錄:

      例如我們需要在NorthWind的Products表中插入一條新的記錄,怎麼做呢?很見但,見下面的代碼

      //插入單條記錄            using (var db = new NorthWind())            {                db.Products.InsertEntity(new Products {                     ProductName = "iPhone",                     UnitPrice = 100.0m,                     Discontinued = true });            }

       

  2. 添加多條記錄:

      如果我們要在NorthWind的Products表中插入多條新記錄,怎麼做呢?也同樣非常簡單。

                  using (var db = new NorthWind())            {                for (int i = 0; i < 10; i++)                {                    db.Products.InsertEntity(new Products {                         ProductName = string.Format("多條{0}", i),                         Discontinued = true });                }            }

      也可以這麼寫:

                  using (var db = new NorthWind())            {                var newRecords = new Products[10];                for (int i = 0; i < 10; i++)                    newRecords[i] = new Products {                         ProductName = string.Format("多條{0}", i),                         Discontinued = true };                db.Products.InsertEntity(newRecords);            }

      有的同學可能要問了,你插入多條怎麼沒用到事務?實際上這裡面有一個隱性的事務,在using塊結束時,會調用db.Dispose(),此時,若無錯誤,會將事務提交,有錯誤則會復原。

      當然也可以顯式的用到事務,如下所示:

                  var northWind = new NorthWind(false);            northWind.BeginTransaction();            try            {                for (int i = 0; i < 10; i++)                {                    northWind.Products.InsertEntity(new Products                    {                        ProductName = string.Format("多條{0}", i),                        Discontinued = true                    });                }                northWind.Commit();            }            catch            {                northWind.RollBack();            }

       

  3. 刪除一條記錄:

      如果需要刪除NorthWind庫Products表中ProductID= 11的記錄,則這麼寫

                  using (var db = new NorthWind())            {                var count = db.Products.DeleteEntity(new Products { ProductID = 11 });                MessageBox.Show(string.Format("共刪除{0}條記錄", count));            }

       

  4. 刪除多條記錄:

      如果想要刪除NorthWind庫中Pruducts表中ProductID=12和13的記錄,這麼寫:

                  using (var db = new NorthWind())            {                var delArray = new Products[2];                delArray[0] = new Products { ProductID = 12 };                delArray[1] = new Products { ProductID = 13 };                var count = db.Products.DeleteEntity(delArray);                MessageBox.Show(string.Format("共刪除{0}條記錄", count));            }

      如果想要刪除NorthWind庫中Products表中ProductID>= 13且ProductID<20的記錄,怎麼寫呢?

                  using (var db = new NorthWind())            {                var count = db.Products.ConditionalDeleteEntity(                    new Products { ProductID = 12 },                     new Products { ProductID = 20 });                MessageBox.Show(                    string.Format("刪除productid >= 12 且\r\n productid <20 的記錄\r\n共刪除{0}條記錄", count));            }

      再複雜一些,我現在需要刪除NorthWind庫中Products表中ProductName首字為“a”的資料怎麼寫呢?這個就稍微麻煩點,需要用到Linq了:

                  using (var db = new NorthWind())            {                var count = db.Products.DeleteEntity(                    from p in db.Products where p.ProductName.StartsWith("a") select p);                MessageBox.Show(string.Format("刪除productname 首字母為“a” 的全部記錄,\r\n共{0}條記錄", count));            }
  5. 修改單條記錄:

      將NorthWind庫中Products表中Productid = 11 的記錄的ProductName改為“修改單條測試”

                  using (var db = new NorthWind())            {                var lst = from p in db.Products where p.ProductID == 11 select p;                if (lst.Count() > 0)                {                    var product = lst.First();                    product.ProductName = "單條修改測試";                    var count = db.Products.UpdateEntity(product);                    MessageBox.Show("修改Productid=11的資料成功");                }                else                    MessageBox.Show("不存在Productid=11的資料");            }
  6. 修改多條記錄:

      將NorthWind庫中Products表中ProductID>11且ProductID<20的記錄查詢出來並修改ProductName

                  using (var db = new NorthWind())            {                var lst = (from p in db.Products where p.ProductID > 11 && p.ProductID < 20                            select p).ToList();                for (int i = 0; i < lst.Count; i++)                    lst[i].ProductName = string.Format("批量修改{0}", i);                var count = db.Products.UpdateEntity(lst);                MessageBox.Show(string.Format("共修改{0}條記錄", count));            }

    批量條件修改,例如,若要修改滿足某個條件的所有記錄的某個欄位,可以用條件修改方法,如下所示:

                using (var db = new NorthWind())            {                var count = db.Products.ConditionalUpdateEntity(                    new Products { UnitPrice = 10.0M },                     new Products { ProductID = 20 },                     new Products { ProductID = 30 });                if (count > 0)                {                    MessageBox.Show(                        string.Format("修改productid>=20且productid<30的資料\r\n共修改{0}條記錄",                         count));                }            }

    上面的代碼錶示將productid>=20且productid<30的所有記錄的UnitPrice改為10.0

好了,以上的樣本已經能夠證明這個Sqlite的資料訪問組件的易用性和靈活行了,我會在下一篇博文中詳細講解其具體的使用方法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.