2017-08-03 197 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