2015-11-04 44 views
8

我有行的ID然後我會更新其他值。如何在Windows 10 C#中更新sqlite.net pcl中的行?

我不知道如何更新我的價值! 我的表:

class MyTable 
{ 
    [PrimaryKey, AutoIncrement] 
    public int Id { get; set; } 
    public string Date { get; set; } 
    public string Volumes { get; set; } 
    public string Price { get; set; } 
} 

其他信息:

string path; 
    SQLite.Net.SQLiteConnection conn; 

    public updatepage() 
    { 
     this.InitializeComponent(); 
     path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "ccdb.sqlite"); 

     conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path); 

     conn.CreateTable<MyTable>(); 
    } 
+1

請參考這篇文章http://blog.tpcware.com/2014/05/universal-app-with-sqlite-part-2/雖然它說通用應用程序,但我認爲它也適用於Windows 10應用程序。 – Janak

回答

7

我最近開始UWP應用工作,也遇到了這個問題。那麼,如何在UWP中使用SQLite更新一行?在這裏你去!

using (var dbConn = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), App.DB_PATH)) 
    { 
     var existingUser = dbConn.Query<User>("select * from User where Id = ?", user.Id).FirstOrDefault(); 
     if (existingUser != null) 
     { 
      existingUser.Name = user.Name; 
      existingUser.Email = user.Email; 
      existingUser.Username = user.Username; 
      existingUser.Surname = user.Surname; 
      existingUser.EmployeeNumber = user.EmployeeNumber; 
      existingUser.Password = user.Password; 
      dbConn.RunInTransaction(() => 
      { 
       dbConn.Update(existingUser); 
      }); 
     } 
    } 

App.DB_PATH與您的'路徑'變量相同。我意識到這個答案有很多不同的方面,所以如果你有任何問題,請隨時提問。

2

只更新一排一組特定的值,然後執行原始SQL查詢會更簡單:

conn.Execute("UPDATE MyTable SET Price = ? Where Id = ?", 1000000, 2); 

這是假定將傳遞到執行語句的輸入已被清理。

+0

然後立即我需要更新這個viewmodel對象?任何最好的方式? –