473,597 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieving table fields from access DB

Hi,

I'm using the following code

DataTable tables = ((OleDbConnecti on)oleconn).Get OleDbSchemaTabl e(
OleDbSchemaGuid .Tables, new object[] { null, null, null,
"TABLE" });
foreach (DataRow r in tables.Rows)
{
string tableName = r["TABLE_NAME "] as string; //
Table-Name ....
//richTextBox1.Te xt += tableName + "\n";
TreeNode node = treeView1.Nodes .Add(tableName) ;
node.Nodes.Add( "Level two node");
}

To get all the tables from a specific access database. If anyone
knows how to get the field/column names for these tables, I'd
appreciate it.

Apr 17 '07 #1
1 7441
"Chris" <co*****@gmail. comwrote in message
news:11******** *************@e 65g2000hsc.goog legroups.com...
Hi,

I'm using the following code

DataTable tables = ((OleDbConnecti on)oleconn).Get OleDbSchemaTabl e(
OleDbSchemaGuid .Tables, new object[] { null, null, null,
"TABLE" });
foreach (DataRow r in tables.Rows)
{
string tableName = r["TABLE_NAME "] as string; //
Table-Name ....
//richTextBox1.Te xt += tableName + "\n";
TreeNode node = treeView1.Nodes .Add(tableName) ;
node.Nodes.Add( "Level two node");
}

To get all the tables from a specific access database. If anyone
knows how to get the field/column names for these tables, I'd
appreciate it.
I don't know if this will work against Access, but I use this against
SQLServer. Try changing all the SQL stuff to OleDB. Sorry; my example is
in VB. If you can't figure it out, post back and I will translate it for
you.

---------------------------
Here are all of the properties (and their types) you can retrieve for the
data columns using a DataReader.

col name = ColumnName, type = System.String
col name = ColumnOrdinal, type = System.Int32
col name = ColumnSize, type = System.Int32
col name = NumericPrecisio n, type = System.Int16
col name = NumericScale, type = System.Int16
col name = IsUnique, type = System.Boolean
col name = IsKey, type = System.Boolean
col name = BaseServerName, type = System.String
col name = BaseCatalogName , type = System.String
col name = BaseColumnName, type = System.String
col name = BaseSchemaName, type = System.String
col name = BaseTableName, type = System.String
col name = DataType, type = System.Type
col name = AllowDBNull, type = System.Boolean
col name = ProviderType, type = System.Int32
col name = IsAliased, type = System.Boolean
col name = IsExpression, type = System.Boolean
col name = IsIdentity, type = System.Boolean
col name = IsAutoIncrement , type = System.Boolean
col name = IsRowVersion, type = System.Boolean
col name = IsHidden, type = System.Boolean
col name = IsLong, type = System.Boolean
col name = IsReadOnly, type = System.Boolean
col name = ProviderSpecifi cDataType, type = System.Type
col name = DataTypeName, type = System.String
col name = XmlSchemaCollec tionDatabase, type = System.String
col name = XmlSchemaCollec tionOwningSchem a, type = System.String
col name = XmlSchemaCollec tionName, type = System.String
col name = UdtAssemblyQual ifiedName, type = System.String
col name = NonVersionedPro viderType, type = System.Int32

Here's how I got this list; tableName is passed in as a String. This shows
the columns you can get, and then shows selected values for each column
defined in the table.

**Note: This list will probably be different for Access.
Dim cn As New SqlConnection(M y.Settings.DBCo nnString)
'put the table name in brackets in case it has spaces in it
Dim SQLString As String = "SELECT * FROM [" & tableName & "]"
Try
cn.Open()
Dim cmd As New SqlCommand(SQLS tring, cn)
Dim rdr As SqlDataReader =
cmd.ExecuteRead er(CommandBehav ior.KeyInfo)
Dim tbl As DataTable = rdr.GetSchemaTa ble
'This shows all of the information you can access about each
column.
For Each col As DataColumn In tbl.Columns
Debug.Print("co l name = " & col.ColumnName & _
", type = " & col.DataType.To String)
Next
For Each row As DataRow In tbl.Rows
'DataTypeName actually gives the same
' data type name as is displayed in SQLServer
Debug.Print("{0 }, ColumnSize = {1}, DataType = {2},
DataTypeName = {3}, IsExpression = {4} ", _
row("ColumnName "), row("ColumnSize "), row("DataType") , _
row("DataTypeNa me"), row("IsExpressi on"))
Next
rdr.Close()
Catch
MessageBox.Show ("Error opening the connection to the database.")
Finally
cn.Close()
End Try
Here's another way to get a list of tables.

Public Sub New(ByVal connectionStrin g As String)
Dim cn As New SqlConnection(c onnectionString )
Try
cn.Open()
'The Restrictions are: table_catalog, table_schema,
' table_name, table_type.
'For my case, the table_catalog is the database name.
'table_Schema is the owner.
'Table_name is nothing because I want all tables.
'table_type is "BASE TABLE".
Dim restrictions() As String = New String() _
{My.Settings.Da tabaseName, "dbo", Nothing, "BASE TABLE"}
Dim dt As DataTable = cn.GetSchema("T ables", restrictions)

'Uncomment this if you want to see the columns you can
' access from the GetSchema command.
'I'm leaving it in here for future reference.
'For Each col As DataColumn In dt.Columns
' Debug.Print(col .ColumnName.ToS tring)
'Next

For Each rw As DataRow In dt.Rows

'Uncomment this if you want to see the values
' for each of these columns.
'I'm leaving it in here for future reference.
'Debug.Print("T able_Catalog = {0}, Table_Schema = {1},
Table_Name = {2}, Table_Type = {3}", _
' rw.Item("TABLE_ CATALOG"), rw.Item("TABLE_ SCHEMA"), _
' rw.Item("TABLE_ NAME"), rw.Item("TABLE_ TYPE"))

'sysdiagrams shows up if you have a database diagram;
exclude it here
If rw.Item("TABLE_ NAME").ToString .ToUpper <"SYSDIAGRAMS "
Then
Me.Add(rw.Item( "TABLE_NAME").T oString)
End If
Next

'sort the list of tables
Me.Sort()

Catch
MessageBox.Show ("Error opening connection to database.")
Finally
cn.Close()
End Try
End Sub

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
-----------------------------------------------
Apr 18 '07 #2

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

Similar topics

0
1575
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
2
6377
by: RSH | last post by:
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
6
1811
by: Duderino82 | last post by:
I was wondering if there is a way to collect the names of the fields from a specific table. I think the soluction is to be researched in the sql code but maybe someone knows of a way to o so directly from php. Example. Table: Categories Field1: type1 Field2: type2 Field3: type3 Field4: type4
17
2687
by: Rico | last post by:
Hello, I am in the midst of converting an Access back end to SQL Server Express. The front end program (converted to Access 2003) uses DAO throughout. In Access, when I use recordset.AddNew I can retrieve the autonum value for the new record. This doesn't occur with SQL Server, which of course causes an error (or at least in this code it does since there's an unhandled NULL value). Is there any way to retrieve this value when I add a...
1
1450
by: Olaf Rabbachin | last post by:
Hi folks, I'm looking for a way of retrieving the default-value of a table's field within an Access 2003 MDB. Any hints on how to accomplish this from ASP.Net? Thanks, Olaf -- My .02: www.Resources.IntuiDev.com
3
1282
by: NasirMunir | last post by:
I just copied a table to access from excel. I named that as "Contents". I am trying to create a look-up field on a form which can return the tuples in a list box. My code is: Option Compare Database Private Sub List3_BeforeUpdate(Cancel As Integer) End Sub
0
3376
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options within options. I have everything being dynamically named from the previously dynamically named element. (I hope this makes sense.) I am not able to retrieve any of the dynamically created values. I can view them on the source page but can't pull them...
9
35422
ADezii
by: ADezii | last post by:
One question which pops up frequently here at TheScripts is: 'How do I retrieve data from a Recordset once I've created it?' One very efficient, and not that often used approach, is the GetRows() Method of the Recordset Object. This Method varies slightly from DAO to ADO, so for purposes of this discussion, we'll be talking about DAO Recordsets. The ADO approach will be addressed in the following Tip. We'll be using a Query, consisting of 5...
3
43465
ADezii
by: ADezii | last post by:
Last Tip, we demonstrated the technique for retrieving data from a DAO Recordset, and placing it into a 2-dimensional Array using the GetRows() Method. This week, we will cover the same exact Method (GetRows()), but only as it applies to an ADO Recordset. Although there are similarities in the 2 methodologies, the ADO Method offers 2 more Optional Arguments, is a little more complex, and of course, the syntax is different in creating the...
0
7886
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
8272
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...
1
8035
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8258
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
6688
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...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3927
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1494
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1238
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.