cjcmay 发表于 2018-10-17 13:06:10

代码中(C#)支持动态拼接SQL的参数化查询

  


[*]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 { "id","name"};
[*]            //定义一个查询字段对应的值集合,这个通常和上面的一个集合一一对应
[*]            //为了简单起见,我们只考虑sql查询中的=、and条件,并且不考虑字段类型
[*]            //注:在实际开发中,字段类型对查询性能有很重要的影响必须考虑,可
[*]            //以使用对象数组来存储,这里我们只是简单用字符串数组表示。
[*]            //string[] columnvalue = new string { "1", "oswica%" };//这时候提示数据没有找到,不会报错
[*]            string[] columnvalue = new string { "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, reader);
[*]                  }
[*]                }
[*]                else
[*]                {
[*]                  Console.WriteLine(&quot;No rows found.&quot;);
[*]                }
[*]                reader.Close();
[*]            }
[*]      }
[*]    }
[*]}
  

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


[*]select * from oswica_test_table_1where id=@id and name=@name
[*]1: 1
  

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


[*]select * from oswica_test_table_1where 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]
查看完整版本: 代码中(C#)支持动态拼接SQL的参数化查询