Access Database Schema

P

Peter

C# .NET 3.5

Does anyone have an example of how to get Schema of Access Database - The
Listing of all tables and fields in each table

Thank You


Peter
 
A

Alberto Poblacion

Peter said:
Does anyone have an example of how to get Schema of Access Database - The
Listing of all tables and fields in each table

To get the tables:

OleDbConnection cn = new OleDbConnection(connectionString);
...
DataTable schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new object[] {null, null, null, "TABLE"});
//schemaTable now contains one row for each table in the database

To get the fields:

DataTable schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
new Object[]{null, null, tableName});
//schemaTable now contains one row for each column in tableName
 
C

Colbert Zhou [MSFT]

Hello Peter,

Thanks for using Microsoft Newsgroup Support Service, this is Colbert Zhou
[MSFT] and I will be working on this issue with you.

The first approach, we can use GetOleDbSchemaTable method to get Access
Database schema. The following codes show how to do it,

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=DatabaseFile.mdb";
OleDbConnection oleDb = new OleDbConnection(connectionString);
oleDb.Open();

object[] arrRestrict;
arrRestrict = new object[] { null, null, "Customers", null };

DataTable tblDbSchema = oleDb.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
arrRestrict);

oleDb.Close();

For more detailed information, please refer the following articles and
documents
http://dotnetfacts.blogspot.com/2008/05/how-to-retrieve-database-schema.html
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection.ge
toledbschematable.aspx

Another option is using ADOX to achieve this objective. ADOX is the object
model of Microsoft Office Acces. We can view, create, and modify the
structure of Access databases by using ADOX objects, methods, and
properties.

Please read the document to know how to list all tables in the Access
DataBase using ADOX,
http://msdn.microsoft.com/en-us/library/aa165325(office.10).aspx

If you have any future questions or concerns, please feel free to let me
know. And I will do a follow up support.

Have a nice day, Peter!

Best regards,
Colbert Zhou (colbertz @online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter

Thank You!

That works!


Alberto Poblacion said:
Peter said:
Does anyone have an example of how to get Schema of Access Database - The
Listing of all tables and fields in each table

To get the tables:

OleDbConnection cn = new OleDbConnection(connectionString);
...
DataTable schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new object[] {null, null, null, "TABLE"});
//schemaTable now contains one row for each table in the database

To get the fields:

DataTable schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
new Object[]{null, null, tableName});
//schemaTable now contains one row for each column in tableName
 
P

Peter

Thank You for your help, I've ended up using ADOX

Colbert Zhou said:
Hello Peter,

Thanks for using Microsoft Newsgroup Support Service, this is Colbert Zhou
[MSFT] and I will be working on this issue with you.

The first approach, we can use GetOleDbSchemaTable method to get Access
Database schema. The following codes show how to do it,

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=DatabaseFile.mdb";
OleDbConnection oleDb = new OleDbConnection(connectionString);
oleDb.Open();

object[] arrRestrict;
arrRestrict = new object[] { null, null, "Customers", null };

DataTable tblDbSchema = oleDb.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
arrRestrict);

oleDb.Close();

For more detailed information, please refer the following articles and
documents
http://dotnetfacts.blogspot.com/2008/05/how-to-retrieve-database-schema.html
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection.ge
toledbschematable.aspx

Another option is using ADOX to achieve this objective. ADOX is the object
model of Microsoft Office Acces. We can view, create, and modify the
structure of Access databases by using ADOX objects, methods, and
properties.

Please read the document to know how to list all tables in the Access
DataBase using ADOX,
http://msdn.microsoft.com/en-us/library/aa165325(office.10).aspx

If you have any future questions or concerns, please feel free to let me
know. And I will do a follow up support.

Have a nice day, Peter!

Best regards,
Colbert Zhou (colbertz @online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
P

Peter

Thank You for your help, I've ended up using ADOX

Colbert Zhou said:
Hello Peter,

Thanks for using Microsoft Newsgroup Support Service, this is Colbert Zhou
[MSFT] and I will be working on this issue with you.

The first approach, we can use GetOleDbSchemaTable method to get Access
Database schema. The following codes show how to do it,

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=DatabaseFile.mdb";
OleDbConnection oleDb = new OleDbConnection(connectionString);
oleDb.Open();

object[] arrRestrict;
arrRestrict = new object[] { null, null, "Customers", null };

DataTable tblDbSchema = oleDb.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
arrRestrict);

oleDb.Close();

For more detailed information, please refer the following articles and
documents
http://dotnetfacts.blogspot.com/2008/05/how-to-retrieve-database-schema.html
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection.ge
toledbschematable.aspx

Another option is using ADOX to achieve this objective. ADOX is the object
model of Microsoft Office Acces. We can view, create, and modify the
structure of Access databases by using ADOX objects, methods, and
properties.

Please read the document to know how to list all tables in the Access
DataBase using ADOX,
http://msdn.microsoft.com/en-us/library/aa165325(office.10).aspx

If you have any future questions or concerns, please feel free to let me
know. And I will do a follow up support.

Have a nice day, Peter!

Best regards,
Colbert Zhou (colbertz @online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top