(vb windows form)求自定义查询条件(也就是sql语句where后面的代码)的代码或控件
(vb windows form)求自定义查询条件(也就是sql语句where后面的代码)的代码或控件 --------------------编程问答-------------------- 定义一个字符串,存放你自定义查询内容,然后判断它是否为空,若不为空,则再加上 AND,连接第二个自定义查询内容。然后一起加到 WHERE 后面就行了。 --------------------编程问答-------------------- 例:
s_SQL = "SELECT * FROM zzz_VIEW_CqBb_zt WHERE 1=1 "
If cmbCj.Text.Trim.Length <> 0 Then
If cmbCj.Text.Trim <> "全部" Then
s_SQL = s_SQL & " AND 车间 = '" & cmbCj.Text.Trim & "'"
End If
End If
If Me.txtYgbh.Text.Trim.Length <> 0 Then
s_SQL = s_SQL & " AND 员工编号 = '" & Me.txtYgbh.Text.Trim & "'"
End If
s_SQL = s_SQL & " AND (入厂日期 BETWEEN '" & GetDTPDataValue(dtpB) & "' AND '" & GetDTPDataValue(dtpE) & "')"
s_SQL = s_SQL & " ORDER BY 入厂日期,员工编号" --------------------编程问答-------------------- 偶也遇到类似的拼接SQL语句的问题了 --------------------编程问答--------------------
strSql = "SELECT * FROM VWEmployees WHERE 1=1 " & GetWhere() & " order by EmpID"
''' <summary>
''' where查询条件
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Private Function GetWhere() As String
Dim strWhere As String = ""
If Me.txtEmpID.Text.Trim.Length > 0 Then
strWhere = strWhere & " AND EmpID ='" + Me.txtEmpID.Text.Trim + "' "
ElseIf Me.txtEmpName.Text.Trim.Length > 0 Then
strWhere = strWhere & " AND EmpName LIKE '%" + Me.txtEmpName.Text.Trim + "%' "
ElseIf Me.cmbEmpSex.Text.Trim.Length > 0 Then
If cmbEmpSex.Text.Trim = "男" Then
strWhere = strWhere & " AND EmpSex LIKE '%" + "1" + "%' "
Else
strWhere = strWhere & " AND EmpSex LIKE '%" + "0" + "%' "
End If
ElseIf Me.txtEmpPID.Text.Trim.Length > 0 Then
strWhere = strWhere & " AND EmpPID LIKE '%" + Me.txtEmpPID.Text.Trim + "%'"
ElseIf Me.txtEmpAge.Text.Trim.Length > 0 Then
strWhere = strWhere & " AND EmpAge LIKE '%" + Me.txtEmpAge.Text.Trim + "%'"
ElseIf Me.cmbDepID.Text.Trim.Length > 0 Then
strWhere = strWhere & "AND DepID LIKE '%" + Me.cmbDepID.Text.Trim + "%'"
ElseIf Me.cmbDepName.Text.Trim.Length > 0 Then
If cmbDepName.Text.Trim = "事业一部" Then
strWhere = strWhere & "AND DepID LIKE '%" + "D01" + "%'"
ElseIf cmbDepName.Text.Trim = "事业二部" Then
strWhere = strWhere & "AND DepID LIKE '%" + "D02" + "%'"
Else
strWhere = strWhere & "AND DepID LIKE '%" + "D020" + "%'"
End If
ElseIf Me.cmbPDepID.Text.Trim.Length > 0 Then
strWhere = strWhere & "AND PDepID LIKE '%" + Me.cmbPDepID.Text.Trim + "%'"
End If
Return strWhere
End Function --------------------编程问答-------------------- 我靠 07年的帖子啊 --------------------编程问答-------------------- 3楼,很好很强大,挖掘机……
补充:.NET技术 , VB.NET