如何从SqlDataReader获取列的表名

发布于 2024-09-06 21:48:41 字数 686 浏览 7 评论 0原文

我有一个从配置文件中获取的 SQL 查询,该查询通常包含 3-6 个连接。

我需要在运行时根据 SqlDataReader 表示的结果集查找每列的表名称。

以下是一些不起作用的事情:

  • SqlDataReader.GetName 返回列名,但不返回表名。
  • SqlDataReader.GetSchemaTable 返回包含列信息的数据表 - 但所有表名称均为空。
  • 查询 information_schema 没有帮助,因为我需要有关当前查询结果的数据(并且列名不是唯一的 - 不同表中存在具有相同名称的列)。

我在控制台应用程序中使用 .net 3.5SP1/C#/SQL Server 2008。

编辑:我知道这不可能适用于所有情况,因为“列”可以由多个表、函数甚至常量表达式组合而成 - 我正在寻找适用于简单情况的东西。

编辑2:发现它不起作用的原因 - 您可以使用 SqlDataReader.GetSchemaTable 来获取表信息,但您必须将 CommandBehavior 设置为 KeyInfo,您可以在 ExecuteReader 调用中执行此操作:

reader = cmd.ExecuteReader(CommandBehavior.KeyInfo);

I have an SQL query I get from a configuration file, this query usually contains 3-6 joins.

I need to find at run time, based on the result set represented by SqlDataReader, to find the name of the table for each column.

Here are some thing that don't work:

  • SqlDataReader.GetName returns the column name but not the table name.
  • SqlDataReader.GetSchemaTable returns a data table with column information - but all the table names are null.
  • Querying information_schema doesn't help because I need data on the results of the current query (and the column names are not unique - there are columns with the same name in different tables).

I'm using .net 3.5SP1/ C#/ SQL Server 2008 in a console application.

EDIT: I know this is not possible for all cases since a "column" can be combined from multiple tables, a function or even a constant expression - I'm looking for something that works in the simple case.

EDIT 2: Found out why it didn't work - You can use SqlDataReader.GetSchemaTable to get table information but you have to set CommandBehavior to KeyInfo, you do that in the ExecuteReader call:

reader = cmd.ExecuteReader(CommandBehavior.KeyInfo);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

落叶缤纷 2024-09-13 21:48:41

您可以使用 SqlDataReader.GetSchemaTable 获取表信息,但必须将 CommandBehavior 设置为 KeyInfo,您可以在 ExecuteReader 调用中执行此操作:

reader = cmd.ExecuteReader(CommandBehavior.KeyInfo);

You can use SqlDataReader.GetSchemaTable to get table information but you have to set CommandBehavior to KeyInfo, you do that in the ExecuteReader call:

reader = cmd.ExecuteReader(CommandBehavior.KeyInfo);
强辩 2024-09-13 21:48:41

stackoverflow 上的这个未回答的问题使用 SqlDataReader.GetSchemaTable 来获取表名称。他们的问题是它返回实际的表名而不是表的别名。不确定这是否适用于您的 sql,但我想我会让您知道以防万一。

This unanswered question on stackoverflow uses SqlDataReader.GetSchemaTable to get the table name. Their problem is that it returns the actual table name rather than the alias that the table has. Not sure if this works with your sql but figured I'd let you know just in case.

谜兔 2024-09-13 21:48:41

我不知道这个信息是否可用。特别是,并非结果集的所有列都来自表。从关系的角度来看,表和结果集是同一件事。

I don't know if this information is available. In particular, not all columns of a result set come from a table. From a relational point of view, tables and resultsets are the same thing.

笑忘罢 2024-09-13 21:48:41
reader = cmd.ExecuteReader();
reader.GetSchemaTable().Rows[0]["BaseTableName"];
reader = cmd.ExecuteReader();
reader.GetSchemaTable().Rows[0]["BaseTableName"];
人心善变 2024-09-13 21:48:41

一般来说,这是不可能的。考虑以下查询:

SELECT col1 FROM table1
UNION ALL
SELECT col1 FROM table2

显然 col1 来自多个表。

In general, this is not possible. Consider the following query:

SELECT col1 FROM table1
UNION ALL
SELECT col1 FROM table2

Clearly col1 comes from more than one table.

少年亿悲伤 2024-09-13 21:48:41

你可以像下面这样解决它:

DataTable schemaTable = sqlReader.GetSchemaTable();

foreach (DataRow row in schemaTable.Rows)
{
    foreach (DataColumn column in schemaTable.Columns)
    {
        MessageBox.Show (string.Format("{0} = {1}", column.ColumnName, row[column]));
    }
}

you can solve it like the following :

DataTable schemaTable = sqlReader.GetSchemaTable();

foreach (DataRow row in schemaTable.Rows)
{
    foreach (DataColumn column in schemaTable.Columns)
    {
        MessageBox.Show (string.Format("{0} = {1}", column.ColumnName, row[column]));
    }
}
肤浅与狂妄 2024-09-13 21:48:41
SqlCeConnection conn = new SqlCeConnection("Data Source = Database1.sdf");
SqlCeCommand query = conn.CreateCommand();
query.CommandText = "myTableName";
query.CommandType = CommandType.TableDirect;
conn.Open();
SqlCeDataReader myreader = query.ExecuteReader(CommandBehavior.KeyInfo);
DataTable myDataTable= myreader.GetSchemaTable();
//thats the code you asked. in the loop
for (int i = 0; i < myDataTable.Rows.Count; i++)
{
    listView1.Columns.Add(myDataTable.Rows[i][0].ToString());
}
SqlCeConnection conn = new SqlCeConnection("Data Source = Database1.sdf");
SqlCeCommand query = conn.CreateCommand();
query.CommandText = "myTableName";
query.CommandType = CommandType.TableDirect;
conn.Open();
SqlCeDataReader myreader = query.ExecuteReader(CommandBehavior.KeyInfo);
DataTable myDataTable= myreader.GetSchemaTable();
//thats the code you asked. in the loop
for (int i = 0; i < myDataTable.Rows.Count; i++)
{
    listView1.Columns.Add(myDataTable.Rows[i][0].ToString());
}

如何获取数据库名、表名&列名称。
也可以获得架构名称。使用 MS SQL 2016 进行测试

必须指明 CommandBehavior.KeyInfo

        SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.KeyInfo);

        DataTable dataTable = sqlDataReader.GetSchemaTable();

        for (int i = 0; i < dataTable.Rows.Count - 1; i++)
        {
            string ii =
                dataTable.Rows[i]["BaseCatalogName"].ToString() + "\\" +
                dataTable.Rows[i]["BaseTableName"].ToString() + "\\" + 
                dataTable.Rows[i]["ColumnName"].ToString();
        }

How to get database name, table name & column name.
Also possible to get Schema name as well. Tested with MS SQL 2016

CommandBehavior.KeyInfo must be indicated

        SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.KeyInfo);

        DataTable dataTable = sqlDataReader.GetSchemaTable();

        for (int i = 0; i < dataTable.Rows.Count - 1; i++)
        {
            string ii =
                dataTable.Rows[i]["BaseCatalogName"].ToString() + "\\" +
                dataTable.Rows[i]["BaseTableName"].ToString() + "\\" + 
                dataTable.Rows[i]["ColumnName"].ToString();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文