举例说明,实现如下多条件查询:
Private Sub Command1_Click() Dim jsql jsql = ""
Dim smyregion As String If Check1.Value = 1 Then
jsql = "图书名称 like '%" + Text1.Text + "%'" End If
If Check2.Value = 1 Then If jsql = "" Then
jsql = "作者姓名 like'%" + Text2.Text + "%'" Else
jsql = jsql &"and 作者姓名 like'%" + Text2.Text + "%'" End If End If
If Check3.Value = 1 Then If jsql = "" Then
jsql = "出版社名称 like'%" + Text3.Text + "%'" Else
jsql = jsql &"and 出版社名称 like '%" + Text3.Text + "%'" End If End If
If Check4.Value = 1 Then If jsql = "" Then
jsql = "出版时间 like '%" + Text4.Text + "%'" Else
jsql = jsql &"and 出版时间 like '%" + Text3.Text + "%'" End If End If
If Check5.Value = 1 Then If jsql = "" Then
jsql = "图书类别 like '%" + Text5.Text + "%'"
Else
jsql = jsql &"and 图书类别 like '%" + Text5.Text + "%'" End If End If
If jsql = "" Then
MsgBox "请选择查询条件!", vbInformation, "图书音像管理系统" Exit Sub Else
Adodc1.RecordSource = "select * from book where " &jsql Adodc1.Refresh End If
If Adodc1.Recordset.RecordCount >0 Then Set DataGrid1.DataSource = Adodc1 End If End Sub
Private Sub Command2_Click() Unload Me End Sub
举例说明,实现如下多条件查询:Private Sub Command1_Click() Dim jsql jsql = ""
Dim smyregion As String If Check1.Value = 1 Then
jsql = "图书名称 like '%" + Text1.Text + "%'" End If
If Check2.Value = 1 ThenIf jsql = "" Then
jsql = "作者姓名 like'%" + Text2.Text + "%'"Else
jsql = jsql &"and 作者姓名 like'%" + Text2.Text + "%'" End If End If
If Check3.Value = 1 ThenIf jsql = "" Then
jsql = "出版社名称 like'%" + Text3.Text + "%'" Else
jsql = jsql &"and 出版社名称 like '%" + Text3.Text + "%'" End If End If
If Check4.Value = 1 Then If jsql = "" Then
jsql = "出版时间 like '%" + Text4.Text + "%'" Else
jsql = jsql &"and 出版时间 like '%" + Text3.Text + "%'" End If End If
If Check5.Value = 1 ThenIf jsql = "" Then
jsql = "图书类别 like '%" + Text5.Text + "%'"
Else
jsql = jsql &"and 图书类别 like '%" + Text5.Text + "%'" End If End If
If jsql = "" Then
MsgBox "请选择查询条件!", vbInformation, "图书音像管理系统" Exit Sub Else
Adodc1.RecordSource = "select * from book where " &jsql Adodc1.Refresh End If
If Adodc1.Recordset.RecordCount >0 Then Set DataGrid1.DataSource = Adodc1 End IfEnd Sub
Private Sub Command2_Click() Unload Me End Sub
1、首先打开Visual Studio 2008代码窗口,添加引用。
2、输入以下代码:Public conn1 As SqlConnection = New SqlConnection 。
3、声明关键字 Public;(因为是全局变量,所以用Public 来声明)。
4、如果SQL 数据库就在本机,则用以下代码连接。
5、如果代码太长,影响可读性,可以用空格加"_"后,回车换行即可。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)