16,555
社区成员
发帖
与我相关
我的任务
分享
Imports System.Data.SqlClient
Imports system.IO
Public Class MyImage
Public imgfield As String '更新字段名
Public tabelname As String '表
Public imgfilename As String '图像文件
Private linkstr As String
Property DataSoure() As String '连接串 Server=xxx.xxx.xxx.xx;User ID=xx;Password=xx;database=xxxx
Get
DataSoure = linkstr
End Get
Set(ByVal value As String)
Dim oconn_sql As New SqlConnection(value)
Try
oconn_sql.Open()
Catch Ex As Exception
Dim exx As New Exception("无法连接服务器")
Throw exx
Return
End Try
linkstr = value
End Set
End Property
Public Function read(ByVal sql As String) As MemoryStream '读取图片 sql为查询图片条件 read("where ID=4")
If DataSoure = Nothing Then
Dim exx As New Exception("请确定Sql地址")
Throw exx
End If
Dim oconn As New SqlConnection(DataSoure)
oconn.Open()
Dim sqlcmd As New SqlClient.SqlCommand(sql, oconn)
sqlcmd.CommandType = CommandType.Text
Dim bt() As Byte = sqlcmd.ExecuteScalar()
If Not bt Is Nothing Then
If bt.Length > 0 Then
sqlcmd = Nothing
oconn.Close()
oconn = Nothing
Return New MemoryStream(bt)
Else
Dim exx As New Exception("无图片")
Throw exx
End If
Else
Dim exx As New Exception("无数据")
Throw exx
End If
sqlcmd = Nothing
oconn.Close()
oconn = Nothing
End Function
Public Sub save(ByVal upwhere As String) '保存图片 upwhere为更新条件 save("where ID=4")
If DataSoure = Nothing Then
Dim exx As New Exception("请确定Sql地址")
Throw exx
Return
End If
If imgfield = Nothing Then
Dim exx As New Exception("请确定字段名")
Throw exx
Return
End If
If tabelname = Nothing Then
Dim exx As New Exception("请确定表名")
Throw exx
Return
End If
Dim cmdstr As String = " alter Procedure InsertImage " & Chr(13) & "@img image" & Chr(13) & "AS" & Chr(13) & "Update " & tabelname & " set " & imgfield & " = @img " & upwhere
Dim sqlcmd As New SqlCommand
Dim oconn As New SqlConnection(DataSoure)
sqlcmd.CommandText = cmdstr
sqlcmd.Connection = oconn
Try
oconn.Open()
sqlcmd.ExecuteNonQuery()
Catch ex As Exception
oconn.Close()
Dim exx As New Exception("存储过程创建失败" & ex.Message)
Throw exx
Return
End Try
Dim fs As FileStream = New FileStream(imgfilename, FileMode.Open, FileAccess.ReadWrite)
Dim bt(fs.Length) As Byte
fs.Read(bt, 0, fs.Length)
fs.Close()
fs = Nothing
Dim sqlConn As SqlClient.SqlConnection = New SqlClient.SqlConnection(DataSoure)
sqlConn.Open()
sqlcmd = New SqlClient.SqlCommand("InsertImage", sqlConn)
sqlcmd.CommandType = CommandType.StoredProcedure
sqlcmd.Parameters.Add("@img", SqlDbType.Image).Value = bt
sqlcmd.ExecuteNonQuery()
sqlcmd = Nothing
sqlConn.Close()
sqlConn = Nothing
oconn.Close()
End Sub
End Class
Dim dbcon As New OleDb.OleDbConnection
Dim dbccom As New OleDb.OleDbCommand
'存入access数据库
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fs As FileStream = New FileStream("C:\Documents and Settings\forest23\桌面\1.jpg", FileMode.Open, FileAccess.ReadWrite)
Dim bt(fs.Length) As Byte
fs.Read(bt, 0, fs.Length)
Try
dbcon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\forest23\桌面\1.mdb"
dbcon.Open()
dbccom.Connection = dbcon
dbccom.CommandText = "create table pic (img longbinary)"
dbccom.CommandText = "insert into pic (img) Values (@img)"
dbccom.Parameters.Add("@img", OleDb.OleDbType.LongVarBinary).Value = bt
dbccom.ExecuteNonQuery()
dbccom = Nothing
dbcon.Close()
dbcon = Nothing
Catch
MsgBox(e.ToString)
End Try
fs.Close()
fs = Nothing
End Sub
'读取图片
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
dbcon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\forest23\桌面\1.mdb"
dbcon.Open()
dbccom.Connection = dbcon
dbccom.CommandText = "select img from pic"
Dim bt() As Byte = dbccom.ExecuteScalar
If Not bt Is Nothing Then
If bt.Length > 0 Then
Dim fs As MemoryStream = New MemoryStream(bt)
Me.PictureBox1.Image = Image.FromStream(fs)
fs.Close()
fs = Nothing
Else
MsgBox("无图片")
End If
Else
MsgBox("无数据")
End If
Catch ex As Exception
Finally
dbccom = Nothing
dbcon.Close()
dbcon = Nothing
End Try
End Sub
Try
dbcon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\forest23\桌面\1.mdb"'这是你mdb文件的位置
dbcon.Open()
dbccom.Connection = dbcon
dbccom.CommandText = "select img from pic where ID=1 "’看你access数据库里怎么存的这里该下
Dim bt() As Byte = dbccom.ExecuteScalar
If Not bt Is Nothing Then
If bt.Length > 0 Then
Dim fs As MemoryStream = New MemoryStream(bt)
Me.PictureBox1.Image = Image.FromStream(fs)
fs.Close()
fs = Nothing
Else
MsgBox("无图片")
End If
Else
MsgBox("无数据")
End If
Catch ex As Exception
Finally
dbccom = Nothing
dbcon.Close()
dbcon = Nothing
End Try
'这是读取图片
select zp from table1 where id = 1
'上传图片
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fs As FileStream = New FileStream("C:\Documents and Settings\forest23\桌面\Water lilies.jpg", FileMode.Open, FileAccess.ReadWrite)
Dim bt(fs.Length) As Byte
fs.Read(bt, 0, fs.Length)
fs.Close()
fs = Nothing
Dim sqlConn As SqlClient.SqlConnection = New SqlClient.SqlConnection("Server=(local);User Id=sa;Password=sa;Database=pubs")
sqlConn.Open()
Dim sqlCmd As New SqlClient.SqlCommand("sp_InsertImage", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.Parameters.Add("@img", SqlDbType.Image).Value = bt
sqlCmd.ExecuteNonQuery()
sqlCmd = Nothing
sqlConn.Close()
sqlConn = Nothing
MsgBox("图片插入成功", MsgBoxStyle.Information)
End Sub
'读取图片
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sqlConn As SqlClient.SqlConnection = New SqlClient.SqlConnection("Server=(local);User Id=sa;Password=sa;Database=pubs")
sqlConn.Open()
Dim sqlCmd As New SqlClient.SqlCommand("SELECT img FROM test WHERE t_ID=70", sqlConn)
sqlCmd.CommandType = CommandType.Text
Dim bt() As Byte = sqlCmd.ExecuteScalar()
If Not bt Is Nothing Then
If bt.Length > 0 Then
Dim fs As MemoryStream = New MemoryStream(bt)
pbReview.Image = Image.FromStream(fs)
fs.Close()
fs = Nothing
Else
MsgBox("无图片")
End If
Else
MsgBox("无数据")
End If
sqlCmd = Nothing
sqlConn.Close()
sqlConn = Nothing
End Sub
'这个是sql数据库的跟access差不多,你改下就可以了