2009年4月25日 星期六

用oledb 查 指定db內所有table

用oledb 查 指定db內所有table
///
/// 查詢db內所有table
///
/// db
/// StringCollection
protected StringCollection do_qry_tab(string Catalog)
{
StringCollection SC = new StringCollection();
OleDbConnection m_objConn = new OleDbConnection();
OleDbDataAdapter m_adper = new OleDbDataAdapter();
String CONNSTR = string.Empty;
CONNSTR = "Data Source=" + txt_server.Text + ";Initial Catalog=" + Catalog + ";Provider=SQLOLEDB;Persist Security Info=True;User ID=" + txt_id.Text + ";Password=" + txt_pw.Text + "";
m_objConn.ConnectionString=CONNSTR;
#region TRY
try
{
m_objConn.Open();
DataTable dt = new DataTable();
dt = m_objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] { null, null, null, "TABLE" }); // 若是查view 用"VIEW"
foreach (DataRow dr in dt.Rows)
{
SC.Add(dr["TABLE_NAME"].ToString());
}

}
catch (Exception)
{
throw;
}
finally
{
m_objConn.Close();
}
#endregion
return SC;
}

沒有留言:

張貼留言