'首先将数据读入dataset中
Dim ImgByt() As Byte
Dim ImgMS As IO.MemoryStream
ImgByt = CType(dataset.Tables("表名").Rows(0).Item("图片字段名"), Byte())
ImgMS = New IO.MemoryStream(ImgByt)
PictureBox1.image = Drawing.Image.FromStream(ImgMS)
Try
Dim strInsert As String
Dim myconn As OleDb.OleDbConnection
Dim mycmd As OleDb.OleDbCommand
myconn = New OleDb.OleDbConnection("Provider=SQLOLEDB;Data Source=qianzh;" & _
"Integrated Security=SSPI;Initial Catalog=employ")
myconn.Open()
strInsert = "INSERT INTO employees (employeeid, eName, title,deptno) VALUES ( '" & _
TxtemployeeID.Text & "','" & TxteName.Text & "','" & Txttitle.Text & "','" & Txtdeptno.Text & "')"
mycmd = New OleDb.OleDbCommand(strInsert, myconn)
'直接对数据库进行更新操作
mycmd.ExecuteNonQuery()
myconn.Close()
Dim adoConn As ADODB.Connection = New ADODB.Connection()
Dim mstream As ADODB.Stream
Dim rs As ADODB.Recordset = New ADODB.Recordset()
adoConn.Open("dsn=myServer;uid=;pwd=;")
rs.Open("SELECT * FROM employees where employeeid='" & TxtemployeeID.Text & "'", adoConn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
mstream = New ADODB.Stream()
mstream.Type = ADODB.StreamTypeEnum.adTypeBinary
mstream.Open()
Dim path As String
path = Txtpath.Text
mstream.LoadFromFile(path)
rs.Fields("image").Value = mstream.Read
rs.Update()
rs.Close()
adoConn.Close()
MessageBox.Show("成功保存!", "信息框", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Catch ex
MessageBox.Show("添加数据出错" & ex.ToString(), "错误")
End Try
'以下清空显示
TxtemployeeID.Text = ""
TxteName.Text = ""
Txttitle.Text = ""
Txtdeptno.Text = ""
Txtdescript.Text = ""
Txthiredate.Text = ""
Txtsal.Text = ""
Txttel.Text = ""
Txtaddress.Text = ""
Txtpath.Text = ""
' 清空图片
Pibox.Image = Nothing
End Sub
'打开图片
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "jpg files (*.jpg)|*.jpg|gif files (*.gif)|*.gif|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
'存储目录
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = DialogResult.OK Then
'在文本框中显示图片的路径
Dim path As String
path = openFileDialog1.FileName
Txtpath.Text = path
'在pinturebox中显示图片
Pibox.SizeMode = PictureBoxSizeMode.StretchImage
Pibox.Image = Image.FromFile(path)
End If
Imports System
Imports System.IO
Imports System.Data
'引入数据库操作类命名空间
Imports System.Data.OleDb
'引入ADO.NET操作命名空间
Imports Microsoft.VisualBasic
Public Class Form1
Inherits System.Windows.Forms.Form
'标志是否进行更该
Dim flag As Integer = 0
Dim flag1 As Integer = 0
Public flag2 As Integer = 0
Dim eid As String
Dim ADOcmd As OleDbDataAdapter
'建立ADODataSetCommand对象
Dim ds As DataSet = New DataSet()
'建立DataSet对象
Dim mytable As Data.DataTable
'建立表单对象
Dim myrow As Data.DataRow
'建立数据行对象
Dim nrow As Integer
'定义一个整型变量来存放当前行数
Public Function OpenDb() As Short
Dim SQLstr As String
'定义SQL查询字符串
SQLstr = "select * from employees"
'建立SQL查询语句
ADOcmd = New OleDbDataAdapter(SQLstr, "Provider=SQLOLEDB;Data Source=qianzh;" & _
"Integrated Security=SSPI;Initial Catalog=employ")
'建立ADODataSetCommand对象
ADOcmd.Fill(ds, "employees")
'取得表单
mytable = ds.Tables.Item(0)
'取得employees表
nrow = 0
'设置为起始行
myrow = mytable.Rows.Item(nrow)
'取得第1行数据
End Function
Private Sub LoadPict()
Dim mstream As ADODB.Stream
mstream = New ADODB.Stream()
mstream.Type = ADODB.StreamTypeEnum.adTypeBinary
mstream.Open()
'读取图片,并再当前目录存为一个临时文件temp.gif
mstream.Write(myrow.Item(9))
mstream.SaveToFile("temp.gif", ADODB.SaveOptionsEnum.adSaveCreateOverWrite)
mstream.Close()
Dim path As String = "temp.gif"
PiBox.SizeMode = PictureBoxSizeMode.StretchImage
PiBox.Image = Image.FromFile(path)
End Sub
评论