111,126
社区成员
发帖
与我相关
我的任务
分享 static void Main()
{
string connectionString = GetConnectionString();
using (SqlConnection connection =
new SqlConnection(connectionString))
{
//Connect to the database, and then retrieve the
//schema information.
connection.Open();
string[] restrictions = new string[4];
restrictions[1] = "dbo";
restrictions[2] = "user_if_t";
DataTable table = connection.GetSchema("Tables", restrictions);
GetPrimaryKeys(table);
// Console.WriteLine(table.Constraints.GetEnumerator);
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
}
private static string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "Data Source=ISS-GHOST\\SQL2008;Initial Catalog=bond2;Integrated Security=True";
}
private static void GetPrimaryKeys(DataTable table)
{
// Create the array for the columns.
DataColumn[] columns;
columns = table.PrimaryKey;
// Get the number of elements in the array.
Console.WriteLine("PK Count: " + columns.Length);
for (int i = 0; i < columns.Length; i++)
{
Console.WriteLine(columns[i].ColumnName + columns[i].DataType);
}
}