我有一个从用户输入构建SQL语句的表单。控件包括CheckBox,TextBox,ComboBox和DateTimePicker。
它们分组在表单的每个部分的全局数组中。一些语句将检查SQL命令的多个控件。
现在我使用一堆if ... else if语句来构建我的SQL语句,检查检查状态,文本更改等等。但是,如果更改数组中的控制索引,则此方法已证明不稳定。
我正在寻找一种更简单的方法来一次构建SQL语句。每次控件的状态发生变化时,我都想过为每个控件组更改一个字符串,从而在用户想要运行时不需要构建SQL,但是我不确定这是不是一个很好的方法处理它。
我目前使用的方法有替代方法吗?那个我一直在考虑转而值得努力的人吗?
编辑:根据要求,这里是我目前使用的一些代码。
'icControls is the class that has all of the global arrays
'DefaultStrings is the module that has the default strings in the textboxes
Dim sqlWhere As String = "Where "
If Me._icControls.chkBasic(0).Checked = True And Me._icControls.chkBasic(1).Checked = True _
And Me._icControls.chkBasic(2).Checked = True Then
sqlWhere += """Field1"" in('A', 'B', 'C')"
ElseIf Me._icControls.chkBasic(0).Checked = True And Me._icControls.chkBasic(1).Checked = True Then
sqlWhere += """Field1"" in('A', 'B')"
ElseIf Me._icControls.chkBasic(0).Checked = True And Me._icControls.chkBasic(2).Checked = True Then
sqlWhere += """Field1"" in('A', 'C')"
ElseIf Me._icControls.chkBasic(1).Checked = True And Me._icControls.chkBasic(2).Checked = True Then
sqlWhere += """Field1"" in('B', 'C')"
ElseIf Me._icControls.chkBasic(0).Checked = True Then
sqlWhere += """Field1"" in('A')"
sqlWhere += " AND (SUBSTR(""Tag #"", 1, 1) not in('#') and ""Inventory Type"" in('F'))"
ElseIf Me._icControls.chkBasic(1).Checked = True Then
sqlWhere += """Field1"" in('B')"
sqlWhere += " AND (SUBSTR(""Tag #"", 1, 1) in('R', 'A', 'Z') and ""Inventory Type"" in('R'))"
ElseIf Me._icControls.chkBasic(2).Checked = True Then
sqlWhere += """Field1"" in('')"
sqlWhere += " AND (SUBSTR(""Tag #"", 1, 1) in('W'))"
End If
If Me._icControls.chkBasic(4).Checked = True And Me._icControls.chkBasic(5).Checked = True Then
sqlWhere += " AND ""Field2"" in('A', 'B')"
ElseIf Me._icControls.chkBasic(4).Checked = True Then
sqlWhere += " AND ""Field2"" in('A')"
ElseIf Me._icControls.chkBasic(5).Checked = True Then
sqlWhere += " AND ""Field2"" in('B')"
End If
If Me._icControls.chkBasic(6).Checked = True And Me._icControls.chkBasic(7).Checked = True Then
sqlWhere += " AND ""Field3"" in('A', 'B')"
ElseIf Me._icControls.chkBasic(6).Checked = True Then
sqlWhere += " AND ""Field3"" in('A')"
ElseIf Me._icControls.chkBasic(7).Checked = True Then
sqlWhere += " AND ""Field3"" in('B')"
End If
If Me._icControls.txtMaterial(0).Text.ToString() <> DefaultStrings.Material(0) Then
sqlWhere += " AND ""Material"" in('" + Me._icControls.txtMaterial(0).Text.ToString() + "')"
End If
答案 0 :(得分:0)
我在评论中提出的解决方案将包括以下步骤:
答案 1 :(得分:0)
我认为仅在您需要时(按下按钮后)生成SQL语句的方法比每次值更改时生成它更好。
那你每次都不需要检查价值,
检查它并将结果保存到变量
Dim chkControl0 As CheckBox
Dim chkControl1 As CheckBox
Dim chkControl2 As CheckBox
Dim chkControl3 As CheckBox
Dim chkControl4 As CheckBox
Dim chkControl5 As CheckBox
Dim chkControl6 As CheckBox
Dim chkControl7 As CheckBox
Private Function GenerateField1WhereClause() As String
Const CHK0 As String = "'A'"
Const CHK1 As String = "'B'"
Const CHK2 As String = "'C'"
Dim checked As New List(Of String)()
If chkControl0.Checked = True Then checked.Add(CHK0)
If chkControl1.Checked = True Then checked.Add(CHK1)
If chkControl2.Checked = True Then checked.Add(CHK2)
If checked.Count = 0 Then Return String.Empty
If checked.Count = 1 Then
Select Case checked(0)
Case CHK0
Return String.Format("""Field1"" = {0} AND (SUBSTR(""Tag #"", 1, 1) NOT IN('#') AND ""Inventory Type"" IN ('F'))", CHK0)
Case CHK1
Return String.Format("""Field1"" = {0} AND (SUBSTR(""Tag #"", 1, 1) in('R', 'A', 'Z') and ""Inventory Type"" in('R'))", CHK1)
Case CHK2
Return String.Format("""Field1"" = {0} AND (SUBSTR(""Tag #"", 1, 1) in('R', 'A', 'Z') and ""Inventory Type"" in('R'))", CHK2)
End Select
String.Format("Field1 = {0} AND (SUBSTR(""Tag #"", 1, 1) in('W'))")
Else
Return String.Format("""Field1"" IN ({0})", String.Join(",", checked))
End If
End Function
Private Function GenerateField2WhereClause() As String
Const CHK0 As String = "'A'"
Const CHK1 As String = "'B'"
Dim checked As New List(Of String)()
If chkControl4.Checked = True Then checked.Add(CHK0)
If chkControl5.Checked = True Then checked.Add(CHK1)
If checked.Count = 0 Then Return String.Empty
Return String.Format("""Field2"" IN ({0})", String.Join(",", checked))
End Function
Private Function GenerateField3WhereClause() As String
Const CHK0 As String = "'A'"
Const CHK1 As String = "'B'"
Dim checked As New List(Of String)()
If chkControl6.Checked = True Then checked.Add(CHK0)
If chkControl7.Checked = True Then checked.Add(CHK1)
If checked.Count = 0 Then Return String.Empty
Return String.Format("""Field3"" IN ({0})", String.Join(",", checked))
End Function
然后使用函数并检查结果是否为空,您可以创建WHERE子句
Private Function GenerateWhereClause() as String
Dim where As New StringBuilder()
Dim field1 As String = Me.GenerateField1WhereClause()
If String.IsNullOrEmpty(field1) = false Then
where.Append(field1)
End If
Dim field2 As String = Me.GenerateField2WhereClause()
If String.IsNullOrEmpty(field2) = false Then
If where.length > 0 Then where.Append(" AND ")
where.Append(field2)
End If
Dim field3 As String = Me.GenerateField3WhereClause()
If String.IsNullOrEmpty(field3) = false Then
If where.length > 0 Then where.Append(" AND ")
where.Append(field3)
End If
If where.Length > 0 Then where.Insert(0, "WHERE ")
Return where.ToString()
End Function
如果您需要表单外部复选框的值,只需创建一个仅返回值
的属性Public ReadOnly Property Check0 As Boolean
Return Me.chkControl0.Checked
End property