I am porting a VB6 database-centric application relying on ADO to C# (relying on ADO.NET). The schema of the databases this application access are arbitrary and I’m relying on DbConnection.GetSchema to retrieve information.
As near as I can tell (And here’s where I need help), GetSchema is supposed to replace the ADOX.Catalog. So here’s what I have (sorry about the formatting!):
List<string> temp = new List<string>();
string[] restrictions = new string[4] { null, null, tableName, null };
using (SqlConnection databaseConnection = new SqlConnection(connString))
{
databaseConnection.Open();
foreach (DataRow row in databaseConnection.GetSchema(
SqlClientMetaDataCollectionNames.Columns, restrictions).Rows)
{
temp.Add(row["COLUMN_NAME"] as string);
}
}
I know for a fact that this correctly fills temp with the names of the columns for the table named tableName. However, the order seems arbitrary, whereas with ADOX.Catalog the order is exactly the same as you’d find it in Sql Management Studio. Here’s how the original does it:
Dim cat As New ADOX.Catalog
Dim T As ADOX.Table
Dim C As ADOX.Column
Set cat.ActiveConnection = conn
'retrieve list of fields for this table'
Set T = cat.Tables(tableName)
For Each C In T.Columns
temp.Add C.Name
Next
They come out in different orders, and I’m not sure what to do about it!
My question is basically: Does ADO.NET have a replacement for ADOX.Catalog other than GetSchema? If it doesn’t how can I order GetSchema so that I don’t get random ordering (it’s going to confuse my co-workers who are using this so badly!)
You can use the
DataTable.Selectmethod to order the results: