版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的
图99A-24库存查询窗口设计
库存查询提供了5种方式的组合查询,每种方式查询语句应该符合库存信息(FormStorageInfo)代码中的sql语句。
例如,以下语句是对产品名称的模糊查询,使用like语句:
(货物信息.产品名称 like '%" + txtGoodsName.Text.Trim() + "%')
需要注意的是:在Access中执行模糊查询,通配符使用*(星号),而在vb中,应该使用%(百分号)。
使用多个查询条件时,其间使用 And 组合。例如以下语句判断前一查询条件是否为空,如果不为空则使用 and 连接。
SqlWhere = SqlWhere == "" ? subSqlWhere : SqlWhere + " And " + subSqlWhere;
另外,为了严格控制用户输入,通常情况下,需要将ComboBox的DropDownStyle属性设置为DropDownList。
全部代码如下:
OleDbConnection connection;
List<int> arrSupplier;
List<int> arrGoodsType;
private void FormStorageInfoQuery_Load(object sender, EventArgs e)
{
arrSupplier = new List<int>();
arrGoodsType = new List<int>();
connection = new OleDbConnection(classMod.databaseConnString);
//打开数据连接
connection.Open();
fillControls();
cbStockCondition.SelectedIndex = 0;
}
//填充数据选项,主要是 cbSupplier 和 cbGoodsType
private void fillControls()
{
//新建OleDbCommand对象实例
OleDbCommand command = new OleDbCommand();
//=========填充供应商选择框==================
//要执行的SQL查询
command.CommandText = "select 供应商ID,公司名称 from 供应商";
//设置OleDbCommand的数据连接为OleDbConnection
command.Connection = connection;
//声明OleDbDataReader对象
OleDbDataReader odReader;
//通过OleDbCommand的ExecuteReader方法获得OleDbDataReader对象实例。
odReader = command.ExecuteReader();
//如果OleDbDataReader中包含数据
if (odReader.HasRows)
{
//循环读取每一行数据,直到Read方法返回false
while (odReader.Read())
{
arrSupplier.Add((int)odReader.GetValue(0));
cbSupplier.Items.Add(odReader.GetString(1));
}
}
odReader.Close();
//==========填充货物类别选择框===================
//要执行的SQL查询
command.CommandText = "select * from 货物类别";
//通过OleDbCommand的ExecuteReader方法获得OleDbDataReader对象实例。
odReader = command.ExecuteReader();
//如果OleDbDataReader中包含数据
if (odReader.HasRows)
{
//循环读取每一行数据,直到Read方法返回False
while (odReader.Read())
{
arrGoodsType.Add((int)odReader.GetValue(0));
cbGoodsType.Items.Add(odReader.GetValue(1));
}
}
//关闭数据读取器
odReader.Close();
cbSupplier.SelectedIndex = 0;
cbGoodsType.SelectedIndex = 0;
}
private void FormStorageInfoQuery_FormClosing(object sender, FormClosingEventArgs e)
{
connection.Close();
}
private void btnOk_Click(object sender, EventArgs e)
{
string SqlWhere = "";
string subSqlWhere = "";
//判断是否勾选了查询条件
Boolean checkCondition = false;
//构建查询的where语句
//1、货物名称
if (ckGoodsName.Checked)
{
checkCondition = true;
if (txtGoodsName.Text.Trim() == "")
{
MessageBox.Show("货物名称不能为空值");
return;
}
SqlWhere = getGoodsName();
}
//2、货物编号
if (ckGoodsNo.Checked)
{
checkCondition = true;
if (txtGoodsNo.Text == "")
{
MessageBox.Show("货物编号不能为空值");
return;
}
subSqlWhere = getGoodsNo();
SqlWhere = SqlWhere == "" ? subSqlWhere : SqlWhere + " And " + subSqlWhere;
}
//3、供应商
if (ckSupplier.Checked)
{
checkCondition = true;
subSqlWhere = getSupplier();
SqlWhere = SqlWhere == "" ? subSqlWhere : SqlWhere + " And " + subSqlWhere;
}
//4、库存量
if (ckStock.Checked == true)
{
checkCondition = true;
subSqlWhere = getStock();
SqlWhere = SqlWhere == "" ? subSqlWhere : SqlWhere + " And " + subSqlWhere;
}
//5、货物类别
if (ckGoodsType.Checked)
{
checkCondition = true;
subSqlWhere = getGoodsType();
SqlWhere = SqlWhere == "" ? subSqlWhere : SqlWhere + " And " + subSqlWhere;
}
if (checkCondition == false)
{
MessageBox.Show("必须勾选一个查询条件");
return;
}
//关闭此窗口
FormStorageInfo F_StorageInfo;
F_StorageInfo = (FormStorageInfo)this.Owner;
F_StorageInfo.customWhere = SqlWhere;
this.Close();
}
#region "设置各个条件下的查询语句"
//注意:查询条件在使用like时
//在Access中,应该使用*
//在vb中,应该使用%
private string getGoodsName()
{
return "(货物信息.产品名称 like '%" + txtGoodsName.Text.Trim() + "%')";
}
private string getGoodsNo()
{
return "(货物信息.产品编号 like '%" + txtGoodsNo.Text.Trim() + "%')";
}
private string getSupplier()
{
return "(货物信息.供应商ID=" + arrSupplier[cbSupplier.SelectedIndex] + ")";
}
private string getStock()
{
switch (cbStockCondition.Text)
{
case "大于":
return "(货物信息.库存量>" + nudStock.Value + ")";
case "等于":
return "(货物信息.库存量=" + nudStock.Value + ")";
default:
return "(货物信息.库存量<" + nudStock.Value + ")";
}
}
private string getGoodsType()
{
return "(货物类别.类别ID=" + arrGoodsType[cbGoodsType.SelectedIndex] + ")";
}
#endregion
private void btnCancel_Click(object sender, EventArgs e)
{
FormStorageInfo F_StorageInfo;
F_StorageInfo = (FormStorageInfo)this.Owner;
F_StorageInfo.customWhere = "";
this.Close();
}
学习更多vb.net知识,请参看vb.net 教程 目录
学习更多C#知识,请参看C#教程 目录