2023年6月21日发(作者:)
语句参数化(SqlParameter⽤法)(多条件模糊查询的实现)使⽤SqlParameter将查询条件参数化可以达到以下效果:1. 避免SQL注⼊,提⾼代码⽽安全性。2. 利⽤SqlParameter的重载⽅法、参数数组等可以提⾼代码的重⽤率3. 可以实现SQl语句参数的动态添加插⼊内容:using的使⽤,在程序中,⽤using的⽅式打开数据库时,当不⽤数据库连接可以⾃动关闭释放资源。有利于资源的回收利⽤
SQL语句参数化的三种⽅式:1.直接参数化(以win32程序为例)using ent;namespace EasySqlParameter{ class Program { static void Main(string[] args) { string conText = "server=.;uid=sa;pwd=123456;database=ars"; using (SqlConnection con = new SqlConnection(conText)) { ();//打开数据库连接 string sqlText = "select sName from student where sId=@sid";//sql语句带多个参数 using (SqlCommand cmd = new SqlCommand(sqlText, con)) { SqlParameter parameter = new SqlParameter("@sid", 20160001); (parameter);//将参数添加到SQL执⾏命令 eNonQuery();//返回受影响的⾏数 } } } }}
2.参数数组参数化(以win32程序为例)using ent;namespace SQLParameters{ class Program { static void Main(string[] args) { string conText = "server=.;uid=sa;pwd=123456;database=ars"; using (SqlConnection con = new SqlConnection(conText)) { using (SqlCommand cmd = Command()) { (); dText= "select sName from student where sId = @sid and sMajor = @sMajor and sPassword = @sPassword"; //构造参数数组parameters SqlParameter[] parameters = {new SqlParameter("@sid",20160001), new SqlParameter("@smajor",1101), new SqlParameter("@spassword",123456) }; ge(parameters); eNonQuery(); } } } }}
3.参数集合参数化(适合多条件查询或者模糊查询使⽤),以两个条件的winform多条件模糊查询为例:(1)查询界⾯构建如下:(2)搜索按钮中的代码如下: private void btnSearch_Click(object sender, EventArgs e) { //************查询条件集合的获取***************// List
(3)模糊查询结果
2023年6月21日发(作者:)
语句参数化(SqlParameter⽤法)(多条件模糊查询的实现)使⽤SqlParameter将查询条件参数化可以达到以下效果:1. 避免SQL注⼊,提⾼代码⽽安全性。2. 利⽤SqlParameter的重载⽅法、参数数组等可以提⾼代码的重⽤率3. 可以实现SQl语句参数的动态添加插⼊内容:using的使⽤,在程序中,⽤using的⽅式打开数据库时,当不⽤数据库连接可以⾃动关闭释放资源。有利于资源的回收利⽤
SQL语句参数化的三种⽅式:1.直接参数化(以win32程序为例)using ent;namespace EasySqlParameter{ class Program { static void Main(string[] args) { string conText = "server=.;uid=sa;pwd=123456;database=ars"; using (SqlConnection con = new SqlConnection(conText)) { ();//打开数据库连接 string sqlText = "select sName from student where sId=@sid";//sql语句带多个参数 using (SqlCommand cmd = new SqlCommand(sqlText, con)) { SqlParameter parameter = new SqlParameter("@sid", 20160001); (parameter);//将参数添加到SQL执⾏命令 eNonQuery();//返回受影响的⾏数 } } } }}
2.参数数组参数化(以win32程序为例)using ent;namespace SQLParameters{ class Program { static void Main(string[] args) { string conText = "server=.;uid=sa;pwd=123456;database=ars"; using (SqlConnection con = new SqlConnection(conText)) { using (SqlCommand cmd = Command()) { (); dText= "select sName from student where sId = @sid and sMajor = @sMajor and sPassword = @sPassword"; //构造参数数组parameters SqlParameter[] parameters = {new SqlParameter("@sid",20160001), new SqlParameter("@smajor",1101), new SqlParameter("@spassword",123456) }; ge(parameters); eNonQuery(); } } } }}
3.参数集合参数化(适合多条件查询或者模糊查询使⽤),以两个条件的winform多条件模糊查询为例:(1)查询界⾯构建如下:(2)搜索按钮中的代码如下: private void btnSearch_Click(object sender, EventArgs e) { //************查询条件集合的获取***************// List
(3)模糊查询结果
发布评论