如何在存储过程中使用if之后的条件

时间:2015-01-30 15:57:39

标签: sql asp.net sql-server stored-procedures

我已经写过这个方法来读取数据库的数据。

string query = @"SELECT 
                    IDproduct, name_product, first_price, 
                    final_price, max_registered_price, 
                    date_record_shamsi, final_date_view_shamsi,
                    count_views, image_1, collection_1, city, model   
                 FROM
                    Table_khodro 
                 WHERE  
                    active = 0 ";

public DataSet ViewProductKhodro(string name, int firstPrice, int finalPrice, string subCollection, byte state,Int16 model)
{
    if (name !="no name")
        query += @" AND (name_product LIKE '%'+(@name_product)+'%')";

    if (finalPrice != 0)
        query += @" AND (first_price BETWEEN @first_price AND @final_price )";

    if (subCollection !="انتخاب کنید")
        query += @" AND (collection_1 = @collection_1  )";

    if (state != 0)
        query += @" AND (id_state = @id_state  )";

    if(model != 0)
       query += @" AND (model = @model  )";

    SqlCommand cmd = new SqlCommand(query);

    if (name != "no name")
    {
        cmd.Parameters.AddWithValue("@name_product", name);
    }

    if (finalPrice !=0)
    {
        cmd.Parameters.AddWithValue("@first_price", firstPrice);
        cmd.Parameters.AddWithValue("@final_price", finalPrice);
    }

    if (subCollection != "انتخاب کنید")
        cmd.Parameters.AddWithValue("@collection_1", subCollection);

    if (state !=0)
        cmd.Parameters.AddWithValue("@id_state", state);

    if (model != 0)
        cmd.Parameters.Add("@model",model );

    // Fill the dataset
    return FillDataSet(cmd, "Table_khodro");
}

现在我想为这个方法编写一个存储过程。

是否可以使用一个存储过程?

如何?

请帮忙

2 个答案:

答案 0 :(得分:0)

您可以在查询中包含所有这些参数。

SELECT  IDproduct ,
        name_product ,
        first_price ,
        final_price ,
        max_registered_price ,
        date_record_shamsi ,
        final_date_view_shamsi ,
        count_views ,
        image_1 ,
        collection_1 ,
        city ,
        model
FROM    Table_khodro
WHERE   active = 0 AND (@NAME IS NULL OR NAME_PRODUCT LIKE @NAME)
AND (@FIRSTPRICE IS NULL OR FIRST_PRICE BETWEEN @FIRSTPRICE AND @FINALPRICE)

因此,您的存储过程将具有所有必需的参数。您还必须传入您认为可能为null或可能不为null的参数。例如,如果@firstprice为null,则sql查询将在不考虑First_price列的情况下执行,否则它将使用介于价格之间并带来相应的结果集

希望它有所帮助。

答案 1 :(得分:0)

感谢您的回答 我改变了我的方法:

 public DataSet ViewProductKhodro(string name, int firstPrice, int finalPrice, string subCollection, byte state,Int16 model)
{  SqlCommand cmd = new SqlCommand();
    cmd.CommandText="search_khodro";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@name_product", name);
    cmd.Parameters.AddWithValue("@first_price",firstPrice);
    cmd.Parameters.AddWithValue("@final_price",finalPrice);
    cmd.Parameters.AddWithValue("@collection_1",subCollection);
    cmd.Parameters.AddWithValue("@id_state", state);
    cmd.Parameters.Add("@model", model);
    return FillDataSet(cmd, "Table_khodro");
}

并编写一个这样的存储过程:

ALTER PROCEDURE dbo.search_khodro   
@name_product nvarchar(50),
@first_price  int,
@final_price  int,
@collection_1 nvarchar(30),
@id_state     tinyint ,
@model        smallint
AS begin SELECT IDproduct,name_product,first_price,final_price,max_registered_price,date_record_shamsi,final_date_view_shamsi, count_views,image_1,collection_1,city,model
from 
Table_khodro
 where 
 active=0 And ( @name_product IS NULL OR NAME_PRODUCT LIKE @name_product)  
   And (@first_price IS NULL OR first_price between  @first_price AND @final_price ) 
 And( @collection_1 IS NULL OR collection_1= @collection_1) And(@id_state IS NULL OR id_state=@id_state ) And (@model IS NULL OR model=@model)
end

此方法没有错误但没有结果。请检查是否有问题?