技术开发 频道

微软WP7本机数据库解决方案之Sqlite

  2.一个有用的工具类-DBHelper

  如上所述,Sqlite Client for Windows Phone使用众所周知的SQL操作针对典型的数据库操作提供了一个高层次的封装。因此,在Silverlight for Windows Phone 7编程中为了处理SQLite数据库操作,我们可以直接使用在文件SQLiteClient.cs中定义的对象(在源库项目),即SQLiteException、SQLiteConnection和SQLiteCommand等。

  虽然Sqlite Client for Windows Phone并没有提供与独立存储的直接互动,但显然增加对独立存储支持是必要的,这样可以改善系统的性能。因此,我们可以进一步封装前面提到的SQLiteClient对象。为此,Chris开发了一个非常好用的实用工具类,叫做DBHelper。为了应用于我们自己的示例,我对它做了轻微的修改。完整的源码如下。

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

  //others omitted…

  using SQLiteClient;

  using System.Linq;

  using System.IO.IsolatedStorage;

  using System.Collections.Generic;

  using System.Collections.ObjectModel;

  namespace WP7SQLiteClient.Helpers

  {

  
public class DBHelper

  {

  
private String _dbName;

  
private SQLiteConnection db = null;

  
public DBHelper(String assemblyName, String dbName)

  {

  IsolatedStorageFile store
=IsolatedStorageFile.GetUserStoreForApplication();

  
if (!store.FileExists(dbName))

  {

  CopyFromContentToStorage(assemblyName, dbName);

  }

  _dbName
= dbName;

  }

  ~DBHelper()

  {

  Close();

  }

  
private void Open()

  {
0
相关文章