楼上的兄弟,能说一下怎样去显式设置主键吗?在sql里面写吗?怎么写呢?谢谢你的回复。

解决方案 »

  1.   

    DataTable.PrimaryKey 属性,获取或者设置主键
    private void SetPrimaryKeys(){
       // Create a new DataTable and set two DataColumn objects as primary keys.
       DataTable myTable = new DataTable();
       DataColumn[] keys = new DataColumn[2];
       DataColumn myColumn;
       // Create column 1.
       myColumn = new DataColumn();
       myColumn.DataType = System.Type.GetType("System.String");
       myColumn.ColumnName= "FirstName";
       // Add the column to the DataTable.Columns collection.
       myTable.Columns.Add(myColumn);
       // Add the column to the array.
       keys[0] = myColumn;   // Create column 2 and add it to the array.
       myColumn = new DataColumn();
       myColumn.DataType = System.Type.GetType("System.String");
       myColumn.ColumnName = "LastName";
       myTable.Columns.Add(myColumn);
       // Add the column to the array.
       keys[1] = myColumn;
       // Set the PrimaryKeys property to the array.
       myTable.PrimaryKey = keys;
    }
      

  2.   

    感谢楼上兄弟的回复,我会试试你的方法的。
    但我现在疑惑的是:如果是单表查询,那么在fill前设置myAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;就没问题;但改为联合查询,这样就不行了,说找不到主键。是不是联合查询是只能得到记录集,而不能得到约束信息等?请大家继续关注一下,谢谢了。
      

  3.   

    brightheroes(闭关|保守的教练,功利的足球)提供的方法我试了,可以达到要求,谢谢兄弟。可我还是觉得别扭,为什么要这样搞呢?有更简单的办法吗?或者说问题到底在那里呢?