从数据库读取信息并显示函数
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'key是textbox控件输入的内容,原来是我的sql语句语法不对,改过来了已经,还是谢谢你。
是这样改?
where name = key? 还是不太明白。key是我通过textbox控件输入的变量,name和num时数据库表的表头,而且我在数据库里面新建查询语句,输入程序里的查询语句没有问题啊。