开发者

how to upload images in vb6 and put it in the mysql database?

does any one know how to upload images in vb6 and can put it in the mysql database??

sample c开发者_JAVA百科odes will be fine.


I don't know how you are connecting VB6 and MySQL but the solution is as below: Step 1. Create a table with Mediumblob to store file as below

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
);

Step 2. Connection to MySQL as you can use your method for connection

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 

Step 3: Sending data to MySQL and closing the connection

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

Step 4: Reverting the Data from 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


Thank you for this code. I found it to be very useful.

Your Reverting Data routine, although technically correct, has an omission. The routine fails unless the rs object is opened correctly (see below) Here is the complete and correct code:

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  


For Oracle 11g I used the following connection string in VB:

cn.Open "Provider=OraOLEDB.Oracle.1;Password=tiger;Persist Security Info=True;User ID=scott"

and all other code is ok for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜