2014-05-07 246 views
0

我不断收到,我不明白的错误:必须声明标量变量“@user”

必须声明标量变量“@user”

我实际上做一个网站,我正在使用C#ASP.NET和SQL Server。我有一个班级名称Connection,另一个名为Query。这里是问题

public class Query 
{ 
    public int ValidateLogin(string userID, string password) 
    { 
     string query = "Select * From tblLogin where UserID = @user and Password = @paswd"; 

     Connection objConn = new Connection(); 

     DataTable dtLogin = objConn.GetDataFromDB(query); 

     int result = 0; 

     if (dtLogin.Rows.Count > 0) 
     { 
      result = 1; 
     } 

     return result; 
} 

public class Connection 
{ 
    string conn = ConfigurationManager.ConnectionStrings["DBConn"].ToString(); 

    public DataTable GetDataFromDB(string query) 
    { 
     SqlConnection myConn = new SqlConnection(conn); 
     myConn.Open(); 

     SqlDataAdapter da = new SqlDataAdapter(); 
     da.SelectCommand = myConn.CreateCommand(); 
     da.SelectCommand.CommandText = query; 

     DataTable dt = new DataTable(); 

     da.Fill(dt); 

     da.Dispose(); 
     myConn.Close(); 

     return dt; 
} 

protected void Page_Load(object sender, EventArgs e) 
{ 
} 

protected void btn_login_Click(object sender, EventArgs e) 
{ 
     int result = 0; 
     Query q = new Query(); 
     result = q.ValidateLogin(txt_userID.Text, txt_password.Text); 

     if (result == 1) 
     { 
      Response.Redirect("~/Performance Appraisal Form.aspx"); 
     } 
     else 
     { 
     } 
} 
+7

您要使用的参数与你的查询,但你是不是将它们连接到命令的任何地方 – Habib

+1

@Habib后它的答案。 –

回答

1

将您的参数添加到SelectCommand。

例子:

private static void UpdateDemographics(Int32 customerID, 
    string demoXml, string connectionString) 
{ 
    // Update the demographics for a store, which is stored 
    // in an xml column. 
    string commandText = "UPDATE Sales.Store SET Demographics = @demographics " 
     + "WHERE CustomerID = @ID;"; 

    using (SqlConnection connection = new SqlConnection(connectionString)) 
    { 
     SqlCommand command = new SqlCommand(commandText, connection); 
     command.Parameters.Add("@ID", SqlDbType.Int); 
     command.Parameters["@ID"].Value = customerID; 

     // Use AddWithValue to assign Demographics. 
     // SQL Server will implicitly convert strings into XML. 
     command.Parameters.AddWithValue("@demographics", demoXml); 

     try 
     { 
      connection.Open(); 
      Int32 rowsAffected = command.ExecuteNonQuery(); 
      Console.WriteLine("RowsAffected: {0}", rowsAffected); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
    } 
} 

您的代码

要做到这一点的另一个参数,以此来

public DataTable GetDataFromDB(string query) 

改变其添加到

public DataTable GetDataFromDB(string query, string[] params) //example, can be another type of collection like ParameterCollection. 


da.SelectCommand.parameters.add("@user",params[0]); 
da.SelectCommand.parameters.add("@paswd",params[1]); 

参数传递给您的方法。

string[] Params= new string[2]; 
    Params[0] = txt_userID.Text; 
    Params[1] =txt_password.Text; 


DataTable dtLogin = objConn.GetDataFromDB(query,Params); 

参考:

http://msdn.microsoft.com/es-es/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx

-2

您正在使用内联SQL,因此需要有效的SQL字符串。 @user不是SQL代码中声明的变量,因此不会被检测到。要么在您的SQL字符串中包含声明语句,要么在您的字符串中注入“user”的实际值。

希望这有助于..

(呵呵,作为一个方面说明..请不要使用内联SQL,这是非常不安全的。切换到使用存储过程至少是...)

+3

内联SQL如何“非常不安全”?对于单行SELECT使用存储过程似乎对我来说过分了。 –

+0

您可以使用内联Sql,但总是使用参数。它会帮助你避免sql注入。 http://stackoverflow.com/questions/6547986/how-to-prevent-a-sql-injection-escaping-strings –

+0

在行SQL不是不安全的 - 你怎么做出这个断言?只要你没有使用字符串连接来生成你的查询,那绝对没问题。您认为Entity Framework或LINQ to SQL与SQL Server通信还有什么意义? –