FieldNames and PrimaryKeys in Table

G

Gerrit

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=TESTDB";

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

Gerrit

I have seen this, but I tried a lot, but I can not make an OleDbConnection
to my Database. That 's the problem I think. Is is possible with an
OdbcConnection do you think?

--

www.gsnel.nl

sloan said:
See this KB

http://support.microsoft.com/kb/309488




Gerrit said:
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=TESTDB";

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

Truong Hong Thi

Try including CommanBehavior.KeyInfo in the call of ExecuteReader.
If no help, maybe the underlying ODBC driver does not support this
function.
 
G

Gerrit

This is not working, but thanks for your answer. I searched today and I
found a connectionstring for oledb connection, so I look further in that
direction.

--

www.gsnel.nl
Truong Hong Thi said:
Try including CommanBehavior.KeyInfo in the call of ExecuteReader.
If no help, maybe the underlying ODBC driver does not support this
function.
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=TESTDB";

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

Chris Dunaway

Gerrit said:
I have seen this, but I tried a lot, but I can not make an OleDbConnection
to my Database. That 's the problem I think. Is is possible with an
OdbcConnection do you think?

Why can you not make an OleDbConnection to your db? Are you getting an
error? What database server are you using and what does your code look
like to create the OleDbConnection? What does the connection string
look like? Perhaps you can try this site to get the correct connection
string: www.connectionstrings.com

Chris
 
G

Gerrit

Chris Dunaway said:
Why can you not make an OleDbConnection to your db? Are you getting an
error? What database server are you using and what does your code look
like to create the OleDbConnection? What does the connection string
look like? Perhaps you can try this site to get the correct connection
string: www.connectionstrings.com

Chris

Hi Chris,

It is a Progress 9.1D database, with OpenLink ODBC driver. There is an OleDb
driver, but we have no licence for that driver. I became a message that the
..lic file could not be found. We have only licence for ODBC. Maybe there are
free other drivers, but I don 't know them.

Gerrit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top