1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Data;  
  5. using System.Data.SqlClient;  
  6.  
  7. namespace oswica  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             //本demo演示如何在c#代码中对动态拼接的SQL使用参数查询  
  14.             //demo开发环境vs2008的控制台项目,连接数据库sql 2008  
  15.             //需要事先在db中建立表和测试数据,表建立和测试数据代码  
  16.             //参考:http://oswica.blog.51cto.com/756561/466004  
  17.             //  
  18.             //定义一个查询语句的开始部分,通常这个部分是固定的  
  19.             string sql = "select * from oswica_test_table_1 ";  
  20.             //定义一个查询字段集合,这个一般是可变部分,我们以数组来存储  
  21.             string[] column = new string[2] { "id","name"};  
  22.             //定义一个查询字段对应的值集合,这个通常和上面的一个集合一一对应  
  23.             //为了简单起见,我们只考虑sql查询中的=、and条件,并且不考虑字段类型  
  24.             //注:在实际开发中,字段类型对查询性能有很重要的影响必须考虑,可  
  25.             //以使用对象数组来存储,这里我们只是简单用字符串数组表示。  
  26.             //string[] columnvalue = new string[2] { "1", "oswica%" };//这时候提示数据没有找到,不会报错  
  27.             string[] columnvalue = new string[2] { "1""oswica" };//这个时候提示找到一条数据  
  28.             //构建执行sql,实际开发中应该使用stringbulider,这里不是我们讨论目的  
  29.             //简单起见,直接使用string拼接。  
  30.             for (int i = 0; i < column.Length; i++)  
  31.             {  
  32.                 if (i == 0)  
  33.                 {  
  34.                     sql = sql + " where " + column[i] + "=@" + column[i];  
  35.                 }  
  36.                 else 
  37.                 {  
  38.                     sql = sql + " and " + column[i] + "=@" + column[i];  
  39.                 }  
  40.             }  
  41.             //debug  
  42.             Console.WriteLine(sql);  
  43.             //定义数据库连接字符串,请替换为你自己的数据库连接字符串!!!  
  44.             string connectionString = "server=xxx;user id=xxx;password=xxx;database=db_temp";  
  45.             using (SqlConnection connection = new SqlConnection(connectionString))  
  46.             {  
  47.                 //构建SqlCommand   
  48.                 SqlCommand command = new SqlCommand();  
  49.                 command.Connection = connection;  
  50.                 command.CommandText = sql;  
  51.                 command.CommandType = CommandType.Text;  
  52.                 //构建  SqlParameter,这里个数应该和上面一致  
  53.                 for (int i = 0; i < columnvalue.Length; i++)  
  54.                 {  
  55.                     SqlParameter parameter = new SqlParameter(column[i], columnvalue[i]);  
  56.                     command.Parameters.Add(parameter);  
  57.                 }  
  58.                 connection.Open();  
  59.                 SqlDataReader reader = command.ExecuteReader();  
  60.                 if (reader.HasRows)  
  61.                 {  
  62.                     while (reader.Read())  
  63.                     {  
  64.                         Console.WriteLine("{0}: {0}", reader[0], reader[1]);  
  65.                     }  
  66.                 }  
  67.                 else 
  68.                 {  
  69.                     Console.WriteLine("No rows found.");  
  70.                 }  
  71.                 reader.Close();  
  72.             }  
  73.         }  
  74.     }  
  75. }  

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

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

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

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

这样我们就能够做到即拼接sql 又能够使用参数,避免SQL注入的危险。

附:测试表结构和测试数据:

  1. create table oswica_test_table_1  
  2. (  
  3.     id int,  
  4.     name varchar(50),  
  5.     remark varchar(100)  
  6. )  
  7. go  
  8. --写入部分测试数据  
  9. insert into oswica_test_table_1 select 1,'oswica','' 
  10. insert into oswica_test_table_1 select 2,'stone','' 
  11. insert into oswica_test_table_1 select 3,'nana','' 
  12. insert into oswica_test_table_1 select 4,'nana','' 
  13. go  

欢迎讨论。