Getting out field names and constraints-C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
Is there any way to take out all the information from a table(field
names,primary key info etc) and use that to create a totally new table


Would be great if you could suggest the classes required
Would be grateful if some of you guys can post some code samples in C#

Thank Yo
Julia
 
AFAIK, there isn't a class as such that will do this for a given datatable.
You can get the schema info from the db using many different methods
http://www.knowdotnet.com/articles/getoledbschematable.html However this all
depends on a connection to the database and getting the info from the DB's
table. On the other hand, if you are referring to a datatable object, then
you'll need to walk through the Columns Collection and extract what you need
from there. You can do this recursively so it's not too big of a chore. Let
me know if you need the data from the DataBase's table or a local datatable,
I can probably be of more help then.

HTH,

Bill
 
Bill,
I established a connection to the database and then used the GetOleDbSchemaTable method of the connection object

( dt = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] {null, null, null, "Table"}); )

to store it as a datatable.

Now my doubt is how to get the required things from it.For example the field names of the table,the datatypes, the various constraints in the table etc.

Could you help me with a few code snippets which does the above things ?

My final aim is to get the field names,datatypes and the various constraints and create a new identical table in a different database.

----- William Ryan wrote: -----

AFAIK, there isn't a class as such that will do this for a given datatable.
You can get the schema info from the db using many different methods
http://www.knowdotnet.com/articles/getoledbschematable.html However this all
depends on a connection to the database and getting the info from the DB's
table. On the other hand, if you are referring to a datatable object, then
you'll need to walk through the Columns Collection and extract what you need
from there. You can do this recursively so it's not too big of a chore. Let
me know if you need the data from the DataBase's table or a local datatable,
I can probably be of more help then.

HTH,

Bill
 
Back
Top