技术开发 频道

微软WP7本地数据库之Sqlite编程技巧

  using Community.CsharpSqlite;

  using System.Collections;

  namespace WP7SQLiteClient

  {

  public partial class TestDataEditor : PhoneApplicationPage

  {

  ObservableCollection _customerEntries = null;

  public TestDataEditor()

 
      {

  InitializeComponent();

  
//retrieve dat

  
string strSelect = "SELECT ID,Name,Email,Desc FROM Customer ORDER BY ID ASC";

  _customerEntries
= (Application.Current as App).db.SelectObservableCollection(strSelect);

  foreach (Customer data in _customerEntries)

  {

  TextBlockID.Text
+= data.ID + Environment.NewLine;

  TextBlockName.Text
+=data.Name + Environment.NewLine;

  TextBlockEmail.Text
+=data.Email + Environment.NewLine;

  TextBlockDesc.Text
+=data.Desc + Environment.NewLine;

  }

  }

  
/ /其他省略...

  在上面的代码中,我们首先定义了一个ObservableCollection 类型的变量_customerEntries。然后,在类构造器中我们建立了一个标准的SQL的SELECT命令的字符串。接下来,通过调用定义于全局App类中DBHelper类相关实例中的方法SelectObservableCollection,实现把所有客户的数据提取到变量_customerEntries中。最后,通过迭代一个结构集合,成功地实现了在屏幕上显示所有客户数据之目的。

  接下来,再看看具体的相关编码吧。

  1. 添加记录

  现在,让我们看看是如何把五个样本客户数据添加到客户表中的。

  清单4:

  private void btnAdd_Click(object sender, RoutedEventArgs e)

  {

  DateTime start
= DateTime.Now;

  
int rec;

 

0