在主类中插入主键

时间:2012-08-03 12:14:15

标签: c# ado.net dataset

我无法更改此类以包含具有自动增量的主键字段。这个架构我通过第三方库从工业设备获取数据,这个方案是由提供这些库的自有公司提供的,我们应该为这个数据层添加一个主键字段,需要另外一层数据访问其他应用程序通过链接的报告检索此数据。

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

是否有建议在当前方案中包含主键字段?

2 个答案:

答案 0 :(得分: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"];

答案 1 :(得分: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"); 
                } 
            } 
        ); 
}