我有一些我想知道的东西,我希望每个人都可以帮助我。所以这是我的问题,我怎样才能缩短我的代码。 :)
If MetroTextBox1.Text = "" Then
If MetroCheckBox1.Checked = True And MetroCheckBox2.Checked = False Then
query = "select id, fname, mname, lname, dept, salary, ' ' as lengthservice from tbl_Employee where dresigned is null and " +
" (id like '%" & MetroTextBox1.Text & "%' or fname like '%" & MetroTextBox1.Text & "%' or mname like '%" & MetroTextBox1.Text & "%' or lname like '%" & MetroTextBox1.Text & "%' or dept like '%" & MetroTextBox1.Text & "%') "
dt = c.GetDataTable(query)
ElseIf MetroCheckBox2.Checked = True And MetroCheckBox1.Checked = False Then
query = "select id, fname, mname, lname, dept, salary, ' ' as lengthservice from tbl_Employee where dresigned is not null and " +
" (id like '%" & MetroTextBox1.Text & "%' or fname like '%" & MetroTextBox1.Text & "%' or mname like '%" & MetroTextBox1.Text & "%' or lname like '%" & MetroTextBox1.Text & "%' or dept like '%" & MetroTextBox1.Text & "%') "
dt = c.GetDataTable(query)
ElseIf MetroCheckBox1.Checked = True And MetroCheckBox2.Checked = True Then
query = "select id, fname, mname, lname, dept, salary from tbl_Employee" +
" where id like '%" & MetroTextBox1.Text & "%' or fname like '%" & MetroTextBox1.Text & "%' or mname like '%" & MetroTextBox1.Text & "%' or lname like '%" & MetroTextBox1.Text & "%' or dept like '%" & MetroTextBox1.Text & "%' "
dt = c.GetDataTable(query)
End If
ElseIf MetroTextBox1.Text <> "" Then
If MetroCheckBox1.Checked = True And MetroCheckBox2.Checked = False Then
query = "select id, fname, mname, lname, dept, salary, ' ' as lengthservice from tbl_Employee where dresigned is null and " +
" (id like '%" & MetroTextBox1.Text & "%' or fname like '%" & MetroTextBox1.Text & "%' or mname like '%" & MetroTextBox1.Text & "%' or lname like '%" & MetroTextBox1.Text & "%' or dept like '%" & MetroTextBox1.Text & "%') "
dt = c.GetDataTable(query)
ElseIf MetroCheckBox2.Checked = True And MetroCheckBox1.Checked = False Then
query = "select id, fname, mname, lname, dept, salary, ' ' as lengthservice from tbl_Employee where dresigned is not null and " +
" (id like '%" & MetroTextBox1.Text & "%' or fname like '%" & MetroTextBox1.Text & "%' or mname like '%" & MetroTextBox1.Text & "%' or lname like '%" & MetroTextBox1.Text & "%' or dept like '%" & MetroTextBox1.Text & "%') "
dt = c.GetDataTable(query)
ElseIf MetroCheckBox1.Checked = True And MetroCheckBox2.Checked = True Then
query = "select id, fname, mname, lname, dept, salary from tbl_Employee" +
" where id like '%" & MetroTextBox1.Text & "%' or fname like '%" & MetroTextBox1.Text & "%' or mname like '%" & MetroTextBox1.Text & "%' or lname like '%" & MetroTextBox1.Text & "%' or dept like '%" & MetroTextBox1.Text & "%' "
dt = c.GetDataTable(query)
End If
Else
query = "select id, fname, mname, lname, dept, salary, ' ' as lengthservice from tbl_Employee where id = ''"
dt = c.GetDataTable(query)
End If
答案 0 :(得分:0)
由于问题是关于缩短代码,请按照以下方式进行:
query = " (id like '%" & MetroTextBox1.Text & "%' or fname like '%" & MetroTextBox1.Text & "%' or mname like '%" & MetroTextBox1.Text & "%' or lname like '%" & MetroTextBox1.Text & "%' or dept like '%" & MetroTextBox1.Text & "%')"
If MetroTextBox1.Text = "" Then
If MetroCheckBox1.Checked = True And MetroCheckBox2.Checked = False Then
query = "select id, fname, mname, lname, dept, salary, ' ' as lengthservice from tbl_Employee where dresigned is null and" + query
ElseIf MetroCheckBox2.Checked = True And MetroCheckBox1.Checked = False Then
query = "select id, fname, mname, lname, dept, salary, ' ' as lengthservice from tbl_Employee where dresigned is not null and" + query
ElseIf MetroCheckBox1.Checked = True And MetroCheckBox2.Checked = True Then
query = "select id, fname, mname, lname, dept, salary from tbl_Employee where" + query
End If
ElseIf MetroTextBox1.Text <> "" Then
If MetroCheckBox1.Checked = True And MetroCheckBox2.Checked = False Then
query = "select id, fname, mname, lname, dept, salary, ' ' as lengthservice from tbl_Employee where dresigned is null and" + query
ElseIf MetroCheckBox2.Checked = True And MetroCheckBox1.Checked = False Then
query = "select id, fname, mname, lname, dept, salary, ' ' as lengthservice from tbl_Employee where dresigned is not null and" + query
ElseIf MetroCheckBox1.Checked = True And MetroCheckBox2.Checked = True Then
query = "select id, fname, mname, lname, dept, salary from tbl_Employee where" + query
End If
Else
query = "select id, fname, mname, lname, dept, salary, ' ' as lengthservice from tbl_Employee where id = ''"
End If
dt = c.GetDataTable(query)
答案 1 :(得分:0)
这是更好(和更短)的代码:
Dim t1 As String = MetroTextBox1.Text
Dim c1 As Boolean = MetroCheckBox1.Checked
Dim c2 As Boolean = MetroCheckBox2.Checked
Dim a, b As String
a = "select id, fname, mname, lname, dept, salary"
b = "(id like '%" & t1 & "%' or fname like '%" & t1 & "%' or mname like '%" & t1 & "%' or lname like '%" & t1 & "%' or dept like '%" & t1 & "%') "
If c1 And Not c2 Then
query = a + ", ' ' as lengthservice from tbl_Employee where dresigned is null and " + b
ElseIf c2 And Not c1 Then
query = a + ", ' ' as lengthservice from tbl_Employee where dresigned is not null and " + b
Else 'If c1 And c2 Then
query = a + " from tbl_Employee where " + b
End If
dt = c.GetDataTable(query)
(很明显,永远不会调用最终的Else
因为总是MetroTextBox1.Text =&#34;&#34;或MetroTextBox1.Text&lt;&gt;&#34;&#34;将是真的。)
但是,这里有一个注释。当MetroTextBox1.Text =&#34;&#34;时,b中指定的条件变为(id like '%%' or fname like '%%' or ...)
,可以从查询中删除where 1=1
之类的内容。但它不是基于您的原始代码。如果你愿意,我可以提供修改后的代码
答案 2 :(得分:0)
由于问题是缩短代码,这里是没有If
语句的简短解决方案
Dim query As String =
"SELECT id, fname, mname, lname, dept, salary, ' ' AS lengthservice
FROM tbl_Employee
WHERE (id LIKE @MetroTextBox1
OR fname LIKE '%" & MetroTextBox1.Text & "%'
OR mname LIKE '%" & MetroTextBox1.Text & "%'
OR lname LIKE '%" & MetroTextBox1.Text & "%'
OR dept LIKE '%" & MetroTextBox1.Text & "%')"
Dim condition As New Dictionary(Of Byte, String) From
{
{0, ""}, 'You don't have result for case False, False
{1, " AND dresigned IS NULL"}, 'If MetroCheckBox1.Checked = True
{2, " AND dresigned IS NOT NULL"}, 'If MetroCheckBox2.Checked = True
{3, ""} 'Both Checkboxes are checked
}
Dim checkBox1 As Byte = Convert.ToByte(MetroCheckBox1)
Dim checkBox2 As Byte = Convert.ToByte(MetroCheckBox2) << 1
Dim selection As Byte = checkBox1 Or checkBox2
query = query & condition(selection)
dt = c.GetDataTable(query)
非常重要:使用SqlParameters将值传递给查询
参数可以帮助您避免Sql注入问题,也可以让您的查询运行得更快。