在c#中将参数传递给sql存储过程

发布于 2024-10-26 13:32:05 字数 927 浏览 4 评论 0原文

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

神经暖 2024-11-02 13:32:05

如果要使用 SqlCommand 调用存储过程,请不要执行 EXEC GetIslemIdleri,只执行 GetIslemIdleri,设置 CommandTypeCommandType.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));
    }
}

If you want to call a stored procedure using a SqlCommand, do not execute EXEC GetIslemIdleri, execute just GetIslemIdleri, setting CommandType to 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));
    }
}
裸钻 2024-11-02 13:32:05

您忘记添加 prodecure 的名称:

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

这必须起作用。

you forgot to add the prodecure`s name:

cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetIslemIdleri";
cmd.Parameters.Add("@CARIID", SqlDBType.Int).Value = 110;

And do procedure as:

CREATE PROCEDURE [dbo].[GetIslemIdleri] 
(
    @CARIID int 
)
AS
BEGIN
SET NOCOUNT ON;    
SELECT ID FROM TBLP1ISLEM WHERE TBLP1ISLEM.CARI_ID = @CARIID
END

This has to work.

乖乖兔^ω^ 2024-11-02 13:32:05

您需要确保您的 SqlCommand 设置为 CommandType.StoredProcedure。

cmd.CommandType = CommandType.StoredProcedure

You need to make sure your SqlCommand is set to CommandType.StoredProcedure.

cmd.CommandType = CommandType.StoredProcedure
简美 2024-11-02 13:32:05

设置参数有点不同:

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CARIID", SqlDBType.Int).Value = 110;

Set the parameter a bit differenty:

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CARIID", SqlDBType.Int).Value = 110;
隔岸观火 2024-11-02 13:32:05

您没有完全正确调用存储过程。您只需传递存储过程的名称(不带exec),将命令类型设置为存储过程,然后添加参数:

var command = new SqlCommand("GetIslemIdleri", conn);
command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add(new SqlParameter("@CARIID", 110));

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:

var command = new SqlCommand("GetIslemIdleri", conn);
command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add(new SqlParameter("@CARIID", 110));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文