2017-08-03 195 views
-1

我有一個運行時錯誤: 必須聲明標量變量\「@經理ID 我敢肯定,我有聲明所有的變量在我的班級和我的Procudure 我的類代碼:必須聲明標量變量「@ManagerID」

public DataTable Select(int ID,string NameFa, string Address, int ManagerID, short TotalUnits, int ChargeMethodID) 
{ 
    DataTable table = new DataTable(); 
    table.Columns.Add("ID", typeof(int)); 
    table.Columns.Add("NameFa", typeof(string)); 
    table.Columns.Add("Address", typeof(string)); 
    table.Columns.Add("ManagerID", typeof(int)); 
    table.Columns.Add("TotalUnits", typeof(short)); 
    table.Columns.Add("ChargeMethodID", typeof(int)); 

    try 
    { 
     con.Open(); 
     SqlCommand command = new SqlCommand("dbo.SelectBuilding", con); 
     command.CommandType = CommandType.StoredProcedure; 
     command.Parameters.Add(new SqlParameter("@ID", ID)); 
     command.Parameters.Add(new SqlParameter("@NameFa", NameFa)); 
     command.Parameters.Add(new SqlParameter("@Address", Address)); 
     command.Parameters.Add(new SqlParameter("@ManagerID", ManagerID)); 
     command.Parameters.Add(new SqlParameter("@TotalUnits", TotalUnits)); 
     command.Parameters.Add(new SqlParameter("@ChargeMethodID", ChargeMethodID)); 
     SqlDataAdapter adapter = new SqlDataAdapter(command); 
     adapter.Fill(table); 
     return table; 
    } 

而且我Procudure代碼是:

@ID int, 
@NameFa nvarchar(150), 
@Address nvarchar(MAX), 
@ManagerID int, 
@TotalUnits smallint, 
@ChargeMethodID int 
As 
    Begin 
     IF(@ID >0) 
      Begin 
       Select ID,NameFa,Address,ManagerID,TotalUnits,ChargeMethodID From Buildings where ID = @ID 
      End 
     ELSE 
      Begin 
       Declare @sqlTxt nvarchar(MAX) 
       SET @sqlTxt = 'SELECT ID,NameFa,Address,ManagerID,TotalUnits,ChargeMethodID FROM Buildings where ID>0' 
       IF(@NameFa!= null) 
       BEGIN 
        SET @sqlTxt = @sqlTxt + ' AND NameFa Like ''%@NameFa%''' 
       END 
       IF(@Address!= null) 
       BEGIN 
        SET @sqlTxt = @sqlTxt + ' AND Address Like ''%@Address%''' 
       END 
       IF(@ManagerID > 0) 
       BEGIN 
        SET @sqlTxt = @sqlTxt + ' AND ManagerID = @ManagerID' 
       END 
       IF(@TotalUnits > 0) 
       BEGIN 
        SET @sqlTxt = @sqlTxt + ' AND TotalUnits = @TotalUnits' 
       END 
       IF(@ChargeMethodID > 0) 
       BEGIN 
        SET @sqlTxt = @sqlTxt + ' AND ChargeMethodID = @ChargeMethodID' 
       END 
       EXEC (@sqlTxt); 
      End 
    END 

,我想使用選擇功能:

DataTable dt = new DataTable(); 
    Buildings.Building bb = new Buildings.Building() {ID=0,NameFa="",Address="",ManagerID=OwnerID,TotalUnits=0,ChargeMethodID=0 }; 
    dt = bu.Select(bb.ID,bb.NameFa,bb.Address,bb.ManagerID,bb.TotalUnits,bb.ChargeMethodID); 

和幫助解決這個問題? ( 它看起來像你的文章主要是代碼;請添加一些更多的細節)。

+0

什麼OWNERID在運行時的價值? – user2657943

+1

'看起來你的文章主要是代碼;請添加一些更多的細節。 –

+0

與這個價值建築物我在Sqlserver中測試但它不在Procudure運行 –

回答

1

您沒有將參數傳遞給exec語句。我會將其更改爲sp_executesql,它具有帶參數的可選參數。

https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-executesql-transact-sql

編輯:我強烈建議擺脫exec和/或sp_executesql命令。因爲取決於輸入,您可以:

a)由於用戶將SQL字符串分隔符鍵入爲有效輸入而獲取運行時錯誤。例如奧哈拉作爲姓氏。

b)惡意用戶可能會混淆你的數據庫。

你可以在一個更簡單的方式獲得類似的結果:

Select 
    ID,NameFa,Address,ManagerID,TotalUnits,ChargeMethodID 
From 
    Buildings 
where 
    (@Id = 0 or ID = @Id) 
    and (@NameFa = '' or NameFa = @NameFa) 
    and (@ManagerID = 0 or ManagerID = @ManagerID) 
    // repeat for the rest of the optional search conditions