关键字“Table”异常附近的语法不正确

时间:2015-05-22 12:47:05

标签: c# ado.net

我正在尝试创建登录屏幕。当我输入用户名和密码时,我收到了一个例外:

sda.Fill(dt);
  

关键字“表格”

附近的语法不正确

我的代码:

private void button1_Click(object sender, EventArgs e)
{

    this.Hide();

    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\TOSHIBA\Documents\admindb.mdf;Integrated Security=True;Connect Timeout=30;");

    SqlDataAdapter sda = new SqlDataAdapter("Select Count (*) From Table where username ='" + textBox1.Text +"' and password='" + textBox2.Text +"'",con);
    DataTable dt = new DataTable();
    sda.Fill(dt) ;
    if(dt.Rows[0][0].ToString() == "1")
    {

    maincs ss = new maincs();
    ss.Show();
    }
    else
    {
    MessageBox.Show("Nichtig Username oder Password") ;

    }
}

1 个答案:

答案 0 :(得分:1)

如果实际调用您的表Table,则必须将表名括在括号中:

SqlDataAdapter sda = 
  new SqlDataAdapter("Select Count (*) From [Table] where username ='" +
      textBox1.Text +"' and password='" + textBox2.Text +"'",con);

否则,正如评论者所说,您需要将Table替换为Sql中的实际表名。

注意:您收到的例外应该指出您正确的问题。

相关问题