如何从SqlDataReader获取列的表名
我有一个从配置文件中获取的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用 SqlDataReader.GetSchemaTable 获取表信息,但必须将 CommandBehavior 设置为 KeyInfo,您可以在 ExecuteReader 调用中执行此操作:
You can use SqlDataReader.GetSchemaTable to get table information but you have to set CommandBehavior to KeyInfo, you do that in the ExecuteReader call:
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.
我不知道这个信息是否可用。特别是,并非结果集的所有列都来自表。从关系的角度来看,表和结果集是同一件事。
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.
一般来说,这是不可能的。考虑以下查询:
显然 col1 来自多个表。
In general, this is not possible. Consider the following query:
Clearly col1 comes from more than one table.
你可以像下面这样解决它:
you can solve it like the following :
如何获取数据库名、表名&列名称。
也可以获得架构名称。使用 MS SQL 2016 进行测试
必须指明
CommandBehavior.KeyInfo
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