版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的
图99A-25 库存货物操作窗口设计
需要强调的是,严格控制用户输入,这样可以减少很多代码。例如库存量、采购价格、销售价格使用NumericUpDown控件,而不采用TextBox控件。
全部代码如下:
//是否新增标志,如果是,设置为True;否则False
Boolean isAdd;
//传入的入库单ID号,也是判断新增还是修改的依据
int orderId;
OleDbConnection connection;
//修改入库清单中的物品时设置标记
Boolean isEditGoods = false;
List<int> arrSupplier;
List<int> arrGoodsType;
//如果是修改原库存物
//键值对分别保存 货物ID和对应数量 ,需要在货物信息表(库存)中进行增删
//原入库单中的
Dictionary<int, int> dicGoodsInfoOld;
//修改后入库单中的
Dictionary<int, int> dicGoodsInfoNew;
//根据传入的入库单号进行确认操作
//如果入库号为0,那么新增
//否则,修改
public FormStorageInfoTable(int OrderId)
{
InitializeComponent();
// 在 InitializeComponent() 调用之后添加任何初始化。
this.orderId = OrderId;
if (this.orderId == 0)
isAdd = true;
else
isAdd = false;
}
private void FormStorageInfoTable_Load(object sender, EventArgs e)
{
arrSupplier = new List<int>();
arrGoodsType = new List<int>();
connection = new OleDbConnection(classMod.databaseConnString);
//打开数据连接
connection.Open();
fillControls();
drawControls();
}
//填充数据选项,主要是 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.GetValue(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 drawControls()
{
if (isAdd == true)
{
EnabledControls();
}
else
{
//如果是修改数据,那么填充所有控件中的数据
//新建OleDbCommand对象实例
OleDbCommand command = new OleDbCommand();
//=========填充lvGoodsType==================
//要执行的SQL查询
command.CommandText = "select 产品名称,单位数量,库存量,产品编号,采购价格,销售价格,供应商ID,类别ID from 货物信息 where 产品ID=" + orderId;
//设置OleDbCommand的数据连接为OleDbConnection
command.Connection = connection;
//声明OleDbDataReader对象
OleDbDataReader odReader;
//通过OleDbCommand的ExecuteReader方法获得OleDbDataReader对象实例。
odReader = command.ExecuteReader(CommandBehavior.SingleRow);
odReader.Read();
//省略了检查数据记录是否有效
txtName.Text = odReader.GetValue(0).ToString();
txtInfo.Text = odReader.GetValue(1).ToString();
string b=odReader.GetValue(2).ToString();
nudStock.Value = odReader.GetInt16(2);
txtGoodsNo.Text = odReader.GetValue(3).ToString();
nudBuyPrice.Value = odReader.GetDecimal(4);
nudSalePrice.Value = odReader.GetDecimal(5);
//供应商
int supplier = (int)odReader.GetValue(6);
for (int i = 0; i < arrSupplier.Count; i++)
if (supplier == arrSupplier[i])
{
cbSupplier.SelectedIndex = i;
return;
}
//货物类别
int goodsType = (int)odReader.GetValue(7);
for (int j = 0; j < arrGoodsType.Count; j++)
{
cbGoodsType.SelectedIndex = j;
return;
}
odReader.Close();
UnabledControls();
}
}
//如果是新建,则允许控件操作
private void EnabledControls()
{
txtName.Enabled = true;
txtInfo.Enabled = true;
nudStock.Enabled = true;
txtGoodsNo.Enabled = true;
nudBuyPrice.Enabled = true;
nudSalePrice.Enabled = true;
cbSupplier.Enabled = true;
cbGoodsType.Enabled = true;
btnSave.Enabled = true;
btnEdit.Enabled = false;
btnClose.Enabled = true;
}
//如果是修改,初始不允许控件操作
private void UnabledControls()
{
txtName.Enabled = false;
txtInfo.Enabled = false;
nudStock.Enabled = false;
txtGoodsNo.Enabled = false;
nudBuyPrice.Enabled = false;
nudSalePrice.Enabled = false;
cbSupplier.Enabled = false;
cbGoodsType.Enabled = false;
btnSave.Enabled = false;
btnEdit.Enabled = true;
btnClose.Enabled = true;
}
//保存数据
private void btnSave_Click(object sender, EventArgs e)
{
string errMsg = checkData();
if (errMsg != "")
{
MessageBox.Show(errMsg);
return;
}
//新建OleDbCommand对象实例
OleDbCommand command = new OleDbCommand();
//设置OleDbCommand的数据连接为OleDbConnection
command.Connection = connection;
//保存数据,分两种情况
//新增或修改
if (isAdd == true)
{
//1、将货物信息添加到数据库中
//新增的SQL语句
command.CommandText = getAddSql();
//不管是新增还是修改,都不用返回值,所以使用ExecuteNonQuery。
command.ExecuteNonQuery();
//2、新增需要返回此次货物对应的编号
command.CommandText = "Select top 1 产品ID from 货物信息 order by 产品ID desc";
OleDbDataReader odReader;
odReader = command.ExecuteReader(CommandBehavior.SingleResult);
odReader.Read();
//记录刚建的出库单ID号
int newGoodsID = odReader.GetInt32(0);
odReader.Close();
//当前货物ID
orderId = newGoodsID;
//设置标志为修改
isAdd = false; }
else {
//修改的SQL语句
command.CommandText = getEditSql();
//不管是新增还是修改,都不用返回值,所以使用ExecuteNonQuery。
command.ExecuteNonQuery();
}
;
//保存之后禁止编辑数据
UnabledControls();
}
//修改数据
private void btnEdit_Click(object sender, EventArgs e)
{
//按下后允许修改数据
EnabledControls();
}
//检查数据合法性
private string checkData()
{
if (txtName.Text.Trim() == "")
return "买家姓名不能为空";
if (txtInfo.Text.Trim() == "")
return "货物单位数量不能为空";
if (txtGoodsNo.Text.Trim() == "")
return "货物编号不能为空";
return "";
}
//新增时候插入出库单详细使用的sql语句
private string getAddSql()
{
//货物名称
string goodsName = txtName.Text.Trim();
//单位数量
string goodsInfo = txtInfo.Text;
//库存量
string Stock = nudStock.Value.ToString();
//货物编号
string goodsNo = txtGoodsNo.Text;
//采购价格
string BuyPrice = nudBuyPrice.Value.ToString();
//销售价格
string SalePrice = nudSalePrice.Value.ToString();
//供应商
int Supplier = arrSupplier[cbSupplier.SelectedIndex];
//货物类别
string GoodsType = arrGoodsType[cbGoodsType.SelectedIndex].ToString();
string sqlString;
sqlString = "insert into 货物信息(产品名称,产品编号,供应商ID,类别ID,单位数量,采购价格,销售价格,库存量,是否删除) " +
"values('" + goodsName + "','" + goodsNo + "'," + Supplier + "," + GoodsType + ",'" + goodsInfo + "'," +
BuyPrice + "," + SalePrice + "," + Stock + ",'否')";
return sqlString;
}
//修改时候出库单详细使用的sql语句
private string getEditSql()
{
//货物名称
string goodsName = txtName.Text.Trim();
//单位数量
string goodsInfo = txtInfo.Text;
//库存量
string Stock = nudStock.Value.ToString();
//货物编号
string goodsNo = txtGoodsNo.Text;
//采购价格
string BuyPrice = nudBuyPrice.Value.ToString();
//销售价格
string SalePrice = nudSalePrice.Value.ToString();
//供应商
int Supplier = arrSupplier[cbSupplier.SelectedIndex];
//货物类别
string GoodsType = arrGoodsType[cbGoodsType.SelectedIndex].ToString();
string sqlString;
sqlString = "update 货物信息 set 产品名称='" + goodsName + "',产品编号='" + goodsNo + "',供应商ID=" + Supplier + ",类别ID=" + GoodsType +
",单位数量='" + goodsInfo + "',采购价格=" + BuyPrice + ",销售价格=" + SalePrice +
" where 产品ID=" + orderId;
return sqlString;
}
//按下关闭按钮
private void btnClose_Click(object sender, EventArgs e)
{
if (btnSave.Enabled == true)
{
if (MessageBox.Show("数据未保存,是否退出?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
this.Close();
}
else
this.Close();
}
private void FormStorageInfoTable_FormClosing(object sender, FormClosingEventArgs e)
{
connection.Close();
}
学习更多vb.net知识,请参看vb.net 教程 目录
学习更多C#知识,请参看C#教程 目录