在VB.NET编程中如何实现对ACCESS数据库存取图片?
在VB.NET编程中如何实现对ACCESS数据库存取图片?PICTUREBOX又怎么样获取ACCESS数据中存在的图片? --------------------编程问答-------------------- 我处理这类数据时是使用的ole数据字段.
如果图形文件过大的项目中,我就只存文件名(使用string字段.)
如果图形文件不大的项目中,把图形文件的数据以二进制的形式写入old字段.使用的时候,生成一个临时文件.载入这个临时文件即可. --------------------编程问答-------------------- 顶一下! --------------------编程问答-------------------- 转换成二进制数组进行存取 --------------------编程问答-------------------- 直接存图片路径 --------------------编程问答-------------------- Dim st As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Dim str As String = txtPath.Text
Dim mbr As BinaryReader = New BinaryReader(st)
'把图片转变为二进制流
Dim buffer(st.Length) As Byte
mbr.Read(buffer, 0, CInt(st.Length))
st.Close()
把图片转为二进制流的部份代码。 --------------------编程问答-------------------- Try
Dim strConn As String = Hot.Base.strConn
'保存,bmp1为一BitMap型对象,并已经装入图形
''方法一、用 hot.sqlhelper 保存
'Dim ms As MemoryStream = New MemoryStream
'pic.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
'Dim b() As Byte = ms.ToArray
'ms.Close()
'Dim strConn As String = Hot.Base.strConn
'Dim strSql As String
'strSql = "insert into home_yjs_ys (pic) values "
'Dim arparms(3) As SqlClient.SqlParameter
'arparms(0) = New SqlClient.SqlParameter("@homeNo", SqlDbType.VarChar, 25)
'arparms(0).Value = ""
'arparms(1) = New SqlClient.SqlParameter("@picname", SqlDbType.VarChar, 60)
'arparms(1).Value = "name"
'arparms(2) = New SqlClient.SqlParameter("@pic", SqlDbType.Image)
'arparms(2).Value = b
'arparms(3) = New SqlClient.SqlParameter("@comment", SqlDbType.VarChar, 255)
'arparms(3).Value = ""
'yssoft.SqlHelper.ExecuteNonQuery(strConn, CommandType.StoredProcedure, "savehome_yjs_ys", arparms)
'方法二、用Command来保存
Dim ms As MemoryStream = New MemoryStream
pic.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim b() As Byte = ms.ToArray
ms.Close()
Dim cn As SqlClient.SqlConnection
Dim ts As SqlClient.SqlTransaction
cn = New SqlClient.SqlConnection(strConn)
cn.Open()
ts = cn.BeginTransaction
Dim cmd As New SqlClient.SqlCommand("savehome_yjs_ys", cn)
cmd.Transaction = ts
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@homeNo", SqlDbType.VarChar, 25).Value = ""
cmd.Parameters.Add("@picname", SqlDbType.VarChar, 25).Value = Today.ToLongTimeString.ToString
cmd.Parameters.Add("@pic", SqlDbType.Image).Value = b
cmd.Parameters.Add("@comment", SqlDbType.VarChar, 255).Value = "备注"
Dim ret As Integer
ret = cmd.ExecuteNonQuery
MsgBox(ret.ToString)
ts.Commit()
cn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
'数据库读取
'Dim ds As DataSet
Dim strconn As String = Hot.Base.strConn
Dim strSql As String
strSql = "select * from home_yjs_ys"
ds = yssoft.SqlHelper.ExecuteDataset(strconn, CommandType.Text, strSql)
If Not IsDBNull(ds.Tables(0).Rows(0)("pic")) Then
Dim b As Byte() = CType(ds.Tables(0).Rows(0)("pic"), Byte())
pic.Image = Image.FromStream(New MemoryStream(b), True)
icur = 0
End If
End Sub --------------------编程问答-------------------- 下面是SQL的例子,区别应该不大:
//把图片读到pictureBox,再写入数据库
SqlConnection conn=new SqlConnection(@"data source=chen易做图2004\VSdotNET;uid=sa;pwd=cym;database=lhf");
conn.Open();
SqlCommand cmd=new SqlCommand("insert into fuser values ('1a','1b',@i)",conn);
byte[] ib=new Byte[60000];
FileStream fs=new FileStream(@"D:\windows temp\temp\1.jpg",FileMode.Open ,FileAccess.Read );
fs.Read(ib,0,60000);
cmd.Parameters.Add("@i",SqlDbType.Image,(int)fs.Length);
cmd.Parameters["@i"].Value=ib;
cmd.ExecuteNonQuery();
conn.Close();
-------------------------------------------------------
//从数据库读图片到picturebox
SqlConnection conn=new SqlConnection(@"data source=chen易做图2004\VSdotNET;uid=sa;pwd=cym;database=lhf");
conn.Open();
SqlCommand cmd=new SqlCommand("select 照片 from fuser where password='1b'",conn);
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream buf=new MemoryStream((byte[])reader[0]);
Image image=Image.FromStream(buf,true);
pictureBox1.Image=image;
--------------------编程问答-------------------- mark --------------------编程问答-------------------- 学习了,试一下。
补充:.NET技术 , VB.NET