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

C#连接数据库查询

        private void button2_Click(object sender, EventArgs e)
        {  string s = textBox2.Text;
            SqlConnection cn = new SqlConnection("Data Source=.\\SQLEXPRESS;DataBase=MyDatas;Integrated Security=True;");
                cn.Open();
                string str=("select Name from Stu where Id=s");
                 SqlCommand cmd=new SqlCommand(str,cn); 
                
                 
                    SqlDataReader Dr = cmd.ExecuteReader();
                   
                while (Dr.Read())
                {
                    string name = Dr.GetString(0);
                    textBox3.Text = name;
                     
                }

         }




就是那个查询条件查询学生的Id,怎么把文本框输入的s的值传给Id啊?????不会,,急啊,在线等各位大哥大姐帮忙!
数据库 C#

------解决方案--------------------
string str=("select Name from Stu where Id=" + s);
------解决方案--------------------
字符串拼接或者参数化查询。后者能有效防止SQL注入攻击。
------解决方案--------------------
引用:
string str=("select Name from Stu where Id=" + s);

这样可能不太安全 有可能出现sql注入
最好用加参数的方式