设为首页 收藏本站
查看: 1710|回复: 0

[经验分享] 代码中(C#)支持动态拼接SQL的参数化查询

[复制链接]

尚未签到

发表于 2018-10-17 13:06:10 | 显示全部楼层 |阅读模式
  


  • using System;
  • using System.Collections.Generic;
  • using System.Text;
  • using System.Data;
  • using System.Data.SqlClient;

  • namespace oswica
  • {
  •     class Program
  •     {
  •         static void Main(string[] args)
  •         {
  •             //本demo演示如何在c#代码中对动态拼接的SQL使用参数查询
  •             //demo开发环境vs2008的控制台项目,连接数据库sql 2008
  •             //需要事先在db中建立表和测试数据,表建立和测试数据代码
  •             //参考:http://oswica.blog.51cto.com/756561/466004
  •             //
  •             //定义一个查询语句的开始部分,通常这个部分是固定的
  •             string sql = "select * from oswica_test_table_1 ";
  •             //定义一个查询字段集合,这个一般是可变部分,我们以数组来存储
  •             string[] column = new string[2] { "id","name"};
  •             //定义一个查询字段对应的值集合,这个通常和上面的一个集合一一对应
  •             //为了简单起见,我们只考虑sql查询中的=、and条件,并且不考虑字段类型
  •             //注:在实际开发中,字段类型对查询性能有很重要的影响必须考虑,可
  •             //以使用对象数组来存储,这里我们只是简单用字符串数组表示。
  •             //string[] columnvalue = new string[2] { "1", "oswica%" };//这时候提示数据没有找到,不会报错
  •             string[] columnvalue = new string[2] { "1", "oswica" };//这个时候提示找到一条数据
  •             //构建执行sql,实际开发中应该使用stringbulider,这里不是我们讨论目的
  •             //简单起见,直接使用string拼接。
  •             for (int i = 0; i < column.Length; i++)
  •             {
  •                 if (i == 0)
  •                 {
  •                     sql = sql + &quot; where &quot; + column + &quot;=@&quot; + column;
  •                 }
  •                 else
  •                 {
  •                     sql = sql + &quot; and &quot; + column + &quot;=@&quot; + column;
  •                 }
  •             }
  •             //debug
  •             Console.WriteLine(sql);
  •             //定义数据库连接字符串,请替换为你自己的数据库连接字符串!!!
  •             string connectionString = &quot;server=xxx;user id=xxx;password=xxx;database=db_temp&quot;;
  •             using (SqlConnection connection = new SqlConnection(connectionString))
  •             {
  •                 //构建SqlCommand
  •                 SqlCommand command = new SqlCommand();
  •                 command.Connection = connection;
  •                 command.CommandText = sql;
  •                 command.CommandType = CommandType.Text;
  •                 //构建  SqlParameter,这里个数应该和上面一致
  •                 for (int i = 0; i < columnvalue.Length; i++)
  •                 {
  •                     SqlParameter parameter = new SqlParameter(column, columnvalue);
  •                     command.Parameters.Add(parameter);
  •                 }
  •                 connection.Open();
  •                 SqlDataReader reader = command.ExecuteReader();
  •                 if (reader.HasRows)
  •                 {
  •                     while (reader.Read())
  •                     {
  •                         Console.WriteLine(&quot;{0}: {0}&quot;, reader[0], reader[1]);
  •                     }
  •                 }
  •                 else
  •                 {
  •                     Console.WriteLine(&quot;No rows found.&quot;);
  •                 }
  •                 reader.Close();
  •             }
  •         }
  •     }
  • }
  

  注释26行结果:(正常的查询数据)
  


  • select * from oswica_test_table_1  where id=@id and name=@name
  • 1: 1
  

  注释27行时候的结果:(传入一些可能引起sql注入的字符,你可以自己修改其他注入语句测试。)
  


  • select * from oswica_test_table_1  where id=@id and name=@name
  • No rows found.
  

  这样我们就能够做到即拼接sql 又能够使用参数,避免SQL注入的危险。
  附:测试表结构和测试数据:


  • create table oswica_test_table_1
  • (
  •     id int,
  •     name varchar(50),
  •     remark varchar(100)
  • )
  • go
  • --写入部分测试数据
  • insert into oswica_test_table_1 select 1,'oswica',''
  • insert into oswica_test_table_1 select 2,'stone',''
  • insert into oswica_test_table_1 select 3,'nana',''
  • insert into oswica_test_table_1 select 4,'nana',''
  • go
  欢迎讨论。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-622835-1-1.html 上篇帖子: 在ADO.NET中使用参数化SQL语句的大同小异 下篇帖子: 安装SQLSERVER2008R2保持和SQL2000独立运行的安装过程
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表