欢迎投稿

今日深度:

微软WP7本机数据库解决方案之SQLite(1)(3)

列表1:更新版本的工具类DBHelper

  1. //others omitted…  
  2. using SQLiteClient;  
  3. using System.Linq;  
  4. using System.IO.IsolatedStorage;  
  5. using System.Collections.Generic;  
  6. using System.Collections.ObjectModel;  
  7. namespace WP7SQLiteClient.Helpers  
  8. {  
  9.    public class DBHelper  
  10.    {  
  11.       private String _dbName;  
  12.       private SQLiteConnection db = null;  
  13.       public DBHelper(String assemblyName, String dbName)  
  14.       {  
  15.          IsolatedStorageFile store =IsolatedStorageFile.GetUserStoreForApplication();  
  16.          if (!store.FileExists(dbName))  
  17.          {  
  18.             CopyFromContentToStorage(assemblyName, dbName);  
  19.          }  
  20.          _dbName = dbName;  
  21.       }  
  22.       ~DBHelper()  
  23.       {  
  24.          Close();  
  25.       }  
  26.       private void Open()  
  27.       {  
  28.          if (db == null)  
  29.          {  
  30.             db = new SQLiteConnection(_dbName);  
  31.             db.Open();  
  32.          }  
  33.       }  
  34.       private void Close()  
  35.       {  
  36.          if (db != null)  
  37.          {  
  38.             db.Dispose();  
  39.             db = null;  
  40.          }  
  41.       }  
  42.       //Insert operation  
  43.       public int Insert(T obj, string statement) where T : new()  
  44.       {  
  45.          try 
  46.          {  
  47.             Open();  
  48.             SQLiteCommand cmd = db.CreateCommand(statement);  
  49.             int rec = cmd.ExecuteNonQuery(obj);  
  50.             return rec;  
  51.          }  
  52.          catch (SQLiteException ex)  
  53.          {  
  54.             System.Diagnostics.Debug.WriteLine("Insert failed: " + ex.Message);  
  55.             throw ex;  
  56.          }  
  57.       }  
  58.       // Delete operation  
  59.       public void Delete(string statement) where T : new()  
  60.       {  
  61.          try 
  62.          {  
  63.             Open();  
  64.             SQLiteCommand cmd = db.CreateCommand(statement);  
  65.             cmd.ExecuteNonQuery();  
  66.          }  
  67.          catch (SQLiteException ex)  
  68.          {  
  69.             System.Diagnostics.Debug.WriteLine("Deletion failed: " + ex.Message);  
  70.             throw ex;  
  71.          }  
  72.       }  
  73.       //Query operation  
  74.       public List SelectList(String statement) where T : new()  
  75.       {  
  76.          Open();  
  77.          SQLiteCommand cmd = db.CreateCommand(statement);  
  78.          var lst = cmd.ExecuteQuery();  
  79.          return lst.ToList();  
  80.       }  
  81.       public ObservableCollection SelectObservableCollection(String statement)  
  82.       where T : new()  
  83.       {  
  84.          List lst = SelectList(statement);  
  85.          ObservableCollection oc = new ObservableCollection();  
  86.          foreach (T item in lst)  
  87.          {  
  88.             oc.Add(item);  
  89.          }  
  90.          return oc;  
  91.       }  
  92.       private void CopyFromContentToStorage(String assemblyName,String dbName)  
  93.       {  
  94.          IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();  
  95.          System.IO.Stream src = Application.GetResourceStream(  
  96.                                 new Uri("/" + assemblyName + ";component/" + dbName,UriKind.Relative)).Stream;  
  97.          IsolatedStorageFileStream dest = new IsolatedStorageFileStream(dbName,  
  98.                                           System.IO.FileMode.OpenOrCreate,  
  99.                                           System.IO.FileAccess.Write, store);  
  100.          src.Position = 0;  
  101.          CopyStream(src, dest);  
  102.          dest.Flush();  
  103.          dest.Close();  
  104.          src.Close();  
  105.          dest.Dispose();  
  106.       }  
  107.       private static void CopyStream(System.IO.Stream input,IsolatedStorageFileStream output)  
  108.       {  
  109.          byte[] buffer = new byte[32768];  
  110.          long TempPos = input.Position;  
  111.          int readCount;  
  112.          do 
  113.          {  
  114.             readCount = input.Read(buffer, 0, buffer.Length);  
  115.             if (readCount > 0)  
  116.             {  
  117.                output.Write(buffer, 0, readCount);  
  118.             }  
  119.          } while (readCount > 0);  
  120.          input.Position = TempPos;  
  121.       }  
  122.    }  

顺便说一句,对于上面这个帮助类我也没有提供细致的优化编码。希望读者根据您的相关工作能够继续进行这项工作(例如提供更好的泛型化的CRUD支持)并分享给广大网友。简言之,我主要是增加了插入和删除方法。上面的代码中最引起您注意是地方一定是方法CopyFromContentToStorage,正是借助这个方法我们实现了上述目标-建立起SQLite数据库与独立存储的关系。

三、小结

本文中简要介绍了Sqlite Client for Windows Phone的主要功能及相关的辅助开发工具。在接下来的第二篇文章中,我们将具体构建一个简单的Windows Phone 7客户端应用程序,当然要涉及到Sqlite Client for Windows Phone的基本编程技巧。


www.htsjk.Com true http://www.htsjk.com/shujukugl/17790.html NewsArticle 列表1:更新版本的工具类DBHelper //othersomitted using SQLiteClient; using System.Linq; using System.IO.IsolatedStorage; using System.Collections.Generic; using System.Collections.ObjectMode...
评论暂时关闭