gpt4 book ai didi

c# - 在类中插入主键

转载 作者:太空宇宙 更新时间:2023-11-03 16:28:12 26 4
gpt4 key购买 nike

我很难更改此类以包含具有自动递增功能的主键字段。这个schema我是通过第三方库从一个工业设备上获取数据的,这个scheme是由提供这些库的自己的公司提供的,我们应该为这个数据层添加一个主键字段将需要把另一层数据访问other应用程序通过链接的报告检索此数据。

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");
}
}
);

在当前方案中包含主键字段有什么建议吗?

最佳答案

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"];
DataColumn[0] key1 = dataSet.Tables["MyTable"].Columns["ItemID"];
key1[0].AutoIncrement = true;
key1[0].AutoIncrementSeed = 1;
key1[0].AutoIncrementStep = 1;
dataSet.Tables["MyTable"].PrimaryKey = key1;

key1 = null;

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");
}
}
);
}

关于c# - 在类中插入主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11795542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com