一直是用C#的,真没想到第一个vb.net的程序是为你写的。
在贴代码以前我想告诉你一句话,我是第一次使用vb.net,要说水平肯定不如你,我也是摸索着写的,
希望你以后遇到问题也能如此,这样对你的水平提高肯定会有帮助。
另外告诉你我不是“断线的风筝”,其实名字真的无所谓,但我希望得到别人的尊重。
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Dim cn1 As OleDbConnection
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Dim recIndex As Integer
#Region " Windows 窗体设计器生成的代码 "
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strConn As String
'连接数据库
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\C#\TestDBPicture\abc.mdb;Persist Security Info=False;"
cn1 = New OleDbConnection(strConn)
cn1.Open()
'打开表并填充DataSet
Dim strSql As String
strSql = "select * from usertag"
da = New OleDbDataAdapter(strSql, cn1)
da.Fill(ds, "aaa")
'如果有记录则显示第一条
If ds.Tables(0).Rows.Count > 0 Then
recIndex = 0
ShowData(ds.Tables(0).Rows(0))
End If
End Sub
Private Sub ShowData(ByVal Row1 As DataRow)
'如果这一行不为空则显示数据,否则清空文本框和图片
If Not (Row1 Is Nothing) Then
Me.TextBox2.Text = Row1("username")
Me.TextBox3.Text = Row1("userage")
ShowPicture(Row1) '调用过程,显示图片
Else
Me.TextBox2.Text = ""
Me.TextBox3.Text = ""
Me.PictureBox1.Image = Nothing
End If
Me.Text = ds.Tables(0).Rows.Count
End Sub
Private Sub ShowPicture(ByVal Row1 As DataRow)
Dim strPic As String
Try
PictureBox1.Image = Nothing '清空图片框
'如果临时文件已经存在,则删除
strPic = "D:\C#\TestDBPicture\temp.bmp"
If File.Exists(strPic) Then
File.Delete(strPic)
End If
'从图片字段读数据,并保存为临时文件
Dim data As Byte() = Row1("userpicture")
Dim myfilestream As New System.IO.FileStream(strPic, IO.FileMode.CreateNew)
myfilestream.Write(data, 0, data.Length)
'从文件流中读数据,并显示在图片框中
PictureBox1.Image = Bitmap.FromStream(myfilestream)
myfilestream.Close()
Catch ee As Exception
'MessageBox.Show(ee.Message)
End Try
End Sub
Private Sub SaveData()
Dim row1 As DataRow
'在数据集中添加一个新行()
row1 = ds.Tables(0).NewRow()
row1("username") = Me.TextBox2.Text
row1("userage") = Val(Me.TextBox3.Text)
row1("userpicture") = SavePicture()
ds.Tables(0).Rows.Add(row1)
'创建一个Command,用于提交更新
Dim strSql As String
Dim cmd As OleDbCommand
strSql = "insert into usertag (username,userage,userpicture) values (?,?,?)"
cmd = cn1.CreateCommand()
cmd.CommandText = strSql
'创建一个带参数的查询,用于提交更新
Dim pc As OleDbParameterCollection
pc = cmd.Parameters
pc.Add("username", OleDbType.BSTR)
pc.Add("userage", OleDbType.Integer)
pc.Add("userpicture", OleDbType.Binary)
pc("username").Value = Me.TextBox2.Text
pc("userage").Value = Val(Me.TextBox3.Text)
pc("userpicture").Value = SavePicture()
'执行插入
cmd.ExecuteNonQuery()
MsgBox("添加完成")
End Sub
Private Function SavePicture() As Byte()
Dim myfilestream As System.IO.FileStream
Dim data() As Byte
Dim strPic As String
'将图片框的图片保存为临时文件
strPic = Application.StartupPath & "\temp.bmp"
PictureBox1.Image.Save(strPic)
'从临时文件读数据,并返回
myfilestream = New System.IO.FileStream(strPic, IO.FileMode.Open)
ReDim data(myfilestream.Length - 1)
myfilestream.Read(data, 0, myfilestream.Length)
myfilestream.Close()
SavePicture = data
End Function
Private Sub ButtonOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOpen.Click
Dim a As OpenFileDialog
'打开一个图片,并显示在图片框中
a = New OpenFileDialog
a.Filter = "*.bmp *.jpg|*.jpg;*.bmp"
a.ShowDialog()
If a.FileName <> "" Then
PictureBox1.Image = New Bitmap(a.FileName)
End If
End Sub
Private Sub ButtonPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPrev.Click
'如果显示的不是第一个,则显示第一条记录
If recIndex > 0 Then
recIndex = recIndex - 1
ShowData(ds.Tables(0).Rows(recIndex))
End If
End Sub
Private Sub ButtonNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNext.Click
'如果显示的不是最后一个,则显示下一条记录
If recIndex < ds.Tables(0).Rows.Count - 1 Then
recIndex = recIndex + 1
ShowData(ds.Tables(0).Rows(recIndex))
End If
End Sub
Private Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click
'调用过程保存数据(添加一条记录)
SaveData()
End Sub
End Class