Table Names Of a MS Access Database
-
I Want to List the Table Names Of a MS Access Database Using ADO Connection. This is How I tried ADODB.Connection myConnection = new ADODB.ConnectionClass(); ADODB.Recordset rsTblNames = new ADODB.RecordsetClass(); if (optAccess.Checked) { ConnStr= "Provider=Microsoft.Jet.OLEDB.4.0;User ID=;Data Source="+ txtDbPath.Text +";Mode=ReadWrite;Extended Properties='';Jet OLEDB:System database='';Jet OLEDB:Registry Path='';Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False"; myConnection.Open(ConnStr,"","",0); StrSql= "SELECT Name FROM MSysObjects WHERE Type = 1"; rsTblNames.Open(StrSql, myConnection, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockReadOnly,0); } But This returns a Error
-
I Want to List the Table Names Of a MS Access Database Using ADO Connection. This is How I tried ADODB.Connection myConnection = new ADODB.ConnectionClass(); ADODB.Recordset rsTblNames = new ADODB.RecordsetClass(); if (optAccess.Checked) { ConnStr= "Provider=Microsoft.Jet.OLEDB.4.0;User ID=;Data Source="+ txtDbPath.Text +";Mode=ReadWrite;Extended Properties='';Jet OLEDB:System database='';Jet OLEDB:Registry Path='';Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False"; myConnection.Open(ConnStr,"","",0); StrSql= "SELECT Name FROM MSysObjects WHERE Type = 1"; rsTblNames.Open(StrSql, myConnection, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockReadOnly,0); } But This returns a Error
Why don't you tell us what that error reads? :doh: Besides, why are you using ADO.NET? It's much faster and for what you need can accomplish the same things. Your connection string doesn't change (although you could drop most of those options since they are mostly defaults and other unnecessary properties), nor does your SQL statement, just the code itself. This is, after all, the C# forum - a language targeting the CLR - so writing .NET applications and libraries is what this is about:
OleDbConnection conn = new OleDbConnection(string.Format(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Mode=ReadWrite",
txtDbPath.Text));
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT Name FROM MSysObjects WHERE Type = 1";
OleDbDataReader reader = null;
try
{
conn.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader.GetString(0));
}
}
catch (Exception e)
{
// Providing something like this in your message would've helped us
// determine what the problem was!
Console.Error.WriteLine("An error occured: {0}", e.Message);
}
finally
{
if (reader != null) reader.Close();
conn.Close();
}Microsoft MVP, Visual C# My Articles
-
I Want to List the Table Names Of a MS Access Database Using ADO Connection. This is How I tried ADODB.Connection myConnection = new ADODB.ConnectionClass(); ADODB.Recordset rsTblNames = new ADODB.RecordsetClass(); if (optAccess.Checked) { ConnStr= "Provider=Microsoft.Jet.OLEDB.4.0;User ID=;Data Source="+ txtDbPath.Text +";Mode=ReadWrite;Extended Properties='';Jet OLEDB:System database='';Jet OLEDB:Registry Path='';Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False"; myConnection.Open(ConnStr,"","",0); StrSql= "SELECT Name FROM MSysObjects WHERE Type = 1"; rsTblNames.Open(StrSql, myConnection, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockReadOnly,0); } But This returns a Error
To get a list of table names: code:-------------------------------------------------------------------------------- SELECT Name FROM MSysObjects WHERE Type = 1; -------------------------------------------------------------------------------- To get list of query names: code:-------------------------------------------------------------------------------- SELECT Name FROM MSysObjects WHERE Type = 5; -------------------------------------------------------------------------------- additional information: code:-------------------------------------------------------------------------------- To get list of Forms: SELECT Name FROM MsysObjects WHERE Type =-32768; To get list of Reports: SELECT Name FROM MsysObjects WHERE Type = -32764; To get list of Modules: SELECT Name FROM MsysObjects WHERE Type = -32761; To get list of Macros: SELECT Name FROM MsysObjects WHERE Type = -32766;