日期:2014-05-17  浏览次数:20678 次

登陆窗口找不到记录
各位大侠,请帮我看一下这个代码哪里有问题。我在做一个登陆窗口,我要查找数据库的这个用户和密码,数据库中明明有这个用户,但是这个代码却显示没有数据。请各位大虾帮我看一下。谢谢!
        private void buttonX1_Click(object sender, EventArgs e)
        {
           String username, userpass; 
           username = this.user.Text.Trim();
           userpass = this.password.Text.Trim();
           String strcmd;
           strcmd="select * from ttmanager where user='"+username+"' and password='"+userpass+"'";
           DataSet mydataset = new DataSet();
           SqlDataAdapter mydataadapter = new SqlDataAdapter(strcmd,conn);
           mydataadapter.Fill(mydataset,"ttmanager");
           this.labelX5.Visible = true;
           this.labelX5.Text = mydataset.Tables[0].Rows.Count.ToString();
           /*
              if (mydataset.Tables["ttmanager"].Rows.Count > 0)
           {
               MessageBox.Show("有这个用户");
               conn.Close();
           }
           else
           {
               MessageBox.Show("没有这个用户");
               conn.Close();
           }
            */
        }

------解决方案--------------------
strcmd="select * from ttmanager where [user]='"+username+"' and [password]='"+userpass+"'";
这样试试 
------解决方案--------------------
引用:
我刚试了,真的可以。为什么要加[]呢?字段user和username这个变量不相同啊?

user在sqlserver中是关键词,最好将列名都加上"[]"