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

在当前方案中包含主键字段的任何建议?

4

2 回答 2

0

你好,你可以试试这段代码:

DataColumn idColumn = new  DataColumn();
idColumn.DataType = System.Type.GetType("System.Int32");
idColumn.ColumnName = "id";
table.Columns.Add(idColumn);

//Just if you want define AutoIncrement 
//idColumn.AutoIncrement = true; 

DataColumn [] keys = new DataColumn [1];
keys[0] = idColumn;
table .PrimaryKey = keys;

在此行之后插入此代码:

DataTable table = dataSet.Tables["MyTable"];
于 2012-08-03T12:24:54.760 回答
0
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"); 
                } 
            } 
        ); 
}
于 2012-08-03T13:17:30.770 回答