Tabledefs and VB.net

  • Thread starter Thread starter Vanny
  • Start date Start date
V

Vanny

I'm really new to vb.net. What I like to know is what would be the code to
read the definition of table or view. Here is the piece of code in VB6 that
try to find a column name called "year" in a view. How to write this in
VB.net?

For Each fldField In gdbOes.TableDefs(viewname)).Fields
strFieldName = fldField.Name
If strFieldName = "year" Then
blnyearFind = True
Exit For
End If
Next

I use odbc connection in vb6 as well as in vb.net. Above, the connection
name is gdbOes.

Thanks in advance for your help

Vanny
 
It would be helpful to know the data source? Firebird, Oracle, SQLServer,
Access?

Robin S.
 
¤ We're using Adaptive Server Anywhere ASA9.

If you have an OLEDB provider for this data source then you can probably use GetOleDbSchemaTable.
Below is an example that uses an Access database:

Dim DatabaseConnection As New System.Data.OleDb.OleDbConnection
Dim SchemaTable As DataTable

DatabaseConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Test Files\db1 XP.mdb"

DatabaseConnection.Open()

'Retrieve schema information about Table1 Columns.
SchemaTable =
DatabaseConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns, _
New Object() {Nothing, Nothing, "Table1"})

Dim RowCount As Int32
For RowCount = 0 To SchemaTable.Rows.Count - 1
Console.WriteLine(SchemaTable.Rows(RowCount)!COLUMN_NAME.ToString)
Next RowCount

DataGrid1.DataSource = SchemaTable

DatabaseConnection.Close()


Paul
~~~~
Microsoft MVP (Visual Basic)
 
Thanks for your help. Unfortunately, this is a group project, I can not use
OLEDB. I could not find the equivalence of this code in odbc:
SchemaTable =
DatabaseConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns,
_
New Object() {Nothing, Nothing, "Table1"})
The function Getschema that does work with odbc connection, seems not having
the same purpose. There is another one Getschematable, but it works with
datareader.

Thanks
Vanny
 
¤ Thanks for your help. Unfortunately, this is a group project, I can not use
¤ OLEDB. I could not find the equivalence of this code in odbc:
¤ SchemaTable =
¤ DatabaseConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns,
¤ _
¤ New Object() {Nothing, Nothing, "Table1"})
¤ The function Getschema that does work with odbc connection, seems not having
¤ the same purpose. There is another one Getschematable, but it works with
¤ datareader.
¤

The ODBC provider is rather limited in this respect. I'm not sure what type of database you are
working with but if it's Microsoft Access then ODBC is a poor choice for reasons of stability and
functionality.

If you're working with a server based database then you may be able to tap into the DDL to return
this information.


Paul
~~~~
Microsoft MVP (Visual Basic)
 
Back
Top