虽然VB.NET已经被微软停更了,但作为一门出色的编程语言,不当误事,还是很多人在使用。今天开始,重温一下VB.NET。
写点什么呢?才能假装很厉害的样子呢。
嗯,写个对数据库操作吧
那么让我们开始吧
首先,新建一个access数据库,创建表,设置好字段和对应的数据类型。
好了,现在打开VS软件,新建一个windows窗体应用项目,然后像我这样把控件摆放好。
给大家介绍一下,我在窗体上下放置了两个GroupBox容器,下面GroupBox上利用SplitContainer容器将区域分为三部分,左边放置listview控件,中间和右边放置两个PictureBox控件。
现在让我们开始码代码吧
首先双击窗体,开始添加Form1_Load代码:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim conn As New OleDbConnection
Dim cmd As New OleDbCommand
Dim reader As OleDbDataReader = Nothing
Dim lv As ListViewItem
Dim sql As String
sql = "select 编号,名称,时间 from 测试"
conn.ConnectionString = "provider=microsoft.ace.oledb.12.0;data source=C:\Users\CY\Desktop\测试.accdb"
cmd.CommandText = sql
cmd.Connection = conn
Try
conn.Open()
reader = cmd.ExecuteReader
While reader.Read
lv = New ListViewItem(reader.GetValue(0).ToString)
lv.SubItems.AddRange({reader.GetString(1), reader.GetDateTime(2)})
ListView1.Items.Add(lv)
End While
Catch ex As Exception
MessageBox.Show("出现异常" & ex.Message)
Finally
reader.Close()
conn.Close()
End Try
End Sub
下面开始添加新增数据功能,需要再新建一个窗体,激不激动现在有两个窗体了,摆好控件,像我这样,相关控件属性按自己喜好自行设置就好。
双击Form1中新增(Button3)按钮
进入Button3_Click(按钮点击事件),添加如下代码,就可以打开Form2(新增)窗体了。
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Form2.ShowDialog()
End Sub
然后就可以在Form2.vb里添加代码了,老样子双击Form2窗体,在Form2_Load中添加代码:
根据需要在class Form2内设置全局变量,因为会在其他事件里会用到。
Public Class Form2
Dim NumText As Integer
Dim i, j As Integer
Dim ImgNam1 As String
Dim ImgNam2 As String
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim conn As New OleDbConnection
Dim cmd As New OleDbCommand()
Dim reader As OleDbDataReader
Dim sql As String
PictureBox1.Image = Nothing
PictureBox2.Image = Nothing
TextBox2.Text = ""
TextBox3.Text = ""
conn.ConnectionString = "provider=microsoft.ace.oledb.12.0;data source=C:\Users\CY\Desktop\测试.accdb"
sql = "select MAX(编号) from 测试"
cmd.CommandText = sql
cmd.Connection = conn
reader = Nothing
Try
conn.Open()
reader = cmd.ExecuteReader()
If reader.Read Then
If reader.GetValue(0).ToString = "" Then
NumText = 0
Else
NumText = reader.GetValue(0)
End If
End If
Catch ex As Exception
MessageBox.Show("出现异常" & ex.Message)
Finally
reader.Close()
conn.Close()
End Try
TextBox1.Text = NumText + 1
这样就顺利自动生成编号了。
接下来我们要双击浏览(Button3)按钮,进入Button3_Click(按钮点击事件)为其添加代码:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim ofd As New OpenFileDialog
ofd.Filter = "所有图片|*.png;*.jpg;*.gif"
If ofd.ShowDialog() = DialogResult.OK Then
i = i + 1
If i = 3 Then
i = 1
j = 1
End If
Else
Return
End If
If i = 1 Then
PictureBox1.Image = Image.FromFile(ofd.FileName)
TextBox3.Text = ofd.FileName
ImgNam1 = ofd.FileName
ElseIf i = 2 Then
PictureBox2.Image = Image.FromFile(ofd.FileName)
TextBox3.Text = ofd.FileName
ImgNam2 = ofd.FileName
End If
End Sub
完成上述代码的效果如下:
接下来我们要双击取消(Button2)按钮,进入Button2_Click(按钮点击事件)为其添加代码:
这个简单。
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
然后就是双击保存(Button1)按钮,进入Button1_Click(按钮点击事件)为其添加代码:
Dim conn As New OleDbConnection
Dim cmd As New OleDbCommand()
Dim reader As OleDbDataReader
Dim TimText As String
Dim NamText As String
Dim sql As String
Dim RecordCount As Integer
Dim ms As New IO.MemoryStream
Dim ImgBuff() As Byte
TimText = Mid(DateTimePicker1.Value, 1, InStr(DateTimePicker1.Value, " ") - 1)
NamText = TextBox2.Text.Trim
If TextBox2.Text = "" Or TextBox3.Text = "" Or (i = 1 And j <> 1) Then
MessageBox.Show("取证信息填写不全")
Return
End If
conn.ConnectionString = "provider=microsoft.ace.oledb.12.0;data source=C:\Users\CY\Desktop\测试.accdb"
sql = String.Format("insert into 测试 (编号,名称,图片1,图片2,时间) values({0},'{1}',@imgType1,@imgType2,'{2}')", NumText + 1, NamText, TimText)
cmd.CommandText = sql
cmd.Connection = conn
Try
conn.Open()
ImgBuff = My.Computer.FileSystem.ReadAllBytes(ImgNam1)
cmd.Parameters.Add("@imgType1", OleDbType.LongVarBinary)
cmd.Parameters("@imgType1").Value = ImgBuff
ImgBuff = My.Computer.FileSystem.ReadAllBytes(ImgNam2)
cmd.Parameters.Add("@imgType2", OleDbType.LongVarBinary)
cmd.Parameters("@imgType2").Value = ImgBuff
RecordCount = cmd.ExecuteNonQuery()
If RecordCount > 0 Then
sql = "select MAX(编号) from 测试"
cmd.CommandText = sql
reader = Nothing
Try
reader = cmd.ExecuteReader()
If reader.Read Then
NumText = reader.GetValue(0)
End If
Catch ex As Exception
MessageBox.Show("出现异常" & ex.Message)
Finally
reader.Close()
End Try
MessageBox.Show("新增信息完成")
TextBox1.Text = NumText + 1
TextBox2.Text = ""
TextBox3.Text = ""
End If
Catch ex As Exception
MessageBox.Show("出现异常" & ex.Message)
Finally
conn.Close()
End Try
PictureBox1.Image = Nothing
PictureBox2.Image = Nothing
i = 0
j = 0
End Sub
点击保存按钮,成功会弹出提示框:
再来看看数据库里有没有新增数据。
成功了。
当新增(Form2)窗体关闭时,我们要更新Form1中listview控件。这时候我们用到Form2的FormClosed事件,在Form2_FormClosed里添加代码:
Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
Dim conn As New OleDbConnection
Dim cmd As New OleDbCommand()
Dim reader As OleDbDataReader
Dim sql As String
Dim lv As ListViewItem
i = 0
Form1.ListView1.Items.Clear()
conn.ConnectionString = "provider=microsoft.ace.oledb.12.0;data source=C:\Users\CY\Desktop\测试.accdb"
sql = "select 编号,名称,时间 from 测试 order by 编号"
cmd.CommandText = sql
cmd.Connection = conn
reader = Nothing
Try
conn.Open()
reader = cmd.ExecuteReader()
While reader.Read
lv = New ListViewItem(reader.GetValue(0).ToString)
lv.SubItems.AddRange({reader.GetString(1), reader.GetDateTime(2)})
Form1.ListView1.Items.Add(lv)
End While
Catch ex As Exception
MessageBox.Show("出现异常" & ex.Message)
Finally
reader.Close()
conn.Close()
End Try
End Sub
下面我们要做的是当点击listview里的项时,显示图片。就要使用到listview中的SelectedIndexChanged事件,添加代码。
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
Dim conn As New OleDbConnection
Dim cmd As New OleDbCommand()
Dim reader As OleDbDataReader
Dim sql As String
Dim Num As String
Dim ImgData1() As Byte = Nothing
Dim ImgData2() As Byte = Nothing
Dim ms As IO.MemoryStream
Dim Img As Image
If ListView1.SelectedItems.Count <> 0 Then
Num = ListView1.SelectedItems.Item(0).Text
conn.ConnectionString = "provider=microsoft.ace.oledb.12.0;data source=C:\Users\CY\Desktop\测试.accdb"
sql = String.Format("select 图片1,图片2 from 测试 where 编号={0}", Num)
cmd.CommandText = sql
cmd.Connection = conn
reader = Nothing
Try
conn.Open()
reader = cmd.ExecuteReader()
If reader.Read Then
ImgData1 = reader(0)
ImgData2 = reader(1)
End If
ms = New IO.MemoryStream(ImgData1)
Img = Image.FromStream(ms)
PictureBox1.Image = Img
ms = New IO.MemoryStream(ImgData2)
Img = Image.FromStream(ms)
PictureBox2.Image = Img
ms.Close()
Catch ex As Exception
MessageBox.Show("出现异常" & ex.Message)
Finally
reader.Close()
conn.Close()
End Try
End If
End Sub
最终效果是这样的。
注意喽,文中没有提控件的属性的设置。