2012-08-03 110 views
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"); 
       } 
      } 
     ); 

任何建議在當前方案中包含主鍵字段?

回答

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"]; 
+0

嗨讓我'竭誠爲您服務 – 2012-08-03 13:38:51

+0

嗨Aghilas謝謝爲你答覆。當我運行代碼時,我收到以下錯誤消息:System.ArgumentException {「列必須屬於一個表。」} – 2012-08-03 13:49:59

+0

只需將列添加到您的表中 – 2012-08-03 13:51:53

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