简体   繁体   English

如何处理动态sql参数

[英]How to handle dynamic sql parameters

What is a good way to handle dynamic sql parameters? 处理动态sql参数的好方法是什么?

I have a search form that takes in a whole bunch of different search parameters. 我有一个搜索表单,其中包含一大堆不同的搜索参数。 If the parameters are empty and I have the parameter in the sql string will it screw or slow down the query? 如果参数为空并且我在sql字符串中有参数,那么它会阻塞或减慢查询?

Depending on the specific implementation, we have two general approaches to this problem: 根据具体实现,我们有两种解决此问题的通用方法:

1) Dynamically build the filter statement for the SQL query in code skipping any parameters that are empty. 1)在代码中动态构建SQL查询的filter语句,跳过任何空的参数。 This is the best approach if you allow the user to select multiple values for a single column (ie select 0 or more of the 50 states to filter the data). 如果允许用户为单个列选择多个值(即选择50个状态中的0个或更多个来过滤数据),这是最佳方法。

For example: 例如:

Assuming txtCondition1 and txtCondition2 are textboxes: 假设txtCondition1和txtCondition2是文本框:

        // Assuming conn is an open SqlConnection

        System.Text.StringBuilder sbSQL = new StringBuilder(500);

        List<SqlParameter> cParameters = new List<SqlParameter>();

        // Add a default condition of 1=1 so that all subsequent conditions can be added 
        // with AND instead of having to check to see whether or not any other conditions
        // were added before adding AND.
        sbSQL.Append("SELECT * FROM MyTestTable WHERE 1 = 1 ");

        if (!String.IsNullOrEmpty(txtCondition1.Text)) {
            sbSQL.Append(" AND Column1 = @Column1");
            cParameters.Add(new SqlParameter("@Column1", txtCondition1.Text));
        }
        if (!String.IsNullOrEmpty(txtCondition1.Text))
        {
            sbSQL.Append(" AND Column2 = @Column2");
            cParameters.Add(new SqlParameter("@Column2", txtCondition2.Text));
        }

        SqlCommand oCommand = new SqlCommand(sbSQL.ToString, conn);
        if (cParameters.Count != 0) 
        {
            oCommand.Parameters.AddRange(cParameters.ToArray());
        } 

        // Do something with oCommand

2) If the values are more constrained, we usually pass them to a stored procedure, which is responsible for determining whether or not the value is to be evaluated by testing the parameter for "emptinesss", either null, empty string, 0 for numerics, etc. 2)如果值受到更多限制,我们通常会将它们传递给存储过程,该过程负责通过测试“空白”参数来确定是否要评估该值,null,空字符串,0表示数字等

One of the things that can be done is check whether the parameter was passed to your stored procedure. 可以做的一件事是检查参数是否已传递给存储过程。 You can do it like this: 你可以这样做:

create procedure my_procedure (
  @param1 as int = null
  @param2 as int = null
) as 
begin

   select field1, field2, fieldn
     from table
    where ((@param1 is null) or (field2 = @param1))
      and ((@param2 is null) or (field2 = @param2))
end 

I'd rather do this on sql procedure than in the application tho 我宁愿在sql程序上执行此操作而不是在应用程序中执行此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM