[英]Inserting a record into SQL Server Compact database using TableAdapter
I'm new to C# and SQL Server so please bear with me if I miss something that seems obvious. 我是C#和SQL Server的新手,所以如果我错过一些显而易见的事情,请多多包涵。
I'm trying to write a program which receives data data from a LAN and stores it onto a local database. 我正在尝试编写一个程序,该程序从LAN接收数据数据并将其存储到本地数据库中。 There will also be a GUI for displaying some of the data.
还有一个用于显示某些数据的GUI。 I am using VS2010 and SQL Server Compact v3.5.
我正在使用VS2010和SQL Server Compact v3.5。 I have been able to create a windows forms application which connects to the database and displays/updates tables and so I believe the database is connected properly.
我已经能够创建一个Windows窗体应用程序,该应用程序连接到数据库并显示/更新表,因此我相信数据库已正确连接。
I have a background worker which receives data from the LAN and now I want the background worker to update one of the tables in the database. 我有一个后台工作程序,它从LAN接收数据,现在我希望后台工作程序更新数据库中的表之一。 I have used this guide:
我使用了本指南:
http://msdn.microsoft.com/en-us/library/ms233812.aspx http://msdn.microsoft.com/en-us/library/ms233812.aspx
to create the following section of code: 创建以下代码部分:
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;
}
dataset1
and measurementTableAdapter
are created earlier in the code with: dataset1
和measurementTableAdapter
在代码的前面创建,其代码如下:
MyDataSet dataset1 = new MyDataSet();
MyDataSetTableAdapters.MeasurementTableAdapter measurementTableAdapter =
new MyDataSetTableAdapters.MeasurementTableAdapter();
MyDataSet
and the table adapter classes were created by the Add New Data Source Wizard. MyDataSet
和表适配器类是由“添加新数据源向导”创建的。 Measurement
is the name of the table in my database I want to add to. Measurement
是我要添加到数据库中的表的名称。
When I run this however I get the following outputs when the Update method is called: 但是,在运行此方法时,调用Update方法时会得到以下输出:
A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll
类型为“ System.Data.SqlServerCe.SqlCeException”的第一次机会异常发生在System.Data.SqlServerCe.dll中
A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.dll类型“ System.Data.SqlServerCe.SqlCeException”的第一次机会异常发生在System.Data.dll中
Error..... at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
错误.....,位于System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent,BatchCommandInfo [] batchCommands,Int32 commandCount)
at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)在System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent,BatchCommandInfo [] batchCommands,Int32 commandCount)中
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)在System.Data.Common.DbDataAdapter.Update(DataRow [] dataRows,DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)在System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable,DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)在System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
If anyone could help me interpret these errors or point me in the right direction as to what might be causing the problem It would be greatly appreciated. 如果有人可以帮助我解释这些错误或就可能导致该问题的原因向我指出正确的方向,将不胜感激。
Probably best to try adding your newMeasurementRow (s) to a DataTable (create one, then add to the dataset) that is part of the DataSet. 最好尝试将newMeasurementRow添加到属于DataSet的DataTable中(先创建一个,然后添加到数据集)。 It looks like the dataadapter can't resolve the rows as simply part of the dataset, it is looking for a datatable instead.
看起来dataadapter无法将行仅作为数据集的一部分来解析,而是在寻找数据表。
So I still don't know what was causing the problem with my original code, however I have tried using the TableAdapter Insert method instead of update and this seems to do what I want no problem: 所以我仍然不知道是什么导致了我的原始代码的问题,但是我尝试使用TableAdapter Insert方法而不是update,这似乎可以解决我所希望的一切:
private MyDataSetTableAdapters.MeasurementTableAdapter measurementTableAdapter =
new MyDataSetTableAdapters.MeasurementTableAdapter();
measurementTableAdapter.Insert((n+1), DeviceID, DateTime.Now, Values[n]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.