Skip to main content
 首页 » 编程设计

c#之如何测试 SQL Server 中的存储过程错误

2024年02月05日10artech

我有一个在 SQL Server 中运行良好的存储过程。

我在 C# 中这样调用它(r 是第一行):

DateTime beginDate = new DateTime(2016, 5, 1); 
DateTime? endDate = new DateTime(2016, 5, 31); 
 
this.getDailySalesTableAdapter.Fill(myDataSet.getDailySales, beginDate, endDate); 
MessageBox.Show(rMSDataSet.getDailySales.Count.ToString()) ;  //SHOWS 8549 ROWS 
 
MYDataSet.getDailySalesRow r = myDataSet.getDailySales.First(); 
string mb = ""; 
 
// error here---->>>             
foreach (DataColumn dc in r.ItemArray) 
{ 
    if (r[dc] != null) // This will check the null values also  
    { 
        mb = mb + r[dc] + " "; 
    } 
 
    MessageBox.Show(mb); 
} 

它编译正常,但我在 DC 上遇到的运行时错误是

Unable to cast object of type 'System.String' to type 'System.Data.DataColumn'

在 SQL Server 中,我的存储过程开始:

ALTER PROCEDURE [dbo].[getDailySales]  
    @begindate date,  
    @enddate date  

请您参考如下方法:

您需要使用 DataColumn.ColumnName属性:

foreach (DataColumn dc in r.ItemArray) 
{ 
   if (r[dc] != null) // This will check the null values also  
   { 
       mb = mb + r[dc.ColumnName] + " ";  //Updated 
   } 
   MessageBox.Show(mb); 
}