我正在尝试按照ObjectType获取数据库表,我该如何实现? 到目前为止我尝试了这种方法
public void GetTableByObjectType(string ObjectType) {
SqlConnection conn = new SqlConnection(
"Data Source=Local;Initial Catalog=myConnection;User ID=sa;Password=12345");
DataTable t = conn.GetSchema("Objectype");
答案 0 :(得分:0)
此代码将帮助您从数据库中获取您尝试访问的所有表
string SQLExpression = "SELECT * FROM SYS.Tables Order By Name";
SqlConnection conn = new SqlConnection(
"Data Source=Local;Initial Catalog=myConnection;User ID=sa;Password=12345");
SqlDataAdapter adp = new SqlDataAdapter(SQLExpression, cn);
DataSet ds = new DataSet();
adp.Fill(ds, "SystemData");
答案 1 :(得分:0)
在此示例中,您将获得数据库表格的模式:
String ObjectType = "Tables"; //collectionName argument to get Tables
String[] tableRestrictions = new String[4];
tableRestrictions[2] = "TableYouWant"; //name of the table whose get the schema
DataTable courseTableSchemaTable = conn.GetSchema(ObjectType, tableRestrictions);
您可以对表集合使用四个限制,因此您应该创建一个4成员数组。
对于数组tableRestrictions
:
另见:https://msdn.microsoft.com/it-it/library/ms136366(v=vs.110).aspx