利用C#编写窗口应用程序,把数据库的信息输出到窗口的textbox中,运行过程出错。 大神们帮帮忙呀

从数据库读取信息并显示函数
public void find(string key) { SqlConnection conn = new SqlConnection(@"Data Source=ACER;Initial Catalog=student;Integrated Security=True");//链接数据库 conn.Open(); string a; if (radioButton1.Checked == true)//判断查找依据,按姓名还是按学号 { a = "select name,num,picpath from student where key = name"; } else { a = "select name,num,picpath from student where key = num"; } SqlCommand cmd = new SqlCommand(a, conn); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { textBox2.Text = dr["name"].ToString();//将读取到的信息相应的去显示在窗口上 textBox3.Text = dr["num"].ToString(); string Picture = dr["picpath"].ToString();//得到图片的路径和名称,并把图片显示在picturebox控件里 pictureBox1.Image = Image.FromFile(Picture); } dr.Close(); conn.Close(); }
平台:vs2010 sql server2008
报错截图:

根据上面你说的:key是我通过textbox控件输入的变量

还有,我看你的截图发现,你是在页面上就选择好了是采用哪种查询方式(name\num)

那么你sql就该是

select name,num,picpath from student where name ='key'
select name,num,picpath from student where num ='key'

//C#:
   a="select name,num,picpath from student where name='"+key+"'";//采用Name
   b="select name,num,picpath from student where num='"+key+"'";//采用num
   //Key是你输入查询条件的内容

温馨提示:内容为网友见解,仅供参考
第1个回答  2013-11-04
a = "select name,num,picpath from student where key = name"。

查询条件where中key是什么?是radiobutton的key,不是数据库表的字段名。不能这样用
用radiobutton的Value值作为查询:
//radiobutton的value值
string rdbName=radiobutton1.value;
//按姓名查询
a= "select name,num,picpath from student where name=rabName"追问

key是textbox控件输入的内容,原来是我的sql语句语法不对,改过来了已经,还是谢谢你。

第2个回答  2013-11-03
sql语句写错了,name和num是一个变量,这个应该作为参数拼接或者传递给sql语句,这么些肯定报错了,错误提示也是sql错了,修改一下就好了追问

是这样改?
where name = key? 还是不太明白。key是我通过textbox控件输入的变量,name和num时数据库表的表头,而且我在数据库里面新建查询语句,输入程序里的查询语句没有问题啊。

第3个回答  2013-11-03
你的SQL语句错了where name=@key
相似回答