2013-12-13 43 views
-1

我現在已經刪除了我的代碼,用它替換它。但是現在剩下的代碼不會工作,有人可以幫忙嗎? 我想創建一個使用ComboBox作爲用戶名的登錄系統,但是由於我改變了它,我只是得到了錯誤。最新的錯誤是:*無效語法,其中「'」WinForm - SQL查詢語法不正確

完整的錯誤信息:System.Data.SqlClient.SqlException - {"Incorrect syntax near '.'."}

using System; 
using System.Data; 
using System.Windows.Forms; 
using System.Data.SqlClient; 
using BarcodeForm.Properties; 



private void Button1Click(object sender, EventArgs e) 
{ 
    var dt = new DataTable(); 
    const string Connectionstring = "Data Source=GARETH-PC1;Initial Catalog=Genres;Integrated Security=True"; 
    var query = "Select Username From Login Where Username [email protected] AND [email protected]"; 
    using (var con = new SqlConnection(Connectionstring)) 
    using (var cmd = new SqlCommand(query, con) 
    { 
     con.Open(); 
     cmd.Parameters.AddWithValue("@uname", ComboBox1.SelectedText); 
     cmd.Parameters.AddWithValue("@pwd", textBox2.Text); 
     using (var sda = new SqlDataAdapter(cmd)) 
     { 
      sda.Fill(dt); 
     } 
    } 

    if (dt.Rows.Count > 0) 
    { 
     Hide(); 

     var ss = new Main(); 
     ss.Show(); 
    } 
    else 
    { 
     MessageBox.Show("Invalid Username or Password"); 
    } 

    private void Form1Load(object sender, EventArgs e) 
    { 
     loginTableAdapter.Fill(genresDataSet2.Login); 
    } 
} 

}

+1

你能發佈ComboBox1.SelectedText和textBox2.Text的內容嗎? – Max

+0

或者只是類似的東西,特別是如果它包含像(',「,...)等特殊字符... 您確定參數是正確的類型。您的代碼看起來是正確的... – GalaxyNetworks

+1

您的三個問題似乎都是相同的問題。在詢問下一個問題之前,請嘗試解決一個問題。 – LarsTech

回答

0

林不知道這是否是問題,但會不會是間距?

用戶名= @ UNAME和密碼= @密碼

用戶名= @uname和密碼= @pwd

SQL服務器犯規讓沒有空格所以可能是您的問題