[英]How to get the DataSet to recognize the table names from stored procedure?
我有一個存儲過程,該過程返回大約5個表,並且正在將它們返回如下:
select <fields> from Products where ProductId = @ProductId
select <fields> from RelatedProducts where ProductId = @ProductId
select <fields> from MetaData where ProductId = @ProductId
好吧,你明白了。 現在在數據集中,如果我這樣做:
DataSet ProductDs = DAL.RetreiveProductMetadata(someInteger);
ProductName = DataSet.Tables["Products"].Rows[0]["columnName"].ToString();
它引發了一個異常……好吧,錯誤提示TableName為空,並且在調試后,結果證明,表名實際上被命名為“ Results1”,“ Results2”,依此類推。
我嘗試將存儲過程更改為:
select * From products AS Products
沒有效果。
如何獲得C#從數據庫中檢測和使用表名?
ps:我在該領域有各種各樣的聯合案例,這不應該引起問題嗎?
另外,嘗試使用谷歌搜索和敲打頭-均無效。
實際上有可能,您只需要首先加載表方案,該方案不包含任何數據,但包含有關表結構的信息,包括表名稱:
adapter.FillSchema(dataset, SchemaType.Source);
adapter.Fill(dataset);
似乎這不適用於數據庫中包含的查詢(例如您可能在MS Access中使用的查詢),但是它可以處理常規表。
你不能 表名在結果集中沒有意義,因為查詢可以包含許多表。
您應該知道結果集是什么,而不必導出表名。
除此以外,
select 'Products' AS ThisTable, <fields> from Products where ProductId = @ProductId
或先加入然后再取消選擇
或預先定義您的dataset.xsd等並根據其進行映射。
從數據集中按名稱檢索表的唯一方法是:如果從適配器填充時為表命名,或者以后一次手動為它們命名一個表:
adapter.fill(dataset, "nameoftable")
現在,將來您訪問ds時,可以按名稱訪問; ds.tables("nameoftable").rows
等。
或稍后再命名。
_ds.tables(0).tablename = "nameoftable"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.