【问题标题】:Writing changes to a SQLite database using SqliteDataAdapter使用 SqliteDataAdapter 将更改写入 SQLite 数据库
【发布时间】:2013-05-05 07:48:00
【问题描述】:

我从以下代码中遗漏了什么?在这个代码片段中,我正在从 SQLite 数据库中读取一个表。然后我更新一个单元格,然后读回更改。

此代码是较大代码的简化版本,但它说明了问题。

代码可以完美地读取表格,但是 AcceptChanges() 不会写回任何内容。我通过重复阅读并转到 SQLiteAdmin 并仔细阅读表来验证这一点。

我尝试添加“oLocalAdapter.Update(oLocalSet.Tables[0]);”线,但这并没有任何区别。我看到它正在搜索。

using System.Data.SQLite;

// DATABASE (Local): Formulate the SQL command.
String strSqlCommand = "SELECT * FROM [tblTest] ORDER BY [IdPrimary] ASC;";
SQLiteCommand oLocalCommand = new SQLiteCommand(strSqlCommand, ClassEngine.Connection);

// DATABASE (Local): Get the data records.
SQLiteDataAdapter oLocalAdapter = new SQLiteDataAdapter(oLocalCommand);
DataSet oLocalSet = new DataSet();
oLocalAdapter.Fill(oLocalSet, "tblTest");

// Try to write to some changes.
String strValue = oLocalSet.Tables[0].Rows[0][8].ToString();
oLocalSet.Tables[0].Rows[0][8] = 9;
oLocalSet.Tables[0].AcceptChanges();
oLocalAdapter.Update(oLocalSet.Tables[0]);

// Clean up.
oLocalSet.Dispose();
oLocalAdapter.Dispose();
oLocalCommand.Dispose();
oLocalCommand = null;

【问题讨论】:

    标签: c# sqlite visual-studio-2012 dataset dataadapter


    【解决方案1】:

    好的,知道了。

    一,我不得不重新定位/修改 AcceptChanges() 行。

    这包括可能的行

    oLocalAdapter.AcceptChangesDuringUpdate = true;
    

    然后我不得不添加

    SQLiteCommandBuilder oBuilder = new SQLiteCommandBuilder(oLocalAdapter);
    oLocalAdapter.UpdateCommand = oBuilder.GetUpdateCommand();
    

    最后一行是更新,它可以工作。这使得代码:

    // DATABASE (Local): Formulate the SQL command.
    String strSqlCommand = "SELECT * FROM tblTest ORDER BY IdPrimary ASC;";
    SQLiteCommand oLocalCommand = new SQLiteCommand(strSqlCommand, ClassEngine.Connection);
    
    // DATABASE (Local): Get the data records.
    SQLiteDataAdapter oLocalAdapter = new SQLiteDataAdapter(oLocalCommand);
    DataSet oLocalSet = new DataSet();
    oLocalAdapter.Fill(oLocalSet, "tblTest");
    
    // 
    SQLiteCommandBuilder oBuilder = new SQLiteCommandBuilder(oLocalAdapter);
    
    // Try to write to some changes.
    String strValue = oLocalSet.Tables[0].Rows[0][8].ToString();
    oLocalSet.Tables[0].Rows[0][8] = 9;
    strValue = oLocalSet.Tables[0].Rows[0][8].ToString();
    oLocalSet.AcceptChanges();
    oLocalAdapter.UpdateCommand = oBuilder.GetUpdateCommand();
    oLocalAdapter.Update(oLocalSet.Tables[0]);
    
    // Clean up.
    oLocalSet.Dispose();
    oLocalAdapter.Dispose();
    oLocalCommand.Dispose();
    oLocalCommand = null;
    

    【讨论】:

      【解决方案2】:
      public void SaveDataTable(DataTable DT)
              {
              try
                  {
                  con.Open();
                  cmd = con.CreateCommand();
                  cmd.CommandText = string.Format("SELECT * FROM {0}", DT.TableName);
                  adapter = new SQLiteDataAdapter(cmd);
                  SQLiteCommandBuilder builder = new SQLiteCommandBuilder(adapter);
                  adapter.Update(DT);
                  con.Close();
                  }
              catch (Exception Ex)
                  {
                  System.Windows.MessageBox.Show(Ex.Message);
                  }
      

      【讨论】:

      • 您可能想详细说明解决方案,提供代码的文本和原因。
      猜你喜欢
      • 2011-07-02
      • 2010-12-03
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 2020-08-03
      • 1970-01-01
      相关资源
      最近更新 更多