搜索10个字段

时间:2011-11-23 01:06:42

标签: sql vb.net

我有十个需要搜索的字段。它由10个文本框组成,每个文本框在数据库中都有自己的指定列,需要搜索。它只有一个搜索按钮。我的问题是,我无法控制用户填充这些框中的哪一个。因此我需要动态检查字段是否为空。附上是我的示例代码..我希望你们可以给我一个更简单的方法..

文本框:     1.注册日期     2.链接代码     3.大厦代码     4. Bldg Desc     5.街道描述     6.村庄描述     7.区域描述     8.地区描述     9.状态描述     10.国家描述

在以下功能项下的示例代码中:     参数actDate =文本框注册日期     参数linkCode =文本框链接代码     参数bldgCode = textbox Bldg Code     参数bldgDesc = textbox Bldg Desc

到目前为止,我只在我的代码中添加了4个文本框。我停止了因为我想知道是否有更简单的方法来做这个...谢谢。

 Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

    accountDt = New DataTable
    accountDt = Items(txtActDate.Text, txtLinkCode.Text, txtBldgCode.Text, txtBldgDesc.Text, "select * from ac_account", "ACTIVATION_DATE_D")
    LoadData(btnCheckAll, btnUnCheckAll, btnAccountWrite, btnAccountFirst, btnAccountPrevious, btnAccountNext, btnAccountLast, _
             lblAccountPage, lstAccount, "ACCOUNT_NAME_V", "ACTIVATION_DATE_D", "ACTIVATED_BY_N", accountDt, "ACSP_AccountLoad", _
             "ACTIVATION_DATE_D", btnMatrix)
    txtActDate.Text = ""
    txtLinkCode.Text = ""
    txtBldgCode.Text = ""
    txtBldgDesc.Text = ""

End Sub

Public Function Items(ByVal actDate As String, ByVal linkCode As String, ByVal bldgCode As String, ByVal bldgDesc As String, ByVal query As String, _
                     ByVal actRegDate As String) As DataTable
    Try
        If actDate <> "" And linkCode <> "" And bldgCode <> "" And bldgDesc <> "" Then
            query = query & " where convert(varchar(10)," & actRegDate & ",101) = '" & actDate & "' and ACCOUNT_LINK_CODE_N = '" & linkCode & "' and BUILDING_CODE_V = '" & bldgCode & "' and BUILDING_DESC_V = '" & bldgDesc & "'"
        ElseIf actDate <> "" And linkCode <> "" And bldgCode <> "" Then
            query = query & " where convert(varchar(10)," & actRegDate & ",101) = '" & actDate & "' and ACCOUNT_LINK_CODE_N = '" & linkCode & "' and BUILDING_CODE_V = '" & bldgCode & "'"
        ElseIf actDate <> "" And linkCode <> "" And bldgDesc <> "" Then
            query = query & " where convert(varchar(10)," & actRegDate & ",101) = '" & actDate & "' and ACCOUNT_LINK_CODE_N = '" & linkCode & "' and BUILDING_DESC_V = '" & bldgDesc & "'"
        ElseIf actDate <> "" And bldgCode <> "" And bldgDesc <> "" Then
            query = query & " where convert(varchar(10)," & actRegDate & ",101) = '" & actDate & "' and BUILDING_CODE_V = '" & bldgCode & "' and BUILDING_DESC_V = '" & bldgDesc & "'"
        ElseIf linkCode <> "" And bldgCode <> "" And bldgDesc <> "" Then
            query = query & " where ACCOUNT_LINK_CODE_N = '" & linkCode & "' and BUILDING_CODE_V = '" & bldgCode & "' and BUILDING_DESC_V = '" & bldgDesc & "'"
        ElseIf actDate <> "" And linkCode <> "" Then
            query = query & " where convert(varchar(10)," & actRegDate & ",101) = '" & actDate & "' and ACCOUNT_LINK_CODE_N = '" & linkCode & "'"
        ElseIf actDate <> "" And bldgCode <> "" Then
            query = query & " where convert(varchar(10)," & actRegDate & ",101) = '" & actDate & "' and BUILDING_CODE_V = '" & bldgCode & "'"
        ElseIf actDate <> "" And bldgDesc <> "" Then
            query = query & " where convert(varchar(10)," & actRegDate & ",101) = '" & actDate & "' and BUILDING_DESC_V = '" & bldgDesc & "'"
        ElseIf linkCode <> "" And bldgCode <> "" Then
            query = query & " where ACCOUNT_LINK_CODE_N = '" & linkCode & "' and BUILDING_CODE_V = '" & bldgCode & "'"
        ElseIf linkCode <> "" And bldgDesc <> "" Then
            query = query & " where ACCOUNT_LINK_CODE_N = '" & linkCode & "' and BUILDING_DESC_V = '" & bldgDesc & "'"
        ElseIf bldgCode <> "" And bldgDesc <> "" Then
            query = query & " where BUILDING_CODE_V = '" & bldgCode & "' and BUILDING_DESC_V = '" & bldgDesc & "'"
        ElseIf actDate <> "" Then
            query = query & " where convert(varchar(10)," & actRegDate & ",101) = '" & actDate & "'"
        ElseIf linkCode <> "" Then
            query = query & " where ACCOUNT_LINK_CODE_N = '" & linkCode & "'"
        ElseIf bldgCode.ToLower <> "" Then
            query = query & " where BUILDING_CODE_V = '" & bldgCode & "'"
        ElseIf bldgDesc.ToLower <> "" Then
            query = query & " where BUILDING_DESC_V LIKE '" & bldgDesc & "%" & "'"
        End If
        conn.Open()
        sqlCmd = New SqlCommand(query, conn)
        sqlCmd.CommandType = CommandType.Text
        sqlDt = New DataTable
        sqlDa = New SqlDataAdapter(sqlCmd)
        sqlDa.Fill(sqlDt)
        conn.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
        If conn.State = ConnectionState.Open Then
            conn.Close()
        End If
    End Try
    Return sqlDt
End Function

2 个答案:

答案 0 :(得分:1)

我们这样做(不幸的是,这是c#,但.NET调用翻译):

List<String> phrases = new List<String>();
if (actDate != "") phrases.Add("convert(varchar(10),actRegDate,101) = '" + actDate.ToShortDateString() + "'";
if (linkCode != "") phrases.Add"(ACCOUNT_LINK_CODE_N = '" + linkCode + "'");

继续向List添加短语。完成后,您可以使用WHERE

创建String.Join子句

String whereClause = String.Join(“AND”,短语);

然后您可以在SQL中使用它。

一次警告:我强烈建议强烈建议使用参数,而不是将值直接连接到短语中。当我在过去完成此操作时,我只是将所有参数添加到SqlCommand,无论它们是否在实际查询中。

但是,如果LINQ2SQL是一个选项,它将为您完成所有这些工作,包括查询组合。

答案 1 :(得分:-1)

对于每个非空搜索框,您应该向WHERE添加另一个条件。如果您不想跟踪是否已添加条件,则添加1 = 1以启动。每个搜索框一个IF。

query = query & "WHERE 1=1 "
IF actDate <> "" THEN 
  query = query & " AND convert(varchar(10)," & actRegDate & ",101) = '" & actDate & "'"
END IF
IF bldgCode <> "" THEN 
   query = query & " and BUILDING_CODE_V = '" & bldgCode & "'"
END IF

每个搜索框都有额外的IF行。