 复制//others omitted… using SQLiteClient; using System.Linq; using System.IO.IsolatedStorage; using System.Collections.Generic; using System.Collections.ObjectModel; namespace WP7SQLiteClient.Helpers { publicclass 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(); } privatevoid Open() { if (db == null) { db = new SQLiteConnection(_dbName); db.Open(); } } privatevoid Close() { if (db != null) { db.Dispose(); db = null; } } //Insert operation publicint Insert(T obj, string statement) where T : new() { try { Open(); SQLiteCommand cmd = db.CreateCommand(statement); int rec = cmd.ExecuteNonQuery(obj); return rec; } catch (SQLiteException ex) { System.Diagnostics.Debug.WriteLine("Insert failed: " + ex.Message); throw ex; } } // Delete operation publicvoid Delete(string statement) where T : new() { try { Open(); SQLiteCommand cmd = db.CreateCommand(statement); cmd.ExecuteNonQuery(); } catch (SQLiteException ex) { System.Diagnostics.Debug.WriteLine("Deletion failed: " + ex.Message); throw ex; } } //Query operation public List SelectList(String statement) where T : new() { Open(); SQLiteCommand cmd = db.CreateCommand(statement); var lst = cmd.ExecuteQuery(); return lst.ToList(); } public ObservableCollection SelectObservableCollection(String statement) where T : new() { List lst = SelectList(statement); ObservableCollection oc = new ObservableCollection(); foreach (T item in lst) { oc.Add(item); } return oc; } privatevoid CopyFromContentToStorage(String assemblyName,String dbName) { IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); System.IO.Stream src = Application.GetResourceStream( new Uri("/" + assemblyName + ";component/" + dbName,UriKind.Relative)).Stream; IsolatedStorageFileStream dest = new IsolatedStorageFileStream(dbName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, store); src.Position = 0; CopyStream(src, dest); dest.Flush(); dest.Close(); src.Close(); dest.Dispose(); } privatestaticvoid CopyStream(System.IO.Stream input,IsolatedStorageFileStream output) { byte[] buffer = newbyte[32768]; long TempPos = input.Position; int readCount; do { readCount = input.Read(buffer, 0, buffer.Length); if (readCount > 0) { output.Write(buffer, 0, readCount); } } while (readCount > 0); input.Position = TempPos; } } } 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.97.98.99.100.101.102.103.104.105.106.107.108.109.110.111.112.113.114.115.116.117.118.119.120.121.122.123. |