-
vb.net 教程 20-4 库存管理系统3.8 库存货物操作(FormStorageInfoTable)
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
关于《Visual Basic.Net 循序渐进》请到百度网盘下载,具体下载地址:
链接:https://pan.baidu.com/s/1IfaLvlklx-nT4KK4VKZuIw
提取码:ip5n
需要强调的是,严格控制用户输入,这样可以减少很多代码。例如库存量、采购价格、销售价格使用NumericUpDown控件,而不采用TextBox控件。
全部代码如下:
Imports System.ComponentModel
Imports System.Data.OleDb
Public Class FormStorageInfoTable
'是否新增标志,如果是,设置为True;否则False
Dim isAdd As Boolean
'传入的入库单ID号,也是判断新增还是修改的依据
Dim orderId As Integer
Dim connection As OleDbConnection
'修改入库清单中的物品时设置标记
Dim isEditGoods As Boolean = False
Dim arrSupplier As List(Of Integer)
Dim arrGoodsType As List(Of Integer)
'如果是修改原库存物
'键值对分别保存 货物ID和对应数量 ,需要在货物信息表(库存)中进行增删
'原入库单中的
Dim dicGoodsInfoOld As Dictionary(Of Integer, Integer)
'修改后入库单中的
Dim dicGoodsInfoNew As Dictionary(Of Integer, Integer)
'根据传入的入库单号进行确认操作
'如果入库号为0,那么新增
'否则,修改
Public Sub New(ByVal OrderId As Integer)
' 此调用是设计器所必需的。
InitializeComponent()
' 在 InitializeComponent() 调用之后添加任何初始化。
Me.orderId = OrderId
If Me.orderId = 0 Then isAdd = True Else isAdd = False
End Sub
Private Sub FormStorageInfoTable_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()
Call drawControls()
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 drawControls()
If isAdd = True Then
Call EnabledControls()
Else
'如果是修改数据,那么填充所有控件中的数据
'新建OleDbCommand对象实例
Dim command As New OleDbCommand()
'=========填充lvGoodsType==================
'要执行的SQL查询
command.CommandText = "select 产品名称,单位数量,库存量,产品编号,采购价格,销售价格,供应商ID,类别ID from 货物信息 where 产品ID=" & orderId
'设置OleDbCommand的数据连接为OleDbConnection
command.Connection = connection
'声明OleDbDataReader对象
Dim odReader As OleDbDataReader
'通过OleDbCommand的ExecuteReader方法获得OleDbDataReader对象实例。
odReader = command.ExecuteReader(CommandBehavior.SingleRow)
odReader.Read()
'省略了检查数据记录是否有效
txtName.Text = odReader.GetValue(0)
txtInfo.Text = odReader.GetValue(1)
nudStock.Value = odReader.GetValue(2)
txtGoodsNo.Text = odReader.GetValue(3)
nudBuyPrice.Value = odReader.GetValue(4)
nudSalePrice.Value = odReader.GetValue(5)
'供应商
Dim supplier As Integer = CType(odReader.GetValue(6), Integer)
For i As Integer = 0 To arrSupplier.Count - 1
If supplier = arrSupplier(i) Then
cbSupplier.SelectedIndex = i
Exit For
End If
Next
'货物类别
Dim goodsType As Integer = CType(odReader.GetValue(7), Integer)
For i As Integer = 0 To arrGoodsType.Count - 1
If goodsType = arrGoodsType(i) Then
cbGoodsType.SelectedIndex = i
Exit For
End If
Next
odReader.Close()
Call UnabledControls()
End If
End Sub
'如果是新建,则允许控件操作
Private Sub 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
End Sub
'如果是修改,初始不允许控件操作
Private Sub 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
End Sub
'修改数据
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
'按下后允许修改数据
Call EnabledControls()
End Sub
'保存数据
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim errMsg As String = checkData()
If errMsg <> "" Then
MessageBox.Show(errMsg)
Exit Sub
End If
'新建OleDbCommand对象实例
Dim command As New OleDbCommand()
'设置OleDbCommand的数据连接为OleDbConnection
command.Connection = connection
'保存数据,分两种情况
'新增或修改
If isAdd = True Then
'1、将货物信息添加到数据库中
'新增的SQL语句
command.CommandText = getAddSql()
'不管是新增还是修改,都不用返回值,所以使用ExecuteNonQuery。
command.ExecuteNonQuery()
'2、新增需要返回此次货物对应的编号
command.CommandText = "Select top 1 产品ID from 货物信息 order by 产品ID desc"
Dim odReader As OleDbDataReader
odReader = command.ExecuteReader(CommandBehavior.SingleResult)
odReader.Read()
'记录刚建的出库单ID号
Dim newGoodsID As Integer = odReader.GetInt32(0)
odReader.Close()
'当前货物ID
orderId = newGoodsID
'设置标志为修改
isAdd = False
Else
'修改的SQL语句
command.CommandText = getEditSql()
'不管是新增还是修改,都不用返回值,所以使用ExecuteNonQuery。
command.ExecuteNonQuery()
End If
'保存之后禁止编辑数据
Call UnabledControls()
End Sub
'检查数据合法性
Private Function checkData() As String
If txtName.Text.Trim = "" Then
Return "买家姓名不能为空"
End If
If txtInfo.Text.Trim = "" Then
Return "货物单位数量不能为空"
End If
If txtGoodsNo.Text.Trim = "" Then
Return "货物编号不能为空"
End If
Return ""
End Function
'新增时候插入出库单详细使用的sql语句
Private Function getAddSql() As String
'货物名称
Dim goodsName As String = txtName.Text.Trim
'单位数量
Dim goodsInfo As String = txtInfo.Text
'库存量
Dim Stock As String = nudStock.Value
'货物编号
Dim goodsNo As String = txtGoodsNo.Text
'采购价格
Dim BuyPrice As String = nudBuyPrice.Value
'销售价格
Dim SalePrice As String = nudSalePrice.Value
'供应商
Dim Supplier As Integer = arrSupplier(cbSupplier.SelectedIndex)
'货物类别
Dim GoodsType As String = arrGoodsType(cbGoodsType.SelectedIndex)
Dim sqlString As String
sqlString = "insert into 货物信息(产品名称,产品编号,供应商ID,类别ID,单位数量,采购价格,销售价格,库存量,是否删除) " &
"values('" & goodsName & "','" & goodsNo & "'," & Supplier & "," & GoodsType & ",'" & goodsInfo & "'," &
BuyPrice & "," & SalePrice & "," & Stock & ",'否')"
Return sqlString
End Function
'修改时候出库单详细使用的sql语句
Private Function getEditSql() As String
'货物名称
Dim goodsName As String = txtName.Text.Trim
'单位数量
Dim goodsInfo As String = txtInfo.Text
'库存量
Dim Stock As String = nudStock.Value
'货物编号
Dim goodsNo As String = txtGoodsNo.Text
'采购价格
Dim BuyPrice As String = nudBuyPrice.Value
'销售价格
Dim SalePrice As String = nudSalePrice.Value
'供应商
Dim Supplier As Integer = arrSupplier(cbSupplier.SelectedIndex)
'货物类别
Dim GoodsType As String = arrGoodsType(cbGoodsType.SelectedIndex)
Dim sqlString As String
sqlString = "update 货物信息 set 产品名称='" & goodsName & "',产品编号='" & goodsNo & "',供应商ID=" & Supplier & ",类别ID=" & GoodsType &
",单位数量='" & goodsInfo & "',采购价格=" & BuyPrice & ",销售价格=" & SalePrice &
" where 产品ID=" & orderId
Return sqlString
End Function
'按下关闭按钮
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
If btnSave.Enabled = True Then
If MessageBox.Show("数据未保存,是否退出?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) = DialogResult.OK Then
Me.Close()
End If
Else
Me.Close()
End If
End Sub
Private Sub FormStorageInfoTable_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
connection.Close()
End Sub
End Class
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供的参考。
————————————————
版权声明:本文为CSDN博主「VB.Net」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/UruseiBest/article/details/114224458
栏目列表
最新更新
Python获取微信好友数据
Python 的排序方法 sort 和 sorted 的区别
WinForm轻松实现自定义分页 (转载)
Mac系统下使用brew搭建PHP(LNMP/LAMP)开发环境
在win系统安装配置 Memcached for PHP 5.3 图文
箱图在数据预处理中的应用
Python WEB开发:用Tornado框架制作简易【表
自从学会了用python解析视频,都不用去找
Python代码阅读(第50篇):对列表间隔取
Python爬虫+数据分析+可视化展示,分析《
MongoDB常用命令(2)
MongoDB基本介绍与安装(1)
SQLServer触发器调用JavaWeb接口
SQL Server索引的原理深入解析
SqlServer2016模糊匹配的三种方式及效率问题
SQL中Truncate的用法
sqlserver 多表关联时在where语句中慎用tri
链接服务器读取Mysql---出现消息 7347,级别
SQL Server解惑——为什么你拼接的SQL语句换
MySQL视图了解一下
php 判断常量、变量和函数是否存在
php if..else 循环
PHP中foreach循环详解
php foreach用法和实例
php for的高级运用
php foreach循环用法介绍
PHP条件语句(if else/switch)语法与示例
PHP 循环语句基本语法结构笔记
PHP 条件语句基本语法结构
php break跳出多重循环实例