473,718 Members | 2,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ob ject sender, EventArgs e)

{

OpenFileDialog openFileDialog1 = new OpenFileDialog( );

openFileDialog1 .InitialDirecto ry = "c:\\";

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

openFileDialog1 .FilterIndex = 2;

openFileDialog1 .RestoreDirecto ry = true;

if (openFileDialog 1.ShowDialog() == DialogResult.OK )

{

String strFilename;

strFilename = openFileDialog1 .FileName;

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

cnAccess.Open() ;

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

System.Data.Dat aTable DataTable1 = cnAccess.GetSch ema();

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

{

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

}

}

}
Dec 21 '05 #1
2 6389
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 (ConnectionStri ng == null)
throw new InvalidOperatio nException("Con nectionString must be set before
trying to retrieve the Table Names.");
_fileName = GetFileNameFrom ConnectionStrin g(ConnectionStr ing);
if (_fileName == null)
throw new ArgumentExcepti on("Invalid Connection String.",
"ConnectionStri ng");
ArrayList tableList = new ArrayList();
ApplicationClas s AccessApp=null;
Database CurrentDb=null;
try
{
AccessApp = new ApplicationClas s();
AccessApp.OpenC urrentDatabase( _fileName, false, null);
CurrentDb = (Database)Acces sApp.CurrentDb( );
foreach(TableDe f table in CurrentDb.Table Defs)
{
if ( !table.Name.Sta rtsWith("MSys") )
tableList.Add( new Table( table.Name, table.Fields.Co unt) );
}
return tableList;
}
finally
{
if (CurrentDb != null)
CurrentDb.Close ();
if (AccessApp != null)
{
AccessApp.Close CurrentDatabase ();
AccessApp.DoCmd .Close(AcObject Type.acDefault, null, AcCloseSave.acS aveNo);
AccessApp.DoCmd .Quit(AcQuitOpt ion.acQuitSaveN one);
AccessApp = null;
}
}
}

public ArrayList GetFieldNames(s tring tableName)
{
if (ConnectionStri ng == null)
throw new InvalidOperatio nException("Con nectionString must be set before
trying to retrieve the Table Names.");
_fileName = GetFileNameFrom ConnectionStrin g(ConnectionStr ing);
if (_fileName == null)
throw new ArgumentExcepti on("Invalid Connection String.",
"ConnectionStri ng");
ArrayList fieldList = new ArrayList();
ApplicationClas s AccessApp=null;
Database CurrentDb=null;
TableDef table=null;
try
{
AccessApp = new ApplicationClas s();
AccessApp.OpenC urrentDatabase( _fileName, false, null);
CurrentDb = (Database)Acces sApp.CurrentDb( );
table = CurrentDb.Table Defs[tableName];
foreach(dao.Fie ld field in table.Fields)
{
fieldList.Add(n ew Field(tableName , field.Name, field.Type.ToSt ring() ) );
}
return fieldList;
}
finally
{
if (table != null)
table = null;
if (CurrentDb != null)
CurrentDb.Close ();
if (AccessApp != null)
{
AccessApp.Close CurrentDatabase ();
AccessApp.DoCmd .Close(AcObject Type.acDefault, null, AcCloseSave.acS aveNo);
AccessApp.DoCmd .Quit(AcQuitOpt ion.acQuitSaveN one);
AccessApp = null;
}
}
}
private static string GetFileNameFrom ConnectionStrin g(string
connectionStrin g)
{
string connectionStrin gExpression = "Provider=(?'pr ovider'.[^;]+);Data
Source=(?'file' .[^;]+);";
Match m = Regex.Match(con nectionString, connectionStrin gExpression);
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 (ConnectionStri ng == null)
throw new InvalidOperatio nException("Con nectionString must be set before
trying to retrieve the Table Names.");
ArrayList tableList = new ArrayList();
OleDbCommand cmd = null;
try
{
if (conn == null)
conn = new OleDbConnection (ConnectionStri ng);
conn.Open();
cmd = new OleDbCommand("S ELECT Name, 0 as TotalColumns FROM MSysObjects
where Type = 1 AND NAME NOT IN
(\"MSysObjects\ ",\"MSysACEs\", \"MSysQueries\" ,\"MSysRelation ships\",\"MSysA ccessObjects\", \"MSysAccessXML \")",
conn);
using( OleDbDataReader dr = cmd.ExecuteRead er() )
{
while (dr.Read())
{
tableList.Add( new Table( dr["Name"].ToString(),
long.Parse(dr["TotalColum ns"].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
2033
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 record is added. The problem is that I don't know how to do it, when I'm not using stored procedures. I found an article in which OnRowUpdated event of the dataadapter is used. On this event ExecuteScalar method of the oledbcommand object is called...
0
1580
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 2000) as follows USERS TABLE
6
16124
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 Image returned in the dataset to a picture box, like: For Each dr in ds.tables(0).rows Pic.Image = dr("ImageColumnName") 'not this simple! Next Thanks,
5
2233
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 OleDbConnection1.State = ConnectionState.Closed Then OleDbConnection1.Open() Else
4
2409
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 there anything that i can do to speed the app up a bit? namespace SQLToAccessBackup
11
2027
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 data access layer, which contists of separate servicebases for each object within my database. CompanyDataService (contains CRUD methods) ContactDataService (contains CRUD methods)
1
7456
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 tables.Rows) { string tableName = r as string; //
5
7949
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, Card, Line. Node table contains Node name, Node id, etc.. Card table contains Node id, Card Type.. Line table contains Node id, Line Type.. Now i want values from fields Node name,Card Type, Line Type and to be put in Hashmap..so that based...
0
8827
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8724
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9053
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5971
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4481
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2555
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.