日期:2014-05-18  浏览次数:20502 次

求大神解救
private void button1_Click(object sender, EventArgs e)
  {
  using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=true;
  User Instance=true"))
  {
  conn.Open();
  string usename=textBox1.Text;
  string password = textBox2.Text;
  using (SqlCommand cmd = conn.CreateCommand())
  {
  cmd.CommandText = "select * from ref where uses="+usename+"";
  using (SqlDataReader reader = cmd.ExecuteReader())
  {
  if (reader.Read()==true)
  {
  string dpasssword = reader.GetString(reader.GetOrdinal("password"));
  if (password == dpasssword)
  {
  MessageBox.Show("登录成功");
  }
  }
  else
  {
  MessageBox.Show("登录失败");
  }



怎么老是SqlDataReader reader = cmd.ExecuteReader()报错说列名无效。

------解决方案--------------------
SQL code

select * from ref where uses="+usename+"

将这条语句放到查询分析器运行看看有没有错。
报列名无效很可能是你的列名写错了或者表名写错了

------解决方案--------------------
cmd.CommandText = "select * from ref where uses='"+usename+"'";