在C#调用存储过程.
这是一个LtnData类中的方法
public int spProc(string spProc)
{
ConnOpen();
// SqlCommand1=new SqlCommand("spModifyPayCheque",SqlConnection1);
SqlCommand1=new SqlCommand(spProc,SqlConnection1);
SqlCommand1.CommandType=CommandType.StoredProcedure;
SqlParameter Param1=new SqlParameter("@Result",SqlDbType.Int);
SqlCommand1.Parameters.Add( Param1);
Param1.Direction=ParameterDirection.Output;
SqlCommand1.ExecuteNonQuery();
return System.Convert.ToInt32(Param1.Value.ToString());
}
在另一个页面调用这个类的spProc方法
protected LtnOA.Classes.LtnData Result=new LtnOA.Classes.LtnData();
private void Page_Load(object sender, System.EventArgs e)
{
string fu="spFu";
int Resultfu;
Resultfu=Result.spProc(fu);
LabFu.Text=Resultfu.ToString();
}
存储过程内容如下
create proc spFu AS
Declare @fu int,@FristDay datetime,@LastDay datetime
select @FristDay=DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) --取当月的第一天
select @LastDay=DATEADD(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))--取当月的最后一天
select @fu=sum(money) from payinvoice
where Datetime between @FristDay and @LastDay
and Cancellation='0'
return @fu
go
在Sql运行spFu
declare @res int
execute @res=spFu
select @res
可以得到结果
在C#报错的内容是:
过程 spFu 没有参数,但却为该过程提供了参数。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Data.SqlClient.SqlException: 过程 spFu 没有参数,但却为该过程提供了参数。
源错误:
行 1139:
行 1140: Param1.Direction=ParameterDirection.Output;
行 1141: SqlCommand1.ExecuteNonQuery();
行 1142:
行 1143: return System.Convert.ToInt32(Param1.Value.ToString());
源文件: e:\aspnet\ltnoa\classes\ltndata.cs 行: 1141
堆栈跟踪: