VB以二进制流保存图片到access,数据库里的主键必须是自动编码,不是自动编码就报错,提示:![](http://imgsrc.baidu.com/forum/w%3D580/sign=9ac744c58bd4b31cf03c94b3b7d7276f/4d3b8794a4c27d1e023164421bd5ad6eddc43821.jpg)
调试显示在Rs.Update
求高手帮解决下,非常感谢!!
代码如下:
Option Explicit
Dim Cn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim mst As ADODB.Stream
Dim mystr As String
Private Sub Form_Load()
Adodc1.ConnectionString = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & App.Path & "\riyao.MDB"
Adodc1.RecordSource = "stock"
Adodc1.Refresh
End Sub
Private Sub Command3_Click() '保存图片到数据库
If mystr <> "" Then
Set Cn = New ADODB.Connection
Cn.ConnectionString = ";DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & App.Path & "\riyao.MDB"
Cn.Open
Set Rs = New ADODB.Recordset
Rs.Open "select * from stock", Cn, adOpenStatic, 3
Set mst = New ADODB.Stream
mst.Type = adTypeBinary
mst.Open
mst.LoadFromFile mystr
Rs.AddNew
Rs.Fields("图片").Value = mst.Read
Rs.Update
Rs.Close
Cn.Close
Adodc1.Refresh
Else
MsgBox ("请先选择图片!")
End If
End Sub
Private Sub Command1_Click()
CommonDialog1.Filter = "bmp|*.jpg;*.bmp;*.gif"
CommonDialog1.FilterIndex = 2
' 显示"打开"对话框
CommonDialog1.ShowOpen
Image1.Picture = LoadPicture(CommonDialog1.FileName)
mystr = CommonDialog1.FileName
End Sub
Private Sub Command2_Click()
End
End Sub
![](http://imgsrc.baidu.com/forum/w%3D580/sign=9ac744c58bd4b31cf03c94b3b7d7276f/4d3b8794a4c27d1e023164421bd5ad6eddc43821.jpg)
调试显示在Rs.Update
求高手帮解决下,非常感谢!!
代码如下:
Option Explicit
Dim Cn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim mst As ADODB.Stream
Dim mystr As String
Private Sub Form_Load()
Adodc1.ConnectionString = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & App.Path & "\riyao.MDB"
Adodc1.RecordSource = "stock"
Adodc1.Refresh
End Sub
Private Sub Command3_Click() '保存图片到数据库
If mystr <> "" Then
Set Cn = New ADODB.Connection
Cn.ConnectionString = ";DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & App.Path & "\riyao.MDB"
Cn.Open
Set Rs = New ADODB.Recordset
Rs.Open "select * from stock", Cn, adOpenStatic, 3
Set mst = New ADODB.Stream
mst.Type = adTypeBinary
mst.Open
mst.LoadFromFile mystr
Rs.AddNew
Rs.Fields("图片").Value = mst.Read
Rs.Update
Rs.Close
Cn.Close
Adodc1.Refresh
Else
MsgBox ("请先选择图片!")
End If
End Sub
Private Sub Command1_Click()
CommonDialog1.Filter = "bmp|*.jpg;*.bmp;*.gif"
CommonDialog1.FilterIndex = 2
' 显示"打开"对话框
CommonDialog1.ShowOpen
Image1.Picture = LoadPicture(CommonDialog1.FileName)
mystr = CommonDialog1.FileName
End Sub
Private Sub Command2_Click()
End
End Sub