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

未处理sqlException SqlDataReader sdr= sqlcmd.ExecuteReader();语法错误
哪位前辈给看看这段代码哪错了 我在网上搜了好久 似乎说是sql语句错了 不知道怎么改 求赐教


private void cbxID_SelectedIndexChanged(object sender, System.EventArgs e)
  {
  //创建SQL命令对象
  SqlCommand sqlcmd = new SqlCommand("SELECT * FROM [Dev_Test].[dbo].[Customers] WHERE CustomersID = @ ID", sqlConnection1);
  //设置参数
  sqlcmd.Parameters.AddWithValue("@ ID", cbxID.Text);
  SqlDataReader sdr;
  sqlConnection1.Open();
  sdr = sqlcmd.ExecuteReader();
  if (sdr.Read())
  {
  //使用不同的方式读取特定字段的值
  txtCompanyName.Text = sdr.GetString(1);
  txtContactName.Text = sdr["ContactName"].ToString();
  txtContactTitle.Text = sdr[3].ToString();
  txtAddress.Text = sdr.GetValue(4).ToString();
  txtCity.Text = sdr["City"].ToString();
  txtRegion.Text = sdr.GetValue(6).ToString();
  txtPostalCode.Text = sdr[7].ToString();
  txtCountry.Text = sdr[8].ToString();
  txtPhone.Text = sdr[9].ToString();
  txtFax.Text = sdr[10].ToString();
  }
  sdr.Close();
  sqlConnection1.Close();
  }

------解决方案--------------------
"@ ID",这个参数ID前怎么还有空格啊
------解决方案--------------------
测试下SQL语句不就知道是不是正确的了?
------解决方案--------------------
"@ ID" 多个空格
------解决方案--------------------
SqlCommand sqlcmd = new SqlCommand("SELECT * FROM [Customers] WHERE CustomersID = @ID", sqlConnection1);
sqlcmd.Parameters.AddWithValue("@ID", cbxID.Text);
------解决方案--------------------
先测试下SQL语句