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
调用下面的存储过程,该参数是一个整数。当我运行代码时发生错误并说"Procedure or function 'GetIslemIdleri' expects parameter '@CARIID', which was not supplied."
但是从我从here读取的示例中我理解的是,我正在使用此代码发送参数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
答案 0 :(得分:3)
如果要使用SqlCommand调用存储过程,请不要执行EXEC GetIslemIdleri
,仅执行GetIslemIdleri
,将CommandType设置为CommandType.StoredProcedure
:
cmd = new SqlCommand("GetIslemIdleri", sqlConn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@CARIID", 110));
using (var reader = cmd.ExecuteReader()) //error occurs here
{
while (reader.Read())
{
islemidleri.Add(reader.GetInt32(0));
}
}
答案 1 :(得分:2)
您需要确保您的SqlCommand设置为CommandType.StoredProcedure。
cmd.CommandType = CommandType.StoredProcedure
答案 2 :(得分:2)
你忘了添加程序名称:
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetIslemIdleri";
cmd.Parameters.Add("@CARIID", SqlDBType.Int).Value = 110;
做程序为:
CREATE PROCEDURE [dbo].[GetIslemIdleri]
(
@CARIID int
)
AS
BEGIN
SET NOCOUNT ON;
SELECT ID FROM TBLP1ISLEM WHERE TBLP1ISLEM.CARI_ID = @CARIID
END
这必须有效。
答案 3 :(得分:0)
设置参数有点不同:
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CARIID", SqlDBType.Int).Value = 110;
答案 4 :(得分:0)
您并没有正确调用存储过程。您只需要传递存储过程的名称(不带exec),将命令类型设置为存储过程,然后添加参数:
var command = new SqlCommand("GetIslemIdleri", conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@CARIID", 110));