How to get fields name from a table using Java/VB6/VB.NET/C# ?

larva

Member
Jul 15, 2001
185
0
0
Hi,

How can I get fields name from a table using Java/VB6/VB.NET/C# and display in a list box ? Thanks !

regards,
Larva
 

BenRosey

Senior member
Nov 30, 2000
465
0
0
As far as VB6 is concerned...

Open a record set with an SQL command like "SELECT DISTINCT abc from xyz"

Then loop through the record set until it is EOF and add the items to the listbox.

Do While Not recordset.EOF
ListBox.AddItem recordset.Fields("abc")
recordset.MoveNext
Loop

I hope that helps.
 

larva

Member
Jul 15, 2001
185
0
0
Hi,

I don't want to get the data inside the column. I want the name of the column. Thanks !

----------------------------------------
Name | ID |
-----------------------------------------
Larva 1000

and I want to get the "Name" and "ID", not the data.

regards,
Larva
 

larva

Member
Jul 15, 2001
185
0
0
Hi,

If lets say I don't know how many column and the column name inside a table, how can I use Java/VB6/VB.NET to list all of them out into a listbox or other ? Thanks !

regards,
Larva
 

Beau

Lifer
Jun 25, 2001
17,730
0
76
www.beauscott.com
In vb6:

'-- First you need to create a connection to your DB:

Dim Conn
Set Conn = CreateObject("ADODB.Connection")
Conn.Open "DSN=MyDSN", "Username", "Password" [, options]

'-- Second, create a recordset that selects all of the fields, you only need one record though.

Dim rs
Set rs = CreateObject("ADODB.Recordset")
rs.Open "SELECT * FROM MyTable WHERE ID=1", Conn, 1

'--- Third, Set the listbox (list1) value by looping through the field names list:
Dim objField, i
i = 0
For Each objField in rs.Fields
     List1.AddItem objField.Name, i
     i = i + 1
Next
 

larva

Member
Jul 15, 2001
185
0
0
Hi Beau6183,

Thanks a lot for your info. How about if I wanna display all the tables name available in my database into the listbox ? Thank you !

regards,
Larva
 

Beau

Lifer
Jun 25, 2001
17,730
0
76
www.beauscott.com
Originally posted by: larva
Hi Beau6183,

Thanks a lot for your info. How about if I wanna display all the tables name available in my database into the listbox ? Thank you !

regards,
Larva

What kind of database are you using? Are you using SQL connectivity or ADO? If you are using ADO, it does not support table cataloging. The only way to do this is to create another table that acts as a list of the existing tables -- you'll have to input this data and keep it updated. However, SQL does support cataloging:

'---|| SQL Table Catalog ||---

'--- Create an instance of the SQL Server Connection ---
Dim OServer, oSQLdb as Object
Set OServer = CreateObject("sqlole.sqlserver")
OServer.Connect "MyServerName", "MyUserID", "MyPassword"

'--- Connect to the specific database on the SQL Server ---
Set oSQLdb = oServer.Databases(MyDatabaseName)

'--- Loop through each table name and assign the value to a listbox (list1) ---
Dim objTable, i
i = 0
For Each objTable in oSQLdb.Tables
     List1.AddItem objTable.Name, i
     i = i + 1
Next
 

larva

Member
Jul 15, 2001
185
0
0
Hi Beau6183,

I m trying to use Java and VB6.0 ADO to connect to my Oracle8i DB. In the last message u wrote :

Dim OServer
Set OServer = CreateObject("sqlole.sqlserver")
OServer.Connect "MyServerName", "MyUserID", "MyPassword"

'--- Loop through each table name and assign the value to a listbox (list1) ---
Dim objTable, i
i = 0
For Each objTable in OServer.Tables
List1.AddItem objTable.Name, i
i = i + 1
Next

What should the OServer 'Dim' As ? Like ADODB.Connection or something else ? If I don't Dim as something, I got an error message when compile.
The "MyServerName" is the DB Service name or the Server name ? Thanks !


regards,
Larva

 

larva

Member
Jul 15, 2001
185
0
0
Hi,

I am so sorry that I have lost my previous thread for the Java answer. Thanks !

regards,
Larva
 

Beau

Lifer
Jun 25, 2001
17,730
0
76
www.beauscott.com
Originally posted by: larva
Hi Beau6183,

I m trying to use Java and VB6.0 ADO to connect to my Oracle8i DB. In the last message u wrote :

Dim OServer
Set OServer = CreateObject("sqlole.sqlserver")
OServer.Connect "MyServerName", "MyUserID", "MyPassword"

'--- Loop through each table name and assign the value to a listbox (list1) ---
Dim objTable, i
i = 0
For Each objTable in OServer.Tables
List1.AddItem objTable.Name, i
i = i + 1
Next

What should the OServer 'Dim' As ? Like ADODB.Connection or something else ? If I don't Dim as something, I got an error message when compile.
The "MyServerName" is the DB Service name or the Server name ? Thanks !


regards,
Larva

It'll work if you just dim at as an object, but I suppose you could do it as a SQLOLE.SQLServer.
MyServerName should be the Server name. I left out a line, check the original post.
 

damiano

Platinum Member
May 29, 2002
2,322
1
0
Originally posted by: larva
Hi Beau6183,

I m trying to use Java and VB6.0 ADO to connect to my Oracle8i DB. In the last message u wrote :

Dim OServer
Set OServer = CreateObject("sqlole.sqlserver")
OServer.Connect "MyServerName", "MyUserID", "MyPassword"

'--- Loop through each table name and assign the value to a listbox (list1) ---
Dim objTable, i
i = 0
For Each objTable in OServer.Tables
List1.AddItem objTable.Name, i
i = i + 1
Next

What should the OServer 'Dim' As ? Like ADODB.Connection or something else ? If I don't Dim as something, I got an error message when compile.
The "MyServerName" is the DB Service name or the Server name ? Thanks !


regards,
Larva

there is a system table called ALL_TAB_COLUMNS and another one called ALL_TABLES
in oracle 8i

you can just select the info you need from these tables

:)

hope this helps


 

larva

Member
Jul 15, 2001
185
0
0
Hi damiano,

You mean I just do As follows :

sql = "SELECT ALL_TABLES"
rs.Open sql ?

Thanks !

regards,
Larva


 

damiano

Platinum Member
May 29, 2002
2,322
1
0
select * from all_tables

select * from all_tabs_columns where table_name ="yourtable"

and just check for what you need...