记得以前连接数据库都没问题,这次出啦问题,非常郁闷,高手帮我解决一下吧!   我贴下代码:
string connectionstring = "Data Source=PC-200801010000 \\SQLEXPRESS;Initial Catalog=financemanager;Integrated Security=True";
 SqlConnection conn = new SqlConnection(connectionstring);
conn.open();
 string sql = string.Format("select COUNT(*) from user where username='{0}' and password='{1}'", this.textBox1.Text.Trim(), this.textBox2.Text.Trim());
sqlcommand cmd=new sqlcommand(sql,conn);
int count = (int)cmd.ExecuteScalar();
                conn.Close();
                if (count>0)
                {
                    MainForm mainform = new MainForm();
                    mainform.Show();
                    this.Hide();                }
                else
                {
                    label5.Visible = true;
                    textBox2.Text = "";
                }上面是部分代码,不知道哪里错啦,语法是没错的啊,但是报错   --------》“关键字user附近有语法错误”。。

解决方案 »

  1.   

     [user] where [username]='{0}' and [password]
      

  2.   

    Try user change to [user].
      

  3.   

    以前碰到过类似的问题
    把 user表 改成其他名字 试试
    我是这样解决的 不知道为什么
      

  4.   

    string sql = string.Format("select COUNT(*) from [user] where username='{0}' and password='{1}'", this.textBox1.Text.Trim(), this.textBox2.Text.Trim());user两边加中括号,因为user是数据库的关键字,如果怕遇到数据库关键字,就在字段名、表名的两边加上中括号。另外在设计表结构的时候应尽量避免数据库的关键字。
      

  5.   

    string sql = string.Format("select COUNT(*) from [user] where username='{0}' and password='{1}'", this.textBox1.Text.Trim(), this.textBox2.Text.Trim()); 
    单步调试语句看看