Wednesday 28 March 2007

MySQL - Downloading data

So you've uploaded your binary file into the database but how do you get it? Place the following code in a function or whatever you choose:


Dim datareader As MySql.Data.MySqlClient.MySqlDataReader
Dim myConnection As New MySql.Data.MySqlClient.MySqlConnection
Dim mycommand As New MySql.Data.MySqlClient.MySqlCommand
myConnection.ConnectionString = "Server=
MYSERVER;User Id=MYUSER;Password=MYPASS;Persist Security Info=True;Database=MYDATABASE"
mycommand.Connection = myConnection


mycommand.CommandText = "SELECT Document, MIMEType, FileName FROM mydb.mytable WHERE ID = 1;"
datareader = mycommand.ExecuteReader()

While datareader.Read
If datareader.Item(0).Equals(System.DBNull.Value) Then
Response.Write("")
Else
Dim FileName
Dim MIMEType
FileName = datareader.Item(2).ToString
MIMEType = datareader.Item(1).ToString
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=""" & FileName & """")
Response.ContentType = MIMEType
Response.BinaryWrite(datareader.Item(0))
End If
End While

No comments: