SqlConnection sqlConnection = new SqlConnection(
@"Data Source=SERVERNAME\SQLEXPRESS;Initial Catalog=" + databaseName + ";Integrated Security=True");
sqlConnection.Open();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("SELECT * FROM " + tableName, sqlConnection);
DataSet dataSet = new DataSet();
sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
sqlDataAdapter.Fill(dataSet);
DataTable dataTable = dataSet.Tables[0];
sqlConnection.Close();
foreach (DataColumn primaryKeyColumn in dataTable.PrimaryKey)
{
Console.WriteLine(primaryKeyColumn.ColumnName);
}
More information: System.Data.DataAdapter.MissingSchemaAction Property
Monday, December 17, 2012
[Solution] DataTable doesn't contain primary key information
If PrimaryKey collection of System.Data.DataTable object is empty, add the following line to your code (in bold):
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment