0

ここでは、Windows 8 の SQLLite データベースの挿入コードで、データベースに追加されたレコードを更新したいと考えています。

private async void insert(object sender, RoutedEventArgs e) {
  if (txt1.Text != "" && txt2.Text != "" && txt3.Text != "") {
    var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "data.db3");
    using (var db = new SQLite.SQLiteConnection(dbpath)) {
      // Create the tables if they don't exist
      db.Insert(new person() {
        id= Guid.NewGuid(),
        name = txt1.Text.ToString(),
        address = txt2.Text.ToString(),
        phone = Convert.ToDouble(txt3.Text.ToString()),
      });

      db.Commit();
      db.Dispose();
      db.Close();
    }
  } else {
    throw new NullReferenceException("Enter The Data In Textboxes");
  }
}
4

1 に答える 1

0

SQLite にGet<T>は、主キーを引数として受け取り、行をオブジェクトとして返すメソッドがあります。Updateメソッドはオブジェクトをパラメーターとして受け入れ、既存のレコードを更新します。ここでは、個人レコードを更新する方法を紹介します。

private void UpdateRecord(int primaryKey)
{
    var dbpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "data.db3");
    using (var db = new SQLite.SQLiteConnection(dbpath))
    {
        var objPerson = db.Get<person>(primaryKey);
        objPerson.name = "New name";
        objPerson.address = "New address ";
        objPerson.phone = Convert.ToDouble("New phone number");
        db.Update(objPerson);
        //You don't need to use db.Commit(), db.Dispose() & db.Close() because you are using "using" keyword.
    }
}
于 2013-04-08T08:46:09.413 に答える