c#
  • sql
  • 2013-05-17 123 views 0 likes 
    0

    我将如何创建一个可以充当where子句的字符串? 目前,我做这样说:根据下拉列表选择创建SQL查询

    string strquery = "select * from tbl_DR_data "; 
    string strq2 = "where [Year 1]='" + DropDownList1.SelectedItem.Text + "'and Product='" + DropDownList2.SelectedItem.Text + 
           "'and Media='" + DropDownList3.SelectedItem.Text + "'and Publication='" + DropDownList4.SelectedItem.Text + "'and Genre='" + DropDownList5.SelectedItem.Text + 
           "'and Month='" + DropDownList6.SelectedItem.Value + "'and Section='" + DropDownList7.SelectedItem.Text + "'"; 
    string str3 = strquery + strq2; 
    

    但问题是,所有的下拉列表中必须有内它们的值。 我希望能够根据什么下拉有一个值在init中创建一个where语句。因此,例如DDL1和DDL4具有值,但不包括所有其他下拉列表。

    我该怎么去做这个?

    +6

    一定要使用参数化查询。您的代码已准备好进行sql注入。 – venerik

    +1

    +1 @venerik。另外,为皮特的缘故使用ORM! –

    回答

    0
    string strq2 = "where '"; 
    if(!string.IsNullOrEmpty(DropDownList1.SelectedItem.Text)) 
    strq2 = strq2 + "[Year 1]='" + DropDownList1.SelectedItem.Text + " and "; 
    if(!string.IsNullOrEmpty(DropDownList2.SelectedItem.Text)) 
    strq2 = strq2 + "Product='" + DropDownList2.SelectedItem.Text+ " and "; 
    if(!string.IsNullOrEmpty(DropDownList3.SelectedItem.Text)) 
    strq2 = strq2 + "Media='" + DropDownList3.SelectedItem.Text+ " and "; 
    if(!string.IsNullOrEmpty(DropDownList4.SelectedItem.Text)) 
    strq2 = strq2 + "Publication='" + DropDownList4.SelectedItem.Text+ " and "; 
    if(!string.IsNullOrEmpty(DropDownList5.SelectedItem.Text)) 
    strq2 = strq2 + "Genre='" + DropDownList5.SelectedItem.Text+ " and "; 
    if(!string.IsNullOrEmpty(DropDownList6.SelectedItem.Value)) 
    strq2 = strq2 + "Month='" + DropDownList6.SelectedItem.Value+ " and "; 
    if(!string.IsNullOrEmpty(DropDownList7.SelectedItem.Value)) 
    strq2 = strq2 + "Section='" + DropDownList7.SelectedItem.Value+ " and "; 
    strq2 = strq2.Remove(strq2.Length - 5); 
    
    +0

    工作像一个魅力,但唯一的事情是,第一个如果总是必须执行 – BlahWoo

    0

    你也许可以格式化你的选择查询作为 -

    select * from tbl_DR_data 
    where 
    ISNULL([YEAR 1], 'X') = ISNULL(DropDownList1.SelectedItem.Text, 'X') 
    AND ISNULL(Product, 'X') = ISNULL(DropDownList2.SelectedItem.Text , 'X') 
    AND ... so on. 
    

    这样一来,如果选择任何值,则wher条款的那部分将永远是正确的。

    0

    你必须学习和使用SqlParameter对象..

    string strq2 = "WHERE 1=1"; 
    if(!string.IsNullOrEmpty(DropDownList1.SelectedItem.Text)) 
    strq2 += " AND [Year 1][email protected]"; 
    if(!string.IsNullOrEmpty(DropDownList2.SelectedItem.Text)) 
    strq2 += " AND [Product][email protected]"; 
    if(!string.IsNullOrEmpty(DropDownList3.SelectedItem.Text)) 
    strq2 += " AND [Media][email protected]"; 
    if(!string.IsNullOrEmpty(DropDownList4.SelectedItem.Text)) 
    strq2 += " AND [Publication][email protected]"; 
    if(!string.IsNullOrEmpty(DropDownList5.SelectedItem.Text)) 
    strq2 += " AND [Genre][email protected]"; 
    if(!string.IsNullOrEmpty(DropDownList6.SelectedItem.Value)) 
    strq2 += " AND [Month][email protected]"; 
    if(!string.IsNullOrEmpty(DropDownList7.SelectedItem.Value)) 
    strq2 += " AND [Section][email protected]ion"; 
    
    相关问题