过程或函数期望参数'@id'没有提供

本文关键字:@id 参数 函数 过程 期望 | 更新日期: 2023-09-27 18:07:55

我错过了什么?

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',但没有提供。

过程或函数期望参数'@id'没有提供

尝试添加

cmd.CommandType = CommandType.StoredProcedure

将存储过程作为语句执行,而不是作为存储过程执行。这就好像你在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 ;
}
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