我建立在c#
一個sql
查詢來進行登錄,在此我得到一個語法錯誤結束,但我想不出是什麼原因導致的錯誤ASP.Net登錄:SQL查詢語法錯誤
SqlDataAdapter sda = new SqlDataAdapter("Select * From bruker Where Brukernavn='"
+ Textbox1.Text + "' and Passord='" + TextBox2.Text"");
c#
我建立在c#
一個sql
查詢來進行登錄,在此我得到一個語法錯誤結束,但我想不出是什麼原因導致的錯誤ASP.Net登錄:SQL查詢語法錯誤
SqlDataAdapter sda = new SqlDataAdapter("Select * From bruker Where Brukernavn='"
+ Textbox1.Text + "' and Passord='" + TextBox2.Text"");
你缺少一個"+"
,你需要通過連接字符串或SqlConnection
作爲第二個參數
SqlDataAdapter sda = new SqlDataAdapter("Select * From bruker Where Brukernavn='" + Textbox1.Text + "' and Passord='" + TextBox2.Text +"","connectionString");
或
SqlDataAdapter sda = new SqlDataAdapter("Select * From bruker Where Brukernavn='" + Textbox1.Text + "' and Passord='" + TextBox2.Text +"",con);
由於上述代碼易受sql注入攻擊,請使用下面的代碼來防止sql注入。
try
{
SqlConnection con = new SqlConnection("connectionString");
SqlCommand commnad = new SqlCommand("Select * From bruker Where [email protected] and [email protected]", con);
commnad.Parameters.AddWithValue("@username", Textbox1.Text.Trim);
commnad.Parameters.AddWithValue("@password", Textbox2.Text.Trim);
//rest of the code
}
catch(Exception ex)
{
//log exception and re-throw or send a generic exception message to UI
throw;
}
finally
{
//close the connection
}
如果能解決您的問題,請將其標記爲答案。
請用非注射代碼更新您的答案。 – mxmissile
@mxmissile我不知道你爲什麼投票。把它變成不可注射的不是他的問題。我只是對他的問題提出了質疑。這是不公平的 –
@KranranB如果不是他的問題沒有關係,我們應該鼓勵它,只要我們看到了機會。 – christophano
你在''「之前錯過了'+'' –
另外,要小心SQL注入。參數化您的查詢。 –
這裏是錯誤圖片https://i.gyazo.com/60e8e3fe52f3cfd6f8b187ae82a0deb4.png – darthlars