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