搜索酒店,用到城市,价格范围,酒店名称,星级4个条件,开始用存储过程做,有错误,后来改用程序拼SQL执行,成功了。
现在有几个问题,希望大家给予帮助:
    1、查询条件是否要指定默认值,因为用户不可能每个条件都输入,默认值在程序里指定还是存储过程里呢?
    2、存储过程拼SQL语句的时候,参数的引号问题:
         
ALTER  proc csp_x_HotelSearch
@Cname varchar(100),    --城市
@BP varchar(100),    --价格起
@EP varchar(100),    --价格止
@Ename varchar(200),    --酒店名
@Estar varchar(50)    --星级
as
declare @sql varchar(1000)
declare @condition varchar(500)

set @sql='select a.Enum,b.Ifullname,c.Elevel,d.Cname,a.PStitle,a.PSprice,e.UTid from dbo.at_ProductService_Eid a
left join dbo.at_Info_Eid b on a.Enum=b.Enum
left join dbo.at_EnterpriseUser c on a.Enum=c.Enum
left join dbo.at_City d on b.Cid=d.Cid
left join dbo.at_Type_CPid e on a.T_UTid=e.UTid
where a.IsDelete=0 and c.Eisfreeze=0 and 
e.CPid in (select f.CPid from dbo.at_ColumnProductService_Did f where f.ETid=2) 
and (d.Cname like 
''%'+@Cname+'%'' or '+@Cname+' is null) 
and (b.Ifullname like 
''%'+@Ename+'%'' or '+@Ename+' is null) 
and (c.Elevel >=
'+@Estar+' or '+@Estar+' is null) 
and 
'
set @condition=
case 
when @BP !='' and @EP !=''  then   'left(a.PSprice,len(a.PSprice)-1) between '+@BP+' and '+@EP
when @BP !='' and @EP =''   then   'left(a.PSprice,len(a.PSprice)-1) >= '+@BP
when @EP !='' and @BP =''   then   'left(a.PSprice,len(a.PSprice)-1) <= '+@EP
end
set @sql = @sql + @condition
exec(@sql)

    3、程序里动态SQL的方法:
string sql = "select a.Enum,b.Ifullname,c.Elevel,d.Cname,a.PStitle,a.PSprice,e.UTid from dbo.at_ProductService_Eid a ";
        sql 
+= "left join dbo.at_Info_Eid b on a.Enum=b.Enum "
        sql 
+= "left join dbo.at_EnterpriseUser c on a.Enum=c.Enum ";
        sql 
+= "left join dbo.at_City d on b.Cid=d.Cid ";
        sql 
+= "left join dbo.at_Type_CPid e on a.T_UTid=e.UTid ";
        sql 
+= "where a.IsDelete=0 and c.Eisfreeze=0 and ";
        sql 
+= "e.CPid in (select f.CPid from dbo.at_ColumnProductService_Did f where f.ETid=2)  ";
        
try
        
{
            
if (tbArea.Text != null && tbArea.Text != "")
                
{
                    sql 
+= " and d.Cname like '%" + tbArea.Text + "%' ";
                }

            
if (tbBp.Text != null && tbBp.Text != "" && tbEp.Text != null && tbEp.Text != "")
                
{
                    sql 
+= " and left(a.PSprice,len(a.PSprice)-1) between " + tbBp.Text + "and " + tbEp.Text;
                }

            
else if (tbBp.Text != null && tbBp.Text != "")
                
{
                    sql 
+= " and left(a.PSprice,len(a.PSprice)-1) >=" + tbBp.Text;
                }

            
else if (tbEp.Text != null && tbEp.Text != "")
                
{
                    sql 
+= " and left(a.PSprice,len(a.PSprice)-1) <=" + tbEp.Text;
                }

            
if (tbHname.Text != null && tbHname.Text != "")
                
{
                    sql 
+= " and b.Ifullname like '%" + tbHname.Text + "%' ";
                }

            
if (rblEstar.SelectedIndex != -1)
                
{
                    sql 
+= " and c.Elevel >=" + float.Parse(rblEstar.SelectedValue.ToString());
                }

                
//Response.Write(sql);
                DataSet ds = dbs.ExecSelectSql(sql);

                GridView1.DataSource 
= ds;
                GridView1.DataBind();
posted on 2007-09-12 12:27  xiaohc  阅读(766)  评论(0编辑  收藏  举报