在C#调用存储过程.

hdhzmx 2006-02-27 11:57:53
这是一个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

堆栈跟踪:

...全文
157 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangjidong 2006-02-27
  • 打赏
  • 举报
回复
把存储过程改一下,因为存储过程没有输出参数列表,vs不识别。
===============================
create proc spFu (@myresult [decimal] output)
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
Kshatriya 2006-02-27
  • 打赏
  • 举报
回复
create proc spFu
(
@fu int out
)
AS
@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
go
Kshatriya 2006-02-27
  • 打赏
  • 举报
回复
存储过程有问题,应用加入返回参数
hdhzmx 2006-02-27
  • 打赏
  • 举报
回复
问题解决谢谢各位

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧