有没有人知道如何在vb6上传图像并将其放入mysql数据库?
示例代码没问题。
答案 0 :(得分:1)
我不知道你是如何连接VB6和MySQL的,但解决方法如下: 步骤1.使用Mediumblob创建一个表来存储文件,如下所示
CREATE TABLE files(
file_id SMALLINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
file_name VARCHAR(64) NOT NULL,
file_size MEDIUMINT UNSIGNED NOT NULL,
file MEDIUMBLOB NOT NULL
);
步骤2.连接到MySQL,因为您可以使用您的连接方法
DRIVER={MySQL ODBC 3.51 Driver}; SERVER=123.456.789.100; DATABASE=mysqldatabase; UID=sampleuser; PWD=12345; OPTION=16427
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
conn.ConnectionString = GloConnectionString
conn.CursorLocation = adUseClient
conn.Open
步骤3:将数据发送到MySQL并关闭连接
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim mystream As ADODB.Stream
Set mystream = New ADODB.Stream
mystream.Type = adTypeBinary
rs.Open "SELECT * FROM files WHERE 1=0", conn, adOpenStatic, adLockOptimistic
rs.AddNew
mystream.Open
mystream.LoadFromFile "c:\\myimage.gif"
rs!file_name = "myimage.gif"
rs!file_size = mystream.size
rs!file = mystream.read
rs.Update
mystream.Close
rs.Close
conn.Close
第4步:从MySQL恢复数据
Dim conn As New ADODB.Connection
conn.ConnectionString = GloConnectionString
conn.CursorLocation = adUseClient
conn.Open
Dim rs As New ADODB.Recordset
Dim mystream As New ADODB.Stream
mystream.Type = adTypeBinary
rs.Open "SELECT * FROM files WHERE files.file_id = 1", conn
mystream.Open
mystream.Write rs!File
mystream.SaveToFile "c:\\newimage.gif", adSaveCreateOverWrite
mystream.close
rs.Close
conn.Close
答案 1 :(得分:1)
感谢您提供此代码。 我发现它非常有用。
您的还原数据例程虽然技术上是正确的但有遗漏。 除非正确打开rs对象,否则例程将失败(参见下文) 这是完整而正确的代码:
Dim rs as ADODB.Recordset
Dim myStream as ADODB.Stream
Set rs = New ADODB.Recordset
rs.Open "SELECT * FROM files WHERE files.file_id=1;", conn, adOpenDynamic, adLockReadOnly
Set myStream = New ADODB.Stream
myStream.Type = adTypeBinary
myStream.Open
myStream.Write rs!File
myStream.SaveToFile "c:\\newimage.gif", adSaveCreateOverWrite
myStream.Close
Set myStream = Nothing
rs.Close
Set rs = Nothing
答案 2 :(得分:0)
对于Oracle 11g,我在VB
中使用了以下连接字符串:
cn.Open "Provider=OraOLEDB.Oracle.1;Password=tiger;Persist Security Info=True;User ID=scott"
所有其他代码对我来说都没问题。