473,395 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Retrieving Access Database Tables

RSH
I am struggling a bit trying to get at all of the Table names in a given
Access database.

I have the code below which should be retrieving the information...i am just
having a bit of trouble displaying the Table names out of the datatable.

Thanks for any assistance you might be able to offer!
Ron

private void browse_Click(object sender, EventArgs e)

{

OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = "c:\\";

openFileDialog1.Filter = "Access Databases|*.mdb";

openFileDialog1.FilterIndex = 2;

openFileDialog1.RestoreDirectory = true;

if (openFileDialog1.ShowDialog() == DialogResult.OK)

{

String strFilename;

strFilename = openFileDialog1.FileName;

cnAccess = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + "Data
Source=" + strFilename + ";Persist Security Info=False");

cnAccess.Open();

string[] restrictions1 = new string[] { null, null, null, "TABLE" };

System.Data.DataTable DataTable1 = cnAccess.GetSchema();

for (int i = 0; i < DataTable1.Rows.Count; i++)

{

checkedListBox1.Items.Add(DataTable1.Rows[0].Table.Columns[0].ToString());
<----- This is where I need to display the Table names

}

}

}
Dec 21 '05 #1
2 6353
Last time i did this I found that using the Access Application gave my more
detailed information. the only problem is that the user/server will have to
have Office Access installed.

public ArrayList GetTableNames()
{
if (ConnectionString == null)
throw new InvalidOperationException("ConnectionString must be set before
trying to retrieve the Table Names.");
_fileName = GetFileNameFromConnectionString(ConnectionString);
if (_fileName == null)
throw new ArgumentException("Invalid Connection String.",
"ConnectionString");
ArrayList tableList = new ArrayList();
ApplicationClass AccessApp=null;
Database CurrentDb=null;
try
{
AccessApp = new ApplicationClass();
AccessApp.OpenCurrentDatabase(_fileName, false, null);
CurrentDb = (Database)AccessApp.CurrentDb();
foreach(TableDef table in CurrentDb.TableDefs)
{
if ( !table.Name.StartsWith("MSys") )
tableList.Add( new Table( table.Name, table.Fields.Count) );
}
return tableList;
}
finally
{
if (CurrentDb != null)
CurrentDb.Close();
if (AccessApp != null)
{
AccessApp.CloseCurrentDatabase();
AccessApp.DoCmd.Close(AcObjectType.acDefault, null, AcCloseSave.acSaveNo);
AccessApp.DoCmd.Quit(AcQuitOption.acQuitSaveNone);
AccessApp = null;
}
}
}

public ArrayList GetFieldNames(string tableName)
{
if (ConnectionString == null)
throw new InvalidOperationException("ConnectionString must be set before
trying to retrieve the Table Names.");
_fileName = GetFileNameFromConnectionString(ConnectionString);
if (_fileName == null)
throw new ArgumentException("Invalid Connection String.",
"ConnectionString");
ArrayList fieldList = new ArrayList();
ApplicationClass AccessApp=null;
Database CurrentDb=null;
TableDef table=null;
try
{
AccessApp = new ApplicationClass();
AccessApp.OpenCurrentDatabase(_fileName, false, null);
CurrentDb = (Database)AccessApp.CurrentDb();
table = CurrentDb.TableDefs[tableName];
foreach(dao.Field field in table.Fields)
{
fieldList.Add(new Field(tableName, field.Name, field.Type.ToString() ) );
}
return fieldList;
}
finally
{
if (table != null)
table = null;
if (CurrentDb != null)
CurrentDb.Close();
if (AccessApp != null)
{
AccessApp.CloseCurrentDatabase();
AccessApp.DoCmd.Close(AcObjectType.acDefault, null, AcCloseSave.acSaveNo);
AccessApp.DoCmd.Quit(AcQuitOption.acQuitSaveNone);
AccessApp = null;
}
}
}
private static string GetFileNameFromConnectionString(string
connectionString)
{
string connectionStringExpression = "Provider=(?'provider'.[^;]+);Data
Source=(?'file'.[^;]+);";
Match m = Regex.Match(connectionString, connectionStringExpression);
if (m.Success)
return m.Groups["file"].Value;
else
return null;
}
Dec 21 '05 #2
Here is the older script from that project uses MSysObjects, but you can't
get much info about the fields or the field types.

public ArrayList GetTableNames()
{
if (ConnectionString == null)
throw new InvalidOperationException("ConnectionString must be set before
trying to retrieve the Table Names.");
ArrayList tableList = new ArrayList();
OleDbCommand cmd = null;
try
{
if (conn == null)
conn = new OleDbConnection(ConnectionString);
conn.Open();
cmd = new OleDbCommand("SELECT Name, 0 as TotalColumns FROM MSysObjects
where Type = 1 AND NAME NOT IN
(\"MSysObjects\",\"MSysACEs\",\"MSysQueries\",\"MS ysRelationships\",\"MSysAccessObjects\",\"MSysAcce ssXML\")",
conn);
using( OleDbDataReader dr = cmd.ExecuteReader() )
{
while (dr.Read())
{
tableList.Add( new Table( dr["Name"].ToString(),
long.Parse(dr["TotalColumns"].ToString()) ) );
}
}
return tableList;
}
finally
{
if (cmd != null)
{
cmd.Dispose();
}
if (conn != null)
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
}
Dec 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Taras | last post by:
Hello! I have a problem. I'm using a dataset in VB.NET with multiple tables with relations between them. I would like dataset to update to all related tables with right identity, when certain...
0
by: Alistair | last post by:
Hi all, I am creating a database based site that keeps track of books, who has read them and the comments they have. After a little help in M.P.I.asp.DB I managed to create a database (access...
6
by: Dean Slindee | last post by:
Does anybody have an actual example of retrieving an Image data type column from a SQL Server table using a dataset (not a datareader)? I would like to see the statements that would move the...
5
by: aniket_sp | last post by:
i am using a data adapter and a dataset for filling and retrieving data into .mdb database. following is the code..... for the form load event Dim dc(0) As DataColumn Try If...
4
by: RSH | last post by:
Hi, I have a situation where I have created a little application that makes an Access database from a SQL Database for reporting purposes. it does the job well, but it seems a bit slow. Is...
11
by: Nemisis | last post by:
Hi everyone, sorry if this post gets really long, i just wanna make sure i fully explain what i am trying to do. I am new to OOP and .net 2.0, so if this is obvious, i am sorry. I have wrote a...
1
by: Chris | last post by:
Hi, I'm using the following code DataTable tables = ((OleDbConnection)oleconn).GetOleDbSchemaTable( OleDbSchemaGuid.Tables, new object { null, null, null, "TABLE" }); foreach (DataRow r in...
5
by: Smartpriya | last post by:
Hello, I am doing project in Swing and Access. I have some Tables in database.. Now I need to retrive some values from it and put in Hashmap. For ex: I have 3 tables like Node,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.