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

c#编程连接数据库问题
目的:想在查询服务窗体,输入书名或编号时,在DataGridView显示符合条件的数据。调试到第三句代码时,却出现始料未及的问题,求解!!!求帮忙!!!初学实在有太多问题了。

代码如下:
  string yanzheng = this.textBox1.Text.Trim();
  int id = Int32.Parse(this.textBox2.Text.Trim());
  SqlConnection cnn = new SqlConnection("Data Source=DJK-PC;Initial Catalog=图书管理系统;Integrated Security=True");
  cnn.Open();
  SqlCommand cd = new SqlCommand();
  cmd.CommandText = "select * from 图书状态表 where book_name ='" + yanzheng + "'or book_id ='" + id + "' ";
  cmd.Connection = conn;
  SqlDataAdapter ada = new SqlDataAdapter(cmd);
  DataSet ds = new DataSet();
  ada.Fill(ds, "图书状态表");
  dataGridView1.DataSource = ds.Tables["图书状态表"];
  conn.Close();


------解决方案--------------------
[code=C#][/code] SqlConnection cnn = new SqlConnection("Data Source=DJK-PC;Initial Catalog=图书管理系统;Integrated Security=True");
cnn.Open();
含有转义字符。改成[code=C#][/code]SqlConnection cnn = new SqlConnection(@"Data Source=DJK-PC;Initial Catalog=图书管理系统;Integrated Security=True");
cnn.Open();