views:

1110

answers:

2

In a C# 2.0 I have a list of fields and a list of values (stored as strings), a table name, and an ODBC Connection.

I need to get the data types of the fields that are on that table, so I know how to generate my sql.

What's the best way to get that information from what I've got?

My only idea is to do a SELECT TOP 0 * FROM @TableName in a data adapter, get back a dataset, and iterate through the list of field names against the datacolumn's in the datatable.

Is there any better way to go about this?

+3  A: 

Try this

select * from sys.columns where object_id = object_id('MyTable')

Hope this helps.

Nick Berardi
You can also use INFORMATION_SCHEMA if it is available.
Sean Bright
True Sean, depending on the version of SQL he is using or even if he is using SQL server.
Nick Berardi
This answer is good in general, but unfortunately I know my Database will be changing in the next year, and want to avoid code that I'll have to change.
C. Ross
+2  A: 

You're best option is to query your datbases system tables like Nick Beradi mentioned. If that's not an option for whatever reason, you can do something like this:

using (SqlConnection conn = new SqlConnection(myConnectionString))
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * from MyTable", conn))
                {
                    DataTable table = new DataTable();
                    adapter.FillSchema(table, SchemaType.Mapped);
                    //at this point, table will have no rows, but will have all the columns of which you can get their datatypes
                }
            }
BFree