_ConnectionPtr m_pConnection
_RecordsetPtr m_pRecordset
CStringList* m_pStrList
m_strConnect = "Provider=SQLOLEDB.1Server=127.0.0.1Database=student/*student为数据库名*/uid=sapwd=123456/*123456为数据库密码*/"
m_pConnection.CreateInstance(__uuidof( Connection ))
m_pConnection->Open((LPCSTR)m_strConnect,"","",adModeUnknown)
m_pRecordset.CreateInstance(__uuidof(Recordset))
CString strSelect("select Name from sysobjects where xtype='u' and status>=0")
m_pRecordset->CursorType = adOpenStatic
m_pRecordset->CursorLocation = adUseClient
m_pRecordset->CacheSize = 1000L
m_pRecordset->Open((LPCTSTR)strSelect, m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText)
LONG lCount = m_pRecordset->GetRecordCount()
lCount就是用户表的个数了,你也可以用记录集指针m_pRecordset遍历取得表名
//C#获取Access数据库中的所有表名和列名string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0" + @"Data Source=E:\report20100819\report20100819.mdb"
OleDbConnection Conn = new OleDbConnection(ConnectionString)
Conn.Open()
DataTable shemaTable = Conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" })
foreach (DataRow dr in shemaTable.Rows)
{
Console.WriteLine(dr["TABLE_NAME"])
DataTable columnTable = Conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, dr["TABLE_NAME"].ToString(), null })
foreach (DataRow dr2 in columnTable.Rows)
{
Console.WriteLine(dr2["COLUMN_NAME"])
}
}
Conn.Close()
解释:
“Conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" })”返回数据库中所有的数据表清单,返回值为DataTable。
“Conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, dr["TABLE_NAME"].ToString(), null })”返回指定数据表中所有的数据列清单,返回值为DataTable。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)