如何指示 DbConnection.GetSchema 返回其值的顺序?
我正在将依赖于 ADO 的以 VB6 数据库为中心的应用程序移植到 C#(依赖于 ADO.NET)。此应用程序访问的数据库架构是任意的,我依靠 DbConnection.GetSchema 来检索信息。
据我所知(这就是我需要帮助的地方),GetSchema
应该取代 ADOX.Catalog。所以这就是我所拥有的(对格式感到抱歉!):
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);
}
}
我知道这样可以正确地使用名为 tableName
的表的列名称填充 temp
。但是,该顺序似乎是任意的,而对于 ADOX.Catalog,该顺序与您在 Sql Management Studio 中找到的顺序完全相同。原版是这样的:
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
它们以不同的顺序出现,我不知道该怎么办!
我的问题基本上是:除了 GetSchema 之外,ADO.NET 是否有 ADOX.Catalog 的替代品?如果不是,我怎样才能订购 GetSchema,这样我就不会得到随机排序(这会让我的同事感到困惑,因为他们经常使用这个!)
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!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
DataTable.Select
方法对结果进行排序:You can use the
DataTable.Select
method to order the results: