2
我很难更改此类以包含具有自动增量的主键字段。这个模式我从一个工业设备通过第三方库获取数据,这个方案是由自己的公司提供的,这个库提供了这些库,我们应该为这个数据层添加一个主键字段,将需要把另一层数据访问应用程序通过链接的报告检索这些数据。在主类中插入主键
const string connectionString =
"Data Source=(local);Initial Catalog=MyDB;Integrated Security=true";
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
// Create all necessary ADO.NET objects.
var adapter = new SqlDataAdapter("SELECT * FROM MyTable", connection);
var dataSet = new DataSet();
adapter.FillSchema(dataSet, SchemaType.Source, "MyTable");
adapter.InsertCommand = new SqlCommandBuilder(adapter).GetInsertCommand();
DataTable table = dataSet.Tables["MyTable"];
int[] handles = EasyDAClient.DefaultInstance.SubscribeMultipleItems(
new[]
{
new DAItemGroupArguments("", "MyOPCServer.1", "OPCItem01", 1000, null),
new DAItemGroupArguments("", "MyOPCServer.1", "OPCItem02", 1000, null),
new DAItemGroupArguments("", "MyOPCServer.1", "OPCItem03", 1000, null),
new DAItemGroupArguments("", "MyOPCServer.1", "OPCItem04", 1000, null)
},
(_, eventArgs) =>
{
if (eventArgs.Vtq != null)
{
// Fill a DataRow with the OPC data, and add it to a DataTable.
table.Rows.Clear();
DataRow row = table.NewRow();
row["ItemID"] = eventArgs.ItemDescriptor.ItemId;
row["Value"] = eventArgs.Vtq.Value; // The DataRow will make the conversion to a string.
row["Timestamp"] = (eventArgs.Vtq.Timestamp < (DateTime) SqlDateTime.MinValue)
? (DateTime)SqlDateTime.MinValue
: eventArgs.Vtq.Timestamp;
row["Quality"] = (short)eventArgs.Vtq.Quality;
table.Rows.Add(row);
// Update the underlying DataSet using an insert command.
adapter.Update(dataSet, "MyTable");
}
}
);
任何建议在当前方案中包含主键字段?
嗨让我'竭诚为您服务 – 2012-08-03 13:38:51
嗨Aghilas谢谢为你答复。当我运行代码时,我收到以下错误消息:System.ArgumentException {“列必须属于一个表。”} – 2012-08-03 13:49:59
只需将列添加到您的表中 – 2012-08-03 13:51:53