如何指示 DbConnection.GetSchema 返回其值的顺序?

发布于 2024-10-01 01:10:18 字数 1206 浏览 7 评论 0原文

我正在将依赖于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年不长 2024-10-08 01:10:18

您可以使用 DataTable.Select 方法对结果进行排序:

databaseConnection.Open();
DataTable columns = databaseConnection.GetSchema(
            SqlClientMetaDataCollectionNames.Columns, restrictions);
foreach (DataRow row in columns.Select("", "COLUMN_NAME"))
{
    temp.Add(row["COLUMN_NAME"] as string);
}

You can use the DataTable.Select method to order the results:

databaseConnection.Open();
DataTable columns = databaseConnection.GetSchema(
            SqlClientMetaDataCollectionNames.Columns, restrictions);
foreach (DataRow row in columns.Select("", "COLUMN_NAME"))
{
    temp.Add(row["COLUMN_NAME"] as string);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文