如何使DataSet从存储过程中识别表名? [英] How to get the DataSet to recognize the table names from stored procedure?

查看:196
本文介绍了如何使DataSet从存储过程中识别表名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程,该过程返回大约5个表,并且正在向它们返回如下内容:

I have a stored procedures which returns about 5 tables and I am returning them something like this:

select <fields> from Products where ProductId = @ProductId
select <fields> from RelatedProducts where ProductId = @ProductId
select <fields> from MetaData where ProductId = @ProductId

嗯,您就明白了。现在在数据集中,如果我这样做:

well, you get the gist. Now in the Dataset, If I do this:

DataSet ProductDs = DAL.RetreiveProductMetadata(someInteger);

ProductName = DataSet.Tables["Products"].Rows[0]["columnName"].ToString();

它抛出了一个异常...好吧,错误说TableName为null,并且在调试时发现,表名实际上被命名为 Results1, Results2,依此类推。

Its throwing up an exception... well, error saying TableName is null and upon debugging, turns out, the table names are actually named as "Results1", "Results2" and so on.

我尝试将存储的proc更改为:

I tried changing stored proc to:

select * From products AS Products

没效果。

如何从数据库中获取C#来检测和使用表名?

How can I get C# to detect and use the Table Names from the database?

ps :我在该领域有各种各样的合并案例,这不应该是一个问题吗?

另外,尝试使用谷歌搜索和敲打头-都行不通。

ps: I have all sorts of Coalesces and cases in the field, that shouldn't present a problem right?
Also, tried googling and banging head - neither worked.

推荐答案

您不能。表名在结果集中没有意义,因为查询可以包含许多表。

You can't. The table name has no meaning in the result set because a query can contain many tables.

您应该知道什么是结果集,而不应该是

You should know what your resultsets are and should not have to derive table names.

否则,

select 'Products' AS ThisTable, <fields> from Products where ProductId = @ProductId

或先加入,然后取消选择

Or JOIN first and unpick later

或预先定义您的数据集。xsd等并根据地图进行映射。

Or define your dataset.xsd etc up front and map according.

这篇关于如何使DataSet从存储过程中识别表名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆