【问题标题】:Inserting a record into SQL Server Compact database using TableAdapter使用 TableAdapter 将记录插入 SQL Server Compact 数据库
【发布时间】:2014-02-13 18:01:38
【问题描述】:

我是 C# 和 SQL Server 的新手,所以如果我遗漏了一些看起来很明显的东西,请多多包涵。

我正在尝试编写一个程序,该程序从 LAN 接收数据并将其存储到本地数据库中。还将有一个用于显示一些数据的 GUI。我正在使用 VS2010 和 SQL Server Compact v3.5。我已经能够创建一个连接到数据库并显示/更新表的 Windows 窗体应用程序,因此我相信数据库已正确连接。

我有一个从 LAN 接收数据的后台工作人员,现在我希望后台工作人员更新数据库中的一个表。我用过这个指南:

http://msdn.microsoft.com/en-us/library/ms233812.aspx

创建以下代码段:

           MyDataSet.MeasurementRow newMeasurementRow;
           newMeasurementRow = dataset1.Measurement.NewMeasurementRow();
           newMeasurementRow.ChannelNo = n;
           newMeasurementRow.DeviceID = DeviceID;
           newMeasurementRow.Value = f;
           newMeasurementRow.TimeStamp = DateTime.Now;

          try  
          {                                                                 
               this.dataset1.Measurement.Rows.Add(newMeasurementRow);
               this.measurementTableAdapter.Update(this.dataset1.Measurement);
               Console.WriteLine("Success ");
          }
          catch (Exception e)
          {
                Console.WriteLine("Error..... " + e.StackTrace);
                throw;            
          } 

dataset1measurementTableAdapter 在前面的代码中创建:

    MyDataSet dataset1 = new MyDataSet();
    MyDataSetTableAdapters.MeasurementTableAdapter measurementTableAdapter =
    new MyDataSetTableAdapters.MeasurementTableAdapter(); 

MyDataSet 和表适配器类是由添加新数据源向导创建的。 Measurement 是我要添加到的数据库中表的名称。

当我运行它时,我在调用 Update 方法时得到以下输出:

System.Data.SqlServerCe.dll 中出现“System.Data.SqlServerCe.SqlCeException”类型的第一次机会异常
System.Data.dll 中出现“System.Data.SqlServerCe.SqlCeException”类型的第一次机会异常

错误.....在 System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
在 System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
在 System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
在 System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
在 System.Data.Common.DbDataAdapter.Update(DataTable dataTable)

如果有人可以帮助我解释这些错误或指出可能导致问题的正确方向,我们将不胜感激。

【问题讨论】:

  • 你有Error.....的那部分,.....是最重要的部分,它说了什么?
  • @MichaelPerrenoud 这就是我在 System.Data.Common .. 等消息中获得这些消息的地方。抱歉,我以某种方式错过了原始问题中的错误.....部分。我不知道如何解释这些消息。

标签: c# sql-server database backgroundworker tableadapter


【解决方案1】:

最好尝试将 newMeasurementRow (s) 添加到作为 DataSet 一部分的 DataTable(创建一个,然后添加到数据集)。看起来数据适配器无法将行简单地解析为数据集的一部分,而是在寻找数据表。

【讨论】:

  • 我已经按照您的建议进行了尝试,现在得到以下信息: System.Data.dll 中出现“System.ArgumentException”类型的第一次机会异常 '错误.....在 System.Data。 System.Data.DataRowCollection.Add(DataRow row)'处的 DataTable.InsertRow(DataRow row, Int64 proposalID, Int32 pos, Boolean fireEvent)'
【解决方案2】:

所以我仍然不知道是什么导致了我的原始代码出现问题,但是我尝试使用 TableAdapter Insert 方法而不是更新,这似乎可以满足我的要求:

         private MyDataSetTableAdapters.MeasurementTableAdapter measurementTableAdapter =
         new MyDataSetTableAdapters.MeasurementTableAdapter();

         measurementTableAdapter.Insert((n+1), DeviceID, DateTime.Now, Values[n]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多