SqlCommand myCommand = new SqlCommand("reportdays",Conn); myCommand.CommandType = CommandType.StoredProcedure; SqlParameter wbn = new SqlParameter("@WBN", SqlDbType.VarChar); wbn.Value = WBN; myCommand.Parameters.Add(wbn); SqlParameter DT1 = new SqlParameter("@DT1", SqlDbType.DateTime); DT1.Value = dt1; myCommand.Parameters.Add(DT1); SqlParameter DT2 = new SqlParameter("@DT2", SqlDbType.DateTime); DT2.Value = dt2; myCommand.Parameters.Add(DT2); SqlParameter cnt = new SqlParameter("@CNT", SqlDbType.Int); cnt.Direction=ParameterDirection.Output; myCommand.Parameters.Add(cnt); myCommand.ExecuteNonQuery(); int cnt1=(int)cnt.Value; return cnt1; 存储过程 CREATE PROCEDURE ReportDays ( @WBN varchar, @DT1 Datetime, @DT2 Datetime, @CNT INTEGER OUT ) AS select Distinct @CNT=Count(ReportDate) from DayReport where state<>0 and WasteBookNo=@WBN and ReportDate between @DT1 and @DT2 GO
在没有任何数据时进行无效的读取尝试。源错误: string sql1="select Count(ReportDate) as cnt from DayReport where state<>0 and WasteBookNo='"+WBN+"' and ReportDate between '"+dt1+"' and '"+dt2+"'"; SqlDataReader myReader=mydata1.executeQuery(sql1); int cnt=(int)myReader["cnt"];
直接COPY过来试试
myCommand.CommandType = CommandType.StoredProcedure; SqlParameter wbn = new SqlParameter("@WBN", SqlDbType.VarChar);
wbn.Value = WBN;
myCommand.Parameters.Add(wbn); SqlParameter DT1 = new SqlParameter("@DT1", SqlDbType.DateTime);
DT1.Value = dt1;
myCommand.Parameters.Add(DT1); SqlParameter DT2 = new SqlParameter("@DT2", SqlDbType.DateTime);
DT2.Value = dt2;
myCommand.Parameters.Add(DT2); SqlParameter cnt = new SqlParameter("@CNT", SqlDbType.Int);
cnt.Direction=ParameterDirection.Output;
myCommand.Parameters.Add(cnt); myCommand.ExecuteNonQuery();
int cnt1=(int)cnt.Value;
return cnt1; 存储过程
CREATE PROCEDURE ReportDays
(
@WBN varchar,
@DT1 Datetime,
@DT2 Datetime,
@CNT INTEGER OUT
)
AS
select Distinct @CNT=Count(ReportDate)
from DayReport where state<>0 and WasteBookNo=@WBN and ReportDate between @DT1 and @DT2
GO
string sql1="select Count(ReportDate) as cnt from DayReport where state<>0 and WasteBookNo='"+WBN+"' and ReportDate between '"+dt1+"' and '"+dt2+"'";
SqlDataReader myReader=mydata1.executeQuery(sql1);
int cnt=(int)myReader["cnt"];
程序访问时,使用全名访问:所有者.sp
或许可以看到些问题。