在c#中将参数传递给sql存储过程
string commandGetIslemIdleri = ("EXEC GetIslemIdleri");
cmd = new SqlCommand(commandGetIslemIdleri, sqlConn);
cmd.Parameters.Add(new SqlParameter("@CARIID", 110));
using (var reader = cmd.ExecuteReader()) //error occurs here
{
while (reader.Read())
{
islemidleri.Add(reader.GetInt32(0));
}
}
上面是我试图编写的代码,用于使用整数参数 CARIID
调用下面的存储过程。当我运行代码时,出现错误并显示“过程或函数'GetIslemIdleri'需要参数'@CARIID',但未提供该参数。”
但据我从此处阅读的示例中了解,我是使用此代码发送参数 cmd.Parameters.Add(new SqlParameter("@CARIID", 110));
我需要帮助,提前谢谢您。
ALTER PROCEDURE [dbo].[GetIslemIdleri]
@CARIID int
AS
BEGIN
SET NOCOUNT ON;
SELECT ID
FROM TBLP1ISLEM
WHERE TBLP1ISLEM.CARI_ID=@CARIID
END
string commandGetIslemIdleri = ("EXEC GetIslemIdleri");
cmd = new SqlCommand(commandGetIslemIdleri, sqlConn);
cmd.Parameters.Add(new SqlParameter("@CARIID", 110));
using (var reader = cmd.ExecuteReader()) //error occurs here
{
while (reader.Read())
{
islemidleri.Add(reader.GetInt32(0));
}
}
Above is the code i am trying to write to call the below stored procedure with a parameter CARIID
which is an integer. when i run the code an error occurs and says "Procedure or function 'GetIslemIdleri' expects parameter '@CARIID', which was not supplied."
but as much as i understand from the examples i read from here i am sending the parameter with this code cmd.Parameters.Add(new SqlParameter("@CARIID", 110));
i need help, thank you in advance.
ALTER PROCEDURE [dbo].[GetIslemIdleri]
@CARIID int
AS
BEGIN
SET NOCOUNT ON;
SELECT ID
FROM TBLP1ISLEM
WHERE TBLP1ISLEM.CARI_ID=@CARIID
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果要使用 SqlCommand 调用存储过程,请不要执行
EXEC GetIslemIdleri
,只执行GetIslemIdleri
,设置 CommandType 到CommandType.StoredProcedure
:If you want to call a stored procedure using a SqlCommand, do not execute
EXEC GetIslemIdleri
, execute justGetIslemIdleri
, setting CommandType toCommandType.StoredProcedure
:您忘记添加 prodecure 的名称:
并执行以下操作:
这必须起作用。
you forgot to add the prodecure`s name:
And do procedure as:
This has to work.
您需要确保您的 SqlCommand 设置为 CommandType.StoredProcedure。
You need to make sure your SqlCommand is set to CommandType.StoredProcedure.
设置参数有点不同:
Set the parameter a bit differenty:
您没有完全正确调用存储过程。您只需传递存储过程的名称(不带exec),将命令类型设置为存储过程,然后添加参数:
You're not quite calling the stored procedure properly. You only need to pass the name of the stored procedure (without exec), set the command type to stored procedure, then add the parameters: