-
vb.net 教程 20-4 库存管理系统3.15 供应商管理(FormSupplier)
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
关于《Visual Basic.Net 循序渐进》请到百度网盘下载,具体下载地址:
链接:https://pan.baidu.com/s/1IfaLvlklx-nT4KK4VKZuIw
提取码:ip5n
全部代码如下:
Imports System.ComponentModel
Imports System.Data.OleDb
Public Class FormSupplier
Dim connection As OleDbConnection
Private Sub FormSupplier_Load(sender As Object, e As EventArgs) Handles MyBase.Load
connection = New OleDbConnection(databaseConnString)
'打开数据连接
connection.Open()
Call fillLv()
End Sub
'填充数据选项,主要是 lvUser
Private Sub fillLv()
lvSupplier.Items.Clear()
'新建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
Dim lvi As New ListViewItem(odReader.GetValue(0).ToString)
lvi.SubItems.Add(odReader.GetValue(1))
lvi.SubItems.Add(odReader.GetValue(2))
lvi.SubItems.Add(odReader.GetValue(3))
lvi.SubItems.Add(odReader.GetValue(4))
lvi.SubItems.Add(odReader.GetValue(5))
lvi.SubItems.Add(odReader.GetValue(6))
lvSupplier.Items.Add(lvi)
Loop
End If
odReader.Close()
'关闭数据读取器
odReader.Close()
End Sub
'点击lvGoods中的项目,将数据添加到对应位置
Private Sub lvSupplier_MouseClick(sender As Object, e As MouseEventArgs) Handles lvSupplier.MouseClick
Dim lvhti As ListViewHitTestInfo = lvSupplier.HitTest(e.X, e.Y)
If IsNothing(lvhti.Item) Then
Exit Sub
End If
txtName.Text = lvhti.Item.SubItems(1).Text
txtContacts.Text = lvhti.Item.SubItems(2).Text
txtJob.Text = lvhti.Item.SubItems(3).Text
txtAddr.Text = lvhti.Item.SubItems(4).Text
txtTel.Text = lvhti.Item.SubItems(5).Text
End Sub
'增加供应商信息
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.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
'供应商公司名称
Dim supplierName As String = txtName.Text.Trim
'检查是否已经存在该供应商
For i As Integer = 0 To lvSupplier.Items.Count - 1
If supplierName = lvSupplier.Items(i).SubItems(1).Text Then
If MessageBox.Show("该供应商已经存在,是否继续添加?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
Exit Sub
End If
End If
Next
'供应商联系人姓名
Dim supplierContacts As String = txtContacts.Text.Trim
'供应商联系人职务
Dim supplierJob As String = txtJob.Text.Trim
'供应商地址
Dim supplierAddr As String = txtAddr.Text.Trim
'供应商电话号码
Dim supplierTel As String = txtTel.Text.Trim
Dim sqlString As String
sqlString = "insert into 供应商(公司名称,联系人姓名,联系人职务,地址,电话,是否停用) " &
"values('" & supplierName & "','" & supplierContacts & "','" & supplierJob & "','" & supplierAddr & "','" & supplierTel & "','否')"
command.CommandText = sqlString
'不管是新增还是修改,都不用返回值,所以使用ExecuteNonQuery。
command.ExecuteNonQuery()
'刷新列表中的数据
Call fillLv()
End Sub
'检查数据合法性
Private Function checkData() As String
If txtName.Text.Trim = "" Then
Return "供应商公司名称不能为空"
End If
If txtContacts.Text.Trim = "" Then
Return "供应商联系人姓名不能为空"
End If
If txtJob.Text.Trim = "" Then
Return "供应商联系人职务不能为空"
End If
If txtAddr.Text.Trim = "" Then
Return "供应商地址不能为空"
End If
If txtTel.Text.Trim = "" Then
Return "供应商电话号码不能为空"
End If
Return ""
End Function
'修改供应商信息
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
If lvSupplier.SelectedItems.Count < 1 Then
MessageBox.Show("未选择需要修改的数据")
Exit Sub
End If
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
'供应商ID
Dim supplierID As Integer = lvSupplier.SelectedItems(0).Text
'供应商公司名称
Dim supplierName As String = txtName.Text.Trim
'供应商联系人姓名
Dim supplierContacts As String = txtContacts.Text.Trim
'供应商联系人职务
Dim supplierJob As String = txtJob.Text.Trim
'供应商地址
Dim supplierAddr As String = txtAddr.Text.Trim
'供应商电话号码
Dim supplierTel As String = txtTel.Text.Trim
Dim sqlString As String
sqlString = "update 供应商 set 公司名称='" & supplierName &
"',联系人姓名='" & supplierContacts &
"',联系人职务='" & supplierJob &
"',地址='" & supplierAddr &
"',电话='" & supplierTel &
"',是否停用='否') where 供应商ID=" & supplierID
command.CommandText = sqlString
'不管是新增还是修改,都不用返回值,所以使用ExecuteNonQuery。
command.ExecuteNonQuery()
'刷新列表中的数据
Call fillLv()
End Sub
Private Sub FormSupplier_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
connection.Close()
End Sub
'停用供应商
'存在的数据通常已经关联了很多其它表的数据,因此,一般不要轻易设置删除
Private Sub btnIsUse_Click(sender As Object, e As EventArgs) Handles btnIsUse.Click
If lvSupplier.SelectedItems.Count < 1 Then
MessageBox.Show("未选择需要修改的数据")
Exit Sub
End If
'供应商ID
Dim supplierID As Integer = lvSupplier.SelectedItems(0).Text
'供应商公司名称
Dim supplierName As String = lvSupplier.SelectedItems(0).SubItems(1).Text
Dim isUseOld As String = lvSupplier.SelectedItems(0).SubItems(6).Text
Dim isUseNew As String = IIf(isUseOld = "否", "是", "否")
Dim operation As String = IIf(isUseOld = "否", "停用", "启用")
If MessageBox.Show("确实要" & operation & "供应商 " & supplierName & " 吗?", operation & "供应商", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) = DialogResult.Cancel Then
Exit Sub
End If
Dim command As New OleDbCommand()
command.Connection = connection
command.CommandText = "update 供应商 set 是否停用='" & isUseNew & "' where 供应商ID=" & supplierID
command.ExecuteNonQuery()
'刷新lvUser中的显示
Call fillLv()
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
Me.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/114224752
栏目列表
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比