版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
关于《Visual Basic.Net 循序渐进》请到百度网盘下载,具体下载地址:
链接:https://pan.baidu.com/s/1IfaLvlklx-nT4KK4VKZuIw
提取码:ip5n
库存查询提供了5种方式的组合查询,每种方式查询语句应该符合库存信息(FormStorageInfo)代码中的sql语句。
例如,以下语句是对产品名称的模糊查询,使用like语句:
(货物信息.产品名称 like '%" & txtGoodsName.Text.Trim & "%')
需要注意的是:在Access中执行模糊查询,通配符使用*(星号),而在vb中,应该使用%(百分号)。
使用多个查询条件时,其间使用 And 组合。例如以下语句判断前一查询条件是否为空,如果不为空则使用 and 连接。
SqlWhere = IIf(SqlWhere = "", subSqlWhere, SqlWhere & " And " & subSqlWhere)
另外,为了严格控制用户输入,通常情况下,需要将ComboBox的DropDownStyle属性设置为DropDownList。
全部代码如下:
Imports System.ComponentModel
Imports System.Data.OleDb
Public Class FormStorageInfoQuery
Dim connection As OleDbConnection
Dim arrSupplier As List(Of Integer)
Dim arrGoodsType As List(Of Integer)
Private Sub FormStorageInfoQuery_Load(sender As Object, e As EventArgs) Handles MyBase.Load
arrSupplier = New List(Of Integer)
arrGoodsType = New List(Of Integer)
connection = New OleDbConnection(databaseConnString)
'打开数据连接
connection.Open()
Call fillControls()
cbStockCondition.SelectedIndex = 0
End Sub
'填充数据选项,主要是 cbSupplier 和 cbGoodsType
Private Sub fillControls()
'新建OleDbCommand对象实例
Dim command As New OleDbCommand()
'=========填充供应商选择框==================
'要执行的SQL查询
command.CommandText = "select 供应商ID,公司名称 from 供应商"
'设置OleDbCommand的数据连接为OleDbConnection
command.Connection = connection
'声明OleDbDataReader对象
Dim odReader As OleDbDataReader
'通过OleDbCommand的ExecuteReader方法获得OleDbDataReader对象实例。
odReader = command.ExecuteReader()
'如果OleDbDataReader中包含数据
If odReader.HasRows Then
'循环读取每一行数据,直到Read方法返回False
Do While odReader.Read
arrSupplier.Add(odReader.GetValue(0))
cbSupplier.Items.Add(odReader.GetValue(1))
Loop
End If
odReader.Close()
'==========填充货物类别选择框===================
'要执行的SQL查询
command.CommandText = "select * from 货物类别"
'通过OleDbCommand的ExecuteReader方法获得OleDbDataReader对象实例。
odReader = command.ExecuteReader()
'如果OleDbDataReader中包含数据
If odReader.HasRows Then
'循环读取每一行数据,直到Read方法返回False
Do While odReader.Read
arrGoodsType.Add(odReader.GetValue(0))
cbGoodsType.Items.Add(odReader.GetValue(1))
Loop
End If
'关闭数据读取器
odReader.Close()
cbSupplier.SelectedIndex = 0
cbGoodsType.SelectedIndex = 0
End Sub
Private Sub FormStorageInfoQuery_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
connection.Close()
End Sub
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Dim SqlWhere As String = ""
Dim subSqlWhere As String = ""
'判断是否勾选了查询条件
Dim checkCondition As Boolean = False
'构建查询的where语句
'1、货物名称
If ckGoodsName.Checked = True Then
checkCondition = True
If txtGoodsName.Text.Trim = "" Then
MessageBox.Show("货物名称不能为空值")
Exit Sub
End If
SqlWhere = getGoodsName()
End If
'2、货物编号
If ckGoodsNo.Checked = True Then
checkCondition = True
If txtGoodsNo.Text = "" Then
MessageBox.Show("货物编号不能为空值")
Exit Sub
End If
subSqlWhere = getGoodsNo()
SqlWhere = IIf(SqlWhere = "", subSqlWhere, SqlWhere & " And " & subSqlWhere)
End If
'3、供应商
If ckSupplier.Checked = True Then
checkCondition = True
subSqlWhere = getSupplier()
SqlWhere = IIf(SqlWhere = "", subSqlWhere, SqlWhere & " And " & subSqlWhere)
End If
'4、库存量
If ckStock.Checked = True Then
checkCondition = True
subSqlWhere = getStock()
SqlWhere = IIf(SqlWhere = "", subSqlWhere, SqlWhere & " And " & subSqlWhere)
End If
'5、货物类别
If ckGoodsType.Checked = True Then
checkCondition = True
subSqlWhere = getGoodsType()
SqlWhere = IIf(SqlWhere = "", subSqlWhere, SqlWhere & " And " & subSqlWhere)
End If
If checkCondition = False Then
MessageBox.Show("必须勾选一个查询条件")
Exit Sub
End If
'关闭此窗口
Dim F_StorageInfo As FormStorageInfo
F_StorageInfo = Me.Owner
F_StorageInfo.customWhere = SqlWhere
Me.Close()
End Sub
#Region "设置各个条件下的查询语句"
'注意:查询条件在使用like时
'在Access中,应该使用*
'在vb中,应该使用%
Private Function getGoodsName() As String
Return "(货物信息.产品名称 like '%" & txtGoodsName.Text.Trim & "%')"
End Function
Private Function getGoodsNo() As String
Return "(货物信息.产品编号 like '%" & txtGoodsNo.Text.Trim & "%')"
End Function
Private Function getSupplier() As String
Return "(货物信息.供应商ID=" & arrSupplier(cbSupplier.SelectedIndex) & ")"
End Function
Private Function getStock() As String
Select Case cbStockCondition.Text
Case "大于"
Return "(货物信息.库存量>" & nudStock.Value & ")"
Case "等于"
Return "(货物信息.库存量=" & nudStock.Value & ")"
Case Else
Return "(货物信息.库存量<" & nudStock.Value & ")"
End Select
End Function
Private Function getGoodsType() As String
Return "(货物类别.类别ID=" & arrGoodsType(cbGoodsType.SelectedIndex) & ")"
End Function
#End Region
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Dim F_StorageInfo As FormStorageInfo
F_StorageInfo = Me.Owner
F_StorageInfo.customWhere = ""
Me.Close()
End Sub
End Class
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供的参考。
学习更多vb.net知识,请参看 vb.net 教程 目录