using System;
using System.Data;
using System.Data.SqlClient;public class DataColumnQuestion
{
public static void Main()
{
SqlConnection thisConnection=new SqlConnection("Data Source=(local);Integrated Security=SSPI;Initial Catalog=northwind");
thisConnection.Open(); SqlDataAdapter thisAdapter=new SqlDataAdapter("select CustomerID,CompanyName from Customers",thisConnection);
SqlCommandBuilder thisBuilder=new SqlCommandBuilder(thisAdapter)
DataSet thisDataSet=new DataSet(); thisAdapter.Fill(thisDataSet,"Customers");
DataColumn[] key;
key=thisDataSet.Tables["Customers"].PrimaryKey;

Console.WriteLine("Column Count: " + key.Length);
for(int i = 0; i < key.Length; i++)
{
Console.WriteLine(key[i].ColumnName + key[i].DataType);
}
}
}运行后结果为Column Count: 0,与预料的结果有出入,请帮忙分析下

解决方案 »

  1.   

    DataColumn[] key;
    key=thisDataSet.Tables["Customers"].PrimaryKey;PrimaryKey是主建,你有设主键吗??直接拿thisDataSet.Tables["Customers"].Columns.Count
      

  2.   

    DataColumn[] key;
    key=thisDataSet.Tables["Customers"].PrimaryKey;
    这里不能取到你key值集合,在这之前,必须跟据你的key值集合建立外部主索引之后才能得到。
      

  3.   

    DataColumn[] key;
    key=this.DataSet.Tables["Customers"].PrimaryKey;//这里你只拿了主键列,可能你并没有设置主键列改成key = this.DataSet.Tables["Customers"].Columns;