2013-04-08 99 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. 
    } 
}