简体   繁体   English

如何从SqlDataReader获取列的表名

[英]How to get table name of a column from SqlDataReader

I have an SQL query I get from a configuration file, this query usually contains 3-6 joins. 我从配置文件中获得了一个SQL查询,此查询通常包含3-6个连接。

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. 我需要在运行时根据SqlDataReader表示的结果集找到每个列的表名。

Here are some thing that don't work: 以下是一些不起作用的东西:

  • SqlDataReader.GetName returns the column name but not the table name. SqlDataReader.GetName返回列名但不返回表名。
  • SqlDataReader.GetSchemaTable returns a data table with column information - but all the table names are null. SqlDataReader.GetSchemaTable返回包含列信息的数据表 - 但所有表名都为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). 查询information_schema没有帮助,因为我需要有关当前查询结果的数据(并且列名不是唯一的 - 在不同的表中有相同名称的列)。

I'm using .net 3.5SP1/ C#/ SQL Server 2008 in a console application. 我在控制台应用程序中使用.net 3.5SP1 / C#/ SQL Server 2008。

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: 编辑2:找出它无法工作的原因 - 您可以使用SqlDataReader.GetSchemaTable获取表信息,但是您必须将CommandBehavior设置为KeyInfo,您可以在ExecuteReader调用中执行此操作:

reader = cmd.ExecuteReader(CommandBehavior.KeyInfo);

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

reader = cmd.ExecuteReader(CommandBehavior.KeyInfo);

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. 从关系的角度来看,表和结果集是相同的。

This unanswered question on stackoverflow uses SqlDataReader.GetSchemaTable to get the table name. stackoverflow上这个未解决的问题使用SqlDataReader.GetSchemaTable来获取表名。 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. 不确定这是否适用于您的sql,但我想我会告诉您以防万一。

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]));
    }
}

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. 很明显,col1来自多个表。

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());
}
reader = cmd.ExecuteReader();
reader.GetSchemaTable().Rows[0]["BaseTableName"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM