2010年1月20日 星期三

[SQL]利用ADO找出現有的table

網路上找到且我試用過的方法有兩個

sqlconn = new System.Data.SqlClient.SqlConnection();
sqlconn.ConnectionString = connectstring;
sqlconn.Open();

// method 1
DataTable dt = sqlconn.GetSchema();
DataTable dt2 = sqlconn.GetSchema("Tables");
Literal1.Text= printTable(dt2);

// skill for write xml to string
//System.IO.StringWriter a=new System.IO.StringWriter();
//dt2.WriteXmlSchema(a);
//Response.Write(a.ToString());

// method 2
DataTable dt3 = new DataTable("Tables");
string commandstring = "select TABLE_NAME from INFORMATION_SCHEMA.Tables where TABLE_TYPE ='BASE TABLE'";
System.Data.SqlClient.SqlCommand sqlcmd = sqlconn.CreateCommand();
sqlcmd.CommandText = commandstring;
dt3.Load(sqlcmd.ExecuteReader());
Literal2.Text= printTable(dt3);

sqlconn.Close();

沒有留言:

張貼留言