1,216
社区成员
发帖
与我相关
我的任务
分享
Imports System.IO
Imports System.Text
Imports System.RunTime.Serialization.Formatters
Imports System.Data
Imports Cargo.Bus
Imports ime
Imports system.Math
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'图片路径
Dim strPath As String = "D:\代理人系统\CLIENTCARGOBOOKING\Resources\88.jpg"
Dim strPhotoPath As String = System.Web.HttpContext.Current.Server.MapPath(strPath) '读取图片
Dim fs As FileStream = New System.IO.FileStream(strPhotoPath, FileMode.Open, FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim photo() As Byte = br.ReadBytes(CType(fs.Length, Integer))
br.Close()
fs.Close()
Dim myConn As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=.;Initial Catalog=JM;User ID=sa;Password=123")
Dim strComm As String = " INSERT INTO zy(personName, personPhotoPath, personPhoto) "
strComm = (strComm + (" VALUES('wangwu', '" + (strPath + "', @photoBinary )")))
Dim myComm As SqlClient.SqlCommand = New SqlClient.SqlCommand(strComm, myConn)
myComm.Parameters.Add("@photoBinary", SqlDbType.Binary, photo.Length)
myComm.Parameters("@photoBinary").Value = photo
myConn.Open()
myComm.ExecuteNonQuery()
myConn.Close()
'3: .VB把文件存入数据库IMAGE字段()
End Sub
End Class
Private Sub Command1_Click() '使用文件打开对话框查找硬盘图片文件
On Error GoTo Err
CommonDialog1.Filter = "JPG文件(*.jpg)|*.jpg|GIF文件(*.gif)|*.gif|BMP文件(*.bmp)|*.bmp|PNG文件(*.png)|*.png"
CommonDialog1.ShowOpen
Text2.Text = CommonDialog1.FileName '得到图片的硬盘路径+文件名+扩展名
SSR = CommonDialog1.FileTitle '得到图片的文件名+扩展名
SSR = Right(SSR, 4) '得到图片的扩展名,带分隔符“.”
Command2.Enabled = True
Exit Sub
Err:
MsgBox Err.Description '点击取消按钮会产生错误,显示错误内容
End Sub
RS.Open "select * from 数据表名称", cn, 3, 2
RS.AddNew
RS.Fields("名称") = Text1.Text '存储由自己设置的图片名称
Mst.Type = adTypeBinary
Mst.Open '打开Mst对象
If Text2.Text <> "" Then Mst.LoadFromFile Text2.Text 'Mst对象将Text2中的图片内容转换成二进制数据
RS.Fields("文件") = Mst.Read '将二进制数据存储在数据表的Image数据类型的字段,这里的字段名称是【文件】
RS.Fields("扩展名") = SSR '将图片文件的扩展名存储在这个字段
RS.Update
Mst.Close
RS.Close
Image1.Picture = LoadPicture("")
RS.Open "select * from 数据表名称 order By ID Desc", cn, 2, 2 'SQL语句根据你显示的要求修改即可
Mst.Type = adTypeBinary
Mst.Open
Mst.Write RS.Fields("文件") 'Mst对象读取数据库二进制数据
Mst.SaveToFile App.Path & "\doc\" & RS.Fields("名称") & RS.Fields("扩展名"), adSaveCreateOverWrite
'Mst对象将转换的图片文件存储在doc目录中,存储的图片是数据库存储的文件名+扩展名
Image1.Picture = LoadPicture(App.Path & "\doc\" & RS.Fields("名称") & RS.Fields("扩展名"))
'在 Image1 控件中将图片显示出来
Mst.Close
RS.Close