当前位置: 代码迷 >> Sql Server >> 求动态sql写法,该如何解决
  详细解决方案

求动态sql写法,该如何解决

热度:40   发布时间:2016-04-27 18:08:11.0
求动态sql写法
比如淘宝:
我们在淘宝购物时可以任意选择产品分类、产品尺寸、产品品牌、产品价格、产品售后情况等等

比如:
所有分类> 笔记本电脑> 15寸> ThinkPad(IBM)> SL500> 5001-7000元> 酷睿二代> 全国联保> SL500-2DC P7570/独显
我在想这些条件都是用户在页面上任意指定的,淘宝技术员是怎么动态把对应的数据查询出来的?应当是动态组合吧?如果是,又如何组合的呢?

------解决方案--------------------
我在程序的做法是这样的
VB.NET code
Dim cnn As New SqlConnection((New ConnectionString().ConnectionInfo))        Dim sql As String        sql = "select * from caigouwuliaoziliao_table where 1=1"        If RadioButton2.Checked = True Then            sql = sql + "and shenhe=1"        End If        If RadioButton3.Checked = True Then            sql = sql + "and shenhe=0"        End If        If textbox1.Text <> "" Then            sql = sql + "and biaodan_no='" & textbox1.Text + "'"        End If        If TextBox2.Text <> "" Then            sql = sql + "and wuliao_no='" & TextBox2.Text + "'"        End If        If TextBox3.Text <> "" Then            sql = sql + "and baojia_no='" & TextBox3.Text + "'"        End If        If TextBox4.Text <> "" Then            sql = sql + "and kehu='" & TextBox4.Text + "'"        End If        If TextBox5.Text <> "" Then            sql = sql + "and sbmc='" & TextBox5.Text + "'"        End If        If TextBox6.Text <> "" Then            sql = sql + "and wlmc='" & TextBox6.Text + "'"        End If        If ComboBox1.Text <> "" Then            sql = sql + "and gys='" & ComboBox1.Text + "'"        End If
------解决方案--------------------
这种应该很好实现的,比如 如果是下拉选择框,前端程序可以判断选择框里面是否有选择选项,若有选择项,则相应增加where子句后面的筛选条件。并且,淘宝应该会做自动筛选,比如你先选择了产品分类(男装),下面的产品子类应该会自动筛选(你就不会再选中女士skirt)。
  相关解决方案