将参数传递给C#中的存储过程

时间:2011-03-24 13:23:06

标签: c# sql sql-server stored-procedures parameter-passing

如何在sql中向存储过程发送三个参数?

这是我的错误:过程或函数GetIslemIdleri指定了太多参数。

这是我的存储过程:

CREATE PROCEDURE GetIslemDetayIdleri 

@islemId int,
@dovizTanim nvarchar(10),
@yapilanIslemTuru nvarchar(20)
AS
BEGIN
    SET NOCOUNT ON;

SELECT ([t0].[TOPLAMTUTAR]) + ([t0].[KDVTUTAR]) AS [value]
FROM [dbo].[TBLP1ISLEMDETAY] AS [t0]
INNER JOIN [dbo].[TBLP1ISLEM] AS [t1] ON [t1].[ID] = [t0].[ISLEM_ID]
WHERE ([t0].[ISLEM_ID] = @islemId) AND 
      ([t0].[FIYATBIRIM] = @dovizTanim) AND 
      ([t1].[YAPILANISLEM] = @yapilanIslemTuru) AND 
      ([t0].[KDVDAHILMI] = 0)
END

这是我的代码:

        decimal kurToplamQuery = 0;
        string connString = System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer1"].ConnectionString;

        SqlConnection sqlConn = new SqlConnection(connString);
        sqlConn.Open();

        SqlCommand cmd;

        cmd = new SqlCommand("GetIslemIdleri", sqlConn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@islemId", SqlDbType.Int)).Value = islemId;
        cmd.Parameters.Add(new SqlParameter("@dovizTanim", SqlDbType.NVarChar)).Value = dovizTanim;
        cmd.Parameters.Add(new SqlParameter("@yapilanIslemTuru", SqlDbType.NVarChar)).Value = yapilanIslemTipi;

        using (var reader = cmd.ExecuteReader())*//error occurs here*
        {
            while (reader.Read())
            {
                kurToplamQuery = reader.GetDecimal(0);                    
            }
        }
        sqlConn.Close();
        return kurToplamQuery;

感谢您的帮助。

4 个答案:

答案 0 :(得分:5)

存储过程称为GetIslemDetayIdleri,但代码使用名为GetIslemIdleri的存储过程。也许后者的参数比前者少,你打算在代码中调用前者?

答案 1 :(得分:3)

GetIslemDetayIdleri!= GetIslemIdleri

答案 2 :(得分:2)

您的存储过程:GetIslemDetayIdleri与您的调用具有不同的名称:

cmd = new SqlCommand("GetIslemIdleri", sqlConn);

答案 3 :(得分:0)

在我的情况下,此代码有效。我希望这对您的情况有效
过程参数名称、类型和长度必须相同 就这样

       cmd.Parameters.Add(new SqlParameter("@islemId", SqlDbType.Int)).Value = islemId;
            cmd.Parameters.Add(new SqlParameter("@dovizTanim", SqlDbType.NVarChar ,10)).Value = dovizTanim;
            cmd.Parameters.Add(new SqlParameter("@yapilanIslemTuru", SqlDbType.NVarChar,20)).Value = yapilanIslemTipi;