我已经设法让搜索功能起作用,但它非常厚实:(
代码:
If dropdown_sV.Value = "N" Then
If WhereParts > 0 Then
WhereClause = WhereClause & " AND"
End If
WhereClause = WhereClause & " [sV] = 'N'"
WhereParts = WhereParts + 1
End If
If dropdown_sV.Value = "G" Then
If WhereParts > 0 Then
WhereClause = WhereClause & " AND"
End If
WhereClause = WhereClause & " [sV] = 'G'"
WhereParts = WhereParts + 1
End If
我可以使这段代码更有效率,因为我有很多选择下拉列表,其中包含以下选项:N,G,O,A,R,U
维持我的申请将是一场噩梦。
答案 0 :(得分:2)
是的,您只能立即使用代码,即 每次写入下拉选择。您可以将代码缩减为:
If WhereParts > 0 Then
WhereClause = WhereClause & " AND"
End If
string value=dropdown_sV.Value;
WhereClause = WhereClause & " [sV] = '"+dropdown_sV.Value+"'"
WhereParts = WhereParts + 1
答案 1 :(得分:1)
您可以使用:
If dropdown_sV.SelectedIndex > -1 Then
If WhereClause.Length > 0 Then
WhereClause = WhereClause & " AND"
End If
WhereClause = WhereClause & " [sV] = " & dropdown_sV.Value
End If
但你真的应该考虑参数化你的查询(如此):
If dropdown_sV.SelectedIndex > -1 Then
If WhereClause.Length > 0 Then
WhereClause = WhereClause & " AND"
End If
WhereClause = WhereClause & " [sV] = @sV"
End If
构建WHERE
子句后,您只需将参数与值相关联:
Dim cmd As SqlCommand = New SqlCommand(queryString, connectionString)
cmd.Parameters.Add("sV", SqlDbType.VarChar, 50).Value = dropdown_sV.Value