2013-10-30 2 views
1

ASP.NET 및 SQL Server 데이터베이스와 Visual Studio 2010에 대한 질문이 있습니다.이 문제는 로그인 컨트롤 CS 부분에 대한 것입니다.로그인 컨트롤 asp.net 및 CS

나는, 데이터베이스 내 양식을 연결 DB에서 사람과 로그인과 암호를 비교해야하지만 라인

SqlDataReader rdr = com.ExecuteReader(); 

는 근처의 구문이 "사용자"

그것을인가에 오류가 있음을 말한다 쿼리 문제?

제발 도와 주실 수 있습니까?

private bool UserLogin(string userName, string password) 
{ 
    // read the coonection string from web.config 
    string conString = ConfigurationManager.ConnectionStrings["MidLinData"].ConnectionString; 

    using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(conString)) 
    { 
     con.Open(); 
     //' declare the command that will be used to execute the select statement 
     // SqlCommand com = new SqlCommand("SELECT login FROM user WHERE userName = @login AND Password = @password", con); 
     SqlCommand com = new SqlCommand("SELECT login FROM user WHERE userName = @login ", con); 
     SqlCommand com2 = new SqlCommand("SELECT password FROM user WHERE password = @password ", con); 
     // set the username and password parameters 
     com.Parameters.Add("@login", SqlDbType.NVarChar).Value = userName; 
     SqlDataReader rdr = com.ExecuteReader(); 
     //com.Parameters.Add("@password", SqlDbType.NVarChar).Value = password; 
     string result = rdr[0].ToString(); 

     // execute the select statment 
     // string result = Convert.ToString(com.ExecuteScalar()); 
     //' check the result 
     if (string.IsNullOrEmpty(result)) 
     { 
      //invalid user/password , return flase 
      return false; 
     } 
     else 
     { 
      // valid login 
      return true; 
     } 

     return true; 
    } 
} 

컴파일러는 말한다 :

Server Error in '/visualStudioWebsite' Application.

Incorrect syntax near the keyword 'user'.

Description: An unhandled exception occurred during the execution of the current web request.

Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'user'.

Source Error:

Line 57: // set the username and password parameters
Line 58: com.Parameters.Add("@login", SqlDbType.NVarChar).Value = userName;
Line 59: SqlDataReader rdr = com.ExecuteReader();
Line 60: com.Parameters.Add("@password", SqlDbType.NVarChar).Value = password;
Line 61:

Source File: c:\Users\annasolovjova\Desktop\visualStudioWebsite\login.aspx.cs Line: 59

+0

도움이 될 수 컴파일러에서 오류 메시지를 게시. – vdbuilder

+0

방금 ​​포스트를 편집했습니다. –

답변

0

user는 SQL 서버에서 예약 키워드 - 당신은 가장 좋은 건 이다있어 테이블 이름으로 사용하려면이 아닙니다! 당신 이 그것을 사용하는 방법에을 주장하는 경우, 당신은 괄호에 넣어해야합니다

SqlCommand com = new SqlCommand("SELECT login FROM [user] WHERE userName = @login ", con); 
0

당신은 다음과 같은 두 명령을 결합 할 수 있습니다 :

SqlCommand com = new SqlCommand(); 
com.Connection = con; 
com.CommandText = "SELECT login, password FROM user WHERE userName = @login and password = @password"; 
com.Parameters.AddWithValue("login", userName); 
com.Parameters.AddWithValue("password", password); 
관련 문제