2013-04-08 101 views
0

在这里,在Windows 8中SQLLite数据库的我的插入代码,我想更新其在数据库中添加更新数据库记录在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"); 
    } 
} 

回答

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. 
    } 
}