Hello,
I try to display the fieldnames and the primary key of a table, with this
code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Odbc;
namespace TableFieldsInfo
{
class Program
{
static void Main(string[] args)
{
string cnString =
"DSN=MyDsn;SERVER=SERVER1;UID=ME;PWD=;DATABASE=TES TDB";
OdbcConnection cn = new OdbcConnection();
cn.ConnectionString = cnString;
cn.Open();
OdbcCommand cmd = new OdbcCommand();
cmd.Connection = cn;
cmd.CommandText = "SELECT * FROM ARTICLES";
OdbcDataReader dr =
cmd.ExecuteReader(CommandBehavior.CloseConnection) ;
DataTable dt = dr.GetSchemaTable();
DisplayFields(dt);
}
private static void DisplayFields(System.Data.DataTable table)
{
foreach (System.Data.DataRow row in table.Rows)
{
foreach (System.Data.DataColumn col in table.Columns)
{
Console.WriteLine("{0} = {1}", col.ColumnName,
row[col]);
}
Console.WriteLine("===========================");
}
}
}
}
I have tried a lot, but I can only make an OdbcConnection.
With this code, I became a list of the fieldnames, but the field who is the
primary key, the output is: IsKey = false.
Do you have an idea how I can get the primary key of my table?
Thank you very much.
--
www.gsnel.nl