程序或功能需要参数' @ id'那是不供给的

时间:2014-06-09 21:32:20

标签: c# sql

我错过了什么?

private void GetGeneralData(ReportPackage myPackage)
{
  using (SqlConnection conn = new SqlConnection(mySqlConn))
  {
    using (SqlCommand cmd = new SqlCommand("[dbo].[GetStuff]", conn))
    {
      cmd.Parameters.AddWithValue("@id", myPackage.IdDeliverable);

      conn.Open();
      using (SqlDataReader dr = cmd.ExecuteReader())
      {
        dr.Read();
        myPackage.DeployServer = dr.GetString(0);
        myPackage.Connection = dr.GetString(1);
      }
    }
  }
}
  

过程或函数'GetStuff'需要参数'@id',这是未提供的。

3 个答案:

答案 0 :(得分:8)

尝试添加

cmd.CommandType = CommandType.StoredProcedure

答案 1 :(得分:2)

您正在将存储过程作为语句执行,而不是作为存储过程执行。就好像您在SQL Server Management Studio中打开了一个查询窗口并输入了

[dbo].[GetStuff]
GO

因此,即使存储过程采用参数,由于您正在执行文本语句,因此它无法将参数映射到文本中的任何内容。将命令类型更改为CommandType.StoredProcedure

如果你做一点重构,你可能会发现你的代码更紧凑,因此:

private void GetGeneralData( ReportPackage myPackage )
{
  using ( SqlConnection conn = new SqlConnection(mySqlConn) )
  using ( SqlCommand    cmd  = conn.CreateCommand() )
  {
      cmd.CommandType = CommandType.StoredProcedure ;
      cmd.CommandText = "dbo.GetStuff" ;

      cmd.Parameters.AddWithValue("@id", myPackage.IdDeliverable);

      conn.Open();
      using (SqlDataReader dr = cmd.ExecuteReader())
      {
        dr.Read();
        myPackage.DeployServer = dr.GetString(0);
        myPackage.Connection   = dr.GetString(1);
      }
  }
   return ;
}

答案 2 :(得分:0)

ALTER Procedure [dbo].[spSearch_Stock]
(
    @KeyW varchar(50),
    @Empty int

)
AS
BEGIN
SET @Empty = (SELECT COUNT(Customer) FROM tbl_Stock)

IF @Empty > 0 
    SELECT StockID,Abb ,LotNo ,InvoiceNo ,TeaState,Customer ,Broker ,TeaGrade,Pkgs,NetWeight ,TotWeight ,PriceUSD,CurrencyRate,TotalAmtUSD,BrokerageUSD
    FROM tbl_Stock

    WHERE 
    (Abb) LIKE '%'+@Keyw+'%' 


    ORDER BY 
    StockID ASC,Abb ASC

        ELSE 
        SELECT 'null' as StockID,'null' as Abb ,'null' as LotNo ,'null' as InvoiceNo ,'null' as TeaState,'null' as Customer ,'null' as Broker ,'null' as TeaGrade,'null' as Pkgs,'null' as NetWeight ,'null' as TotWeight ,'null' as PriceUSD,'null' as CurrencyRate,'null' as TotalAmtUSD,'null' as  BrokerageUSD

END