2015-11-04 43 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对象?任何最好的方式? –