This is a multi-part message in MIME format.
------=_NextPart_000_000B_01C797EE.028DDA50
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi,
Try following:-
'Retrieve Dataset Schema
Dim cn As New SqlConnection()
Dim cmd As New SqlCommand()
Dim schemaTable As DataTable
Dim myReader As SqlDataReader
'Dim myField As DataRow
'Dim myProperty As DataColumn
cn.connectionstring =3D My.Settings.AccDataConnectionString
cn.Open()
'Retrieve records from the Employees table into a DataReader.
cmd.Connection =3D cn
''I used 1=3D0 because I don't want to waste time in retrieving the =
records, just a Schema
cmd.CommandText =3D "SELECT * FROM Customers Where 1=3D0"=20
myReader =3D cmd.ExecuteReader(CommandBehavior.KeyInfo)
'Retrieve column schema into a DataTable.
schemaTable =3D myReader.GetSchemaTable()
''For each field in the table...
For Each myField In schemaTable.Rows
'For each property of the field...
For Each myProperty In schemaTable.Columns
' 'Display the field name and value.
Console.WriteLine(myProperty.ColumnName & " =3D " & =
myField(myProperty).ToString())
Next
Console.WriteLine()
' 'Pause.
Console.ReadLine()
Next
Best Regards,
Luqman
"Ken" <
fkml99@yahoo.com>wrote in message =
Quote
I'm trying to run a loop to capture column property information from a
table in my datasource. Can anybody see where this is going wrong?
=20
Dim tbl As New DataTable
Dim col As DataColumn
Dim x As Integer
Dim colName(99) As String
Dim colType(99) As String
cn.Open()
tbl =3D cn.GetSchema("Orders") 'Orders is a table in the
datasource
For Each col In tbl.Columns
x +=3D 1
colName(x) =3D col.ColumnName.ToString()
colType(x) =3D col.DataType.ToString()
Next
cn.Close()
=20
If it's not obvious, my intended result are two variables for each
column from the original datatable that will capture a) the name of
the column, and b) the datatype of the column. I will then use these
variables to drive subsequent logic.
=20
I'm getting an error on the GetSchema line: The requested collection
(Orders) is not defined.
=20
I have little experience with the GetSchema method, so any advice on
how to accomplish this is appreciated.
=20
Thx,
Ken
------=_NextPart_000_000B_01C797EE.028DDA50
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.6000.16441" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV>
<P><FONT face=3DArial size=3D2>Hi,</FONT></P>
<P><FONT face=3DArial size=3D2></FONT> </P>
<P><FONT face=3DArial size=3D2>Try following:-</FONT></P>
<P><FONT face=3DArial size=3D2></FONT> </P>
<P><FONT face=3DArial size=3D2>'Retrieve Dataset Schema</FONT></P>
<P><FONT face=3DArial size=3D2>Dim cn As New SqlConnection()</FONT></P>
<P><FONT face=3DArial size=3D2>Dim cmd As New SqlCommand()</FONT></P>
<P><FONT face=3DArial size=3D2>Dim schemaTable As DataTable</FONT></P>
<P><FONT face=3DArial size=3D2>Dim myReader As SqlDataReader</FONT></P>
<P><FONT face=3DArial size=3D2>'Dim myField As DataRow</FONT></P>
<P><FONT face=3DArial size=3D2>'Dim myProperty As DataColumn</FONT></P>
<P><FONT face=3DArial size=3D2>cn.connectionstring =3D=20
My.Settings.AccDataConnectionString</FONT></P>
<P><FONT face=3DArial size=3D2>cn.Open()</FONT></P>
<P><FONT face=3DArial size=3D2>'Retrieve records from the Employees =
table into a=20
DataReader.</FONT></P>
<P><FONT face=3DArial size=3D2>cmd.Connection =3D cn</FONT></P>
<P><FONT face=3DArial size=3D2>''I used 1=3D0 because I don't want to =
waste time in=20
retrieving the records, just a Schema</FONT></P>
<P><FONT face=3DArial size=3D2>cmd.CommandText =3D "SELECT * FROM =
Customers Where 1=3D0"=20
</FONT></P>
<P><FONT face=3DArial size=3D2>myReader =3D=20
cmd.ExecuteReader(CommandBehavior.KeyInfo)</FONT></P>
<P><FONT face=3DArial size=3D2>'Retrieve column schema into a =
DataTable.</FONT></P>
<P><FONT face=3DArial size=3D2>schemaTable =3D =
myReader.GetSchemaTable()</FONT></P>
<P><FONT face=3DArial size=3D2>''For each field in the =
table...</FONT></P>
<P><FONT face=3DArial size=3D2>For Each myField In =
schemaTable.Rows</FONT></P>
<P><FONT face=3DArial size=3D2>'For each property of the =
field...</FONT></P>
<P><FONT face=3DArial size=3D2>For Each myProperty In =
schemaTable.Columns</FONT></P>
<P><FONT face=3DArial size=3D2>' 'Display the field name and =
value.</FONT></P>
<P><FONT face=3DArial =
size=3D2> Console.WriteLine(myProperty.ColumnName & "=20
=3D " & myField(myProperty).ToString())</FONT></P>
<P><FONT face=3DArial size=3D2> Next</FONT></P>
<P><FONT face=3DArial size=3D2> Console.WriteLine()</FONT></P>
<P><FONT face=3DArial size=3D2>' 'Pause.</FONT></P>
<P><FONT face=3DArial size=3D2> Console.ReadLine()</FONT></P>
<P><FONT face=3DArial size=3D2>Next</FONT></P>
<P><FONT face=3DArial size=3D2>Best Regards,</FONT></P>
<P><FONT face=3DArial size=3D2>Luqman</FONT></P>
<P><FONT face=3DArial size=3D2></FONT> </P>
<P><FONT face=3DArial size=3D2></FONT> </P></DIV>
<DIV><FONT face=3DArial size=3D2>"Ken" <</FONT><A=20
href=3D"mailto:
fkml99@yahoo.com"><FONT face=3DArial=20
size=3D2>
fkml99@yahoo.com</FONT></A><FONT face=3DArial size=3D2>> =
wrote in message=20
</FONT><A=20
href=3D"news:
1177466970.811703.125840@t38g2000prd.googlegroups.com"><FONT=
=20
face=3DArial=20
size=3D2>news:
1177466970.811703.125840@t38g2000prd.googlegroups.com</FONT=
Quote
</A><FONT=20
face=3DArial size=3D2>...</FONT></DIV><FONT face=3DArial size=3D2>> =
I'm trying to run=20
a loop to capture column property information from a<BR>> table in my =
datasource. Can anybody see where this is going wrong?<BR>>=20
<BR>> Dim tbl As New=20
DataTable<BR>> Dim col As=20
DataColumn<BR>> Dim x As=20
Integer<BR>> Dim =
colName(99) As=20
String<BR>> Dim colType(99) =
As=20
String<BR>> =20
cn.Open()<BR>> tbl =3D=20
cn.GetSchema("Orders") 'Orders is a table in the<BR>>=20
datasource<BR>> For Each =
col In=20
tbl.Columns<BR>> =
=20
x +=3D =
1<BR>> &nbs=
p;=20
colName(x) =3D=20
col.ColumnName.ToString()<BR>> &nbs=
p; =20
colType(x) =3D=20
col.DataType.ToString()<BR>> =
=20
Next<BR>> =
cn.Close()<BR>>=20
<BR>> If it's not obvious, my intended result are two variables for=20
each<BR>> column from the original datatable that will capture a) the =
name=20
of<BR>> the column, and b) the datatype of the column. I will =
then use=20
these<BR>> variables to drive subsequent logic.<BR>> <BR>> I'm =
getting=20
an error on the GetSchema line: The requested collection<BR>> =
(Orders)=20
is not defined.<BR>> <BR>> I have little experience with the =
GetSchema=20
method, so any advice on<BR>> how to accomplish this is =
appreciated.<BR>>=20
<BR>> Thx,<BR>> Ken<BR>></FONT></BODY></HTML>
------=_NextPart_000_000B_01C797EE.028DDA50--
-