SQL select语句有2个条件

时间:2016-10-31 15:38:17

标签: sql vb.net

我有一个包含CustomerCellReceiptType列的表格。我需要创建一个SELECT语句,显示与CustomerCellReceiptType匹配的每条记录。

我试过这段代码:

If TextBox1.Text.Trim.Length <> 0 OrElse CheckBox4.Checked = True Then
    Dim Conn As New SqlConnection(constr)
    Dim ds As New DataTable
    Dim sqlstr As String = "Select [RcptNum], [RcptDate], [RcptCustName], [RcptCustCell], [RcptAmount], [RcptType], [RcptFld1], [RcptFld2], [RcptFld3], [RcptUser] From [tblReceipt] where (RcptCustCell = '" & TextBox1.Text & "') or ([RcptType] = 'Cash') "
    Dim da As New SqlDataAdapter(sqlstr, Conn)
    ds.Reset()
    da = New SqlDataAdapter(sqlstr, Conn)
    da.Fill(ds)
    dgv.DataSource = ds
    Call griddraw()
    Conn.Close()
End If

Textbox1用于CustomerCellCheckBox4用于ReceiptType。当我输入客户单元格和收据类型时,我应该看到2条记录,但是使用上面的代码我只能看到一条记录。

这是我的表格:

enter image description here

2 个答案:

答案 0 :(得分:2)

如上所述,查看参数以避免SQL注入,它会更清楚地查询您的查询。我把它放在一起可能有所帮助。可能需要为您的应用程序进行一些调整:

/* Anything that gets to the document
 /* Anything that gets to the document
   will hide the dropdown */
$(document).click(function(){
  $("#dropdown").hide();
});

/* Clicks within the dropdown won't make
   it past the dropdown itself */
$("#dropdown").click(function(e){
 e.stopPropagation();
});

答案 1 :(得分:1)

Dim Conn As New SqlConnection(constr)
Dim ds As New DataTable
Dim sqlstr As String = "Select [RcptNum], [RcptDate], [RcptCustName], [RcptCustCell], [RcptAmount], [RcptType], [RcptFld1], [RcptFld2], [RcptFld3], [RcptUser] From [tblReceipt]"

If TextBox1.Text.trim.length <> 0 then
  sqlstr += "where (RcptCustCell = '" & TextBox1.Text & "')"
endif
If chkPaymentCheck.checked then
  if sqlstr.contains("where") = false then
    sqlstr += "where RcptType = 'Check'"
  EndIf
  sqlstr += "or RcptType = 'Check'"
endif
Dim da As New SqlDataAdapter(sqlstr, Conn)
ds.Reset()
da = New SqlDataAdapter(sqlstr, Conn)
da.Fill(ds)
dgv.DataSource = ds
Call griddraw()
Conn.Close()

尝试此操作,您可以继续使用if语句添加更多检查。