"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.
-----------------------------------------------