[英]System.Data.SqlClient.SqlException: 'Incorrect syntax near 'tbl_LoginInfo (specific to my code)
need some help im completely new to the database , i have made a server and a database now im trying to connect it with my c# code so that it verifies from the database before giving access to the main form, its giving this error : System.Data.SqlClient.SqlException: 'Incorrect syntax near 'tbl_LoginInfo'.'需要一些帮助,我对数据库完全陌生,我已经制作了一个服务器和一个数据库,现在我正在尝试将它与我的 c# 代码连接,以便它在访问主表单之前从数据库中进行验证,它给出了这个错误:System. Data.SqlClient.SqlException: 'tbl_LoginInfo' 附近的语法不正确。
my code:我的代码:
private void Login_Button_Click(object sender, EventArgs e)
{
SqlConnection newconnect = new SqlConnection(@"Data Source=DESKTOP-3DH5S38\HR_SERVER;Initial Catalog=BMS_PRO_DB;Integrated Security=True");
string query = "Select * tbl_LoginInfo where Username = '" + UserName_Textbox.Text.Trim()+ "'and Password = "+Password_Textbox.Text.Trim();
SqlDataAdapter dataAdapter = new SqlDataAdapter(query, newconnect);
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);
if (dataTable.Rows.Count==1)
{
this.Hide();
MainForm mainForm = new MainForm();
mainForm.ShowDialog();
this.Close();
}
else
{
MessageBox.Show("Check Username/Password !");
}
}
I change my code to this now it works perfectly:我将我的代码更改为现在它可以完美运行:
SqlConnection connection = new SqlConnection("server=DESKTOP-3DH5S38\\HR_SERVER;database=BMS_PRO_DB;Integrated Security =true");
SqlCommand sqlCommand = new SqlCommand("Select * From tbl_LoginInfo where username=@user and password=@password", connection);
connection.Open();
sqlCommand.Parameters.AddWithValue("@user", UserName_Textbox.Text);
sqlCommand.Parameters.AddWithValue("@password", Password_Textbox.Text);
SqlDataReader dataReader = sqlCommand.ExecuteReader();
if (dataReader.HasRows == true)
{
this.Hide();
MainForm mainForm = new MainForm();
mainForm.ShowDialog();
this.Close();
}
else
{
MessageBox.Show("Check Username/Password !");
}
I still don't know what was the problem there.我仍然不知道那里出了什么问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.