我们写程序的,难免要和数据库进行打交道,存储过程的参数也是在编程中经常要用到的.
但在存储过程参数中,如果有 int 型的参数,我一般习惯于这样写:
create PROCEDURE [dbo].[usp_jf_stat_log_jifen]
(
@timebegin datetime,
@timeend datetime,
@stat int -- 0,所有 1,添加积分 2,消费积分
)
AS
begin
select * from table1
end
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在客户端调用时代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
SqlParameter[] prms = new SqlParameter[]
{
new SqlParameter("@timebegin",this.tbTimeBegin.Text.Trim()),
new SqlParameter("@timeend",this.tbTimeEnd.Text.Trim()),
new SqlParameter("@stat",0) //Convert.ToInt32(this.ddlStyle.SelectedValue))
};
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DataSet ds = DbHelper.ExecuteDataset(DbHelper.ConnectionString, CommandType.StoredProcedure, "usp_jf_stat_log_jifen", prms);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
注意红色的"0",这时,这个参数,到底是DBNull.Value 呢?还是 (int32)0 呢? 我们打开SQL Server Profiler,发现执行的如下的TSQL语句:
exec usp_jf_stat_log_jifen @timebegin=N'2007-05-22',@timeend=N'2007-06-22',@stat=NULL
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
是NULL,不是0. 真是一个臭虫.
改成这样如何呢?
new SqlParameter("@stat",0D)
结果还是这样:
exec usp_jf_stat_log_jifen @timebegin=N'2007-05-22',@timeend=N'2007-06-22',@stat=NULL
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
最后改成这样:
new SqlParameter("@stat",Convert.ToInt32(0))
最后结果:
exec usp_jf_stat_log_jifen @timebegin=N'2007-05-22',@timeend=N'2007-06-22',@stat=0
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
嘻嘻,终于成功了.
本文章解决的只是一个小问题!为一个朋友解释什么是调程序,怎么调程序而写!
哈哈!