Upload Image to SQL

  • Thread starter Thread starter C CORDON
  • Start date Start date
C

C CORDON

Is there any good example on how to do this, better if it shows how to limit
the image to X size and save both..original and thumbnail size to SQL.

TIA
 
Hi,

I do it like this:
Import System.Data.SqlClient
Import System.IO
....
.....
.....

dim strFileName = ....
dim myFile as System.IO.FileInfo
dim bw as BinaryWriter
dim br as BinaryReader
dim lngFileSize as long
dim arybyteFileData() as Byte
dim strFileName as string

dim mda as new SQLClient.SqlDataAdapter
dim cmdInsert as new SqlClient.SqlCommand

dim strReadFromFile as string = txtFileName.Text // file name entered
in text box


If File.Exists(strFileName) Then
Dim anImage() as Byte = GetImage(strFileName) // se below

myFile = new System.IO.FileInfo(fileName:=strReadFromFile)
lngFileSize = myFile.Length
strFileName = myFile.Name

Redim arybyteFileData(lngFileSize)

strSQL = "INSERT INTO myTable ( .... ) VALUES( @parID,
@parDateAdded, @parImage, @parFileName)

cmdInsert.CommandText = strSQL
cmdInsert.Parameters.Add("@parID", SQLDbTyhpe.BigInt, 8).Value =
lngDocID // if you add image data to another table and actual bytes
in this table (myTable), then you have the id of that image already
cmdInsert.Parameters.Add("@parDateAdded", SQlDbType.DateTime).value
= ...
cmdInsert.Parametes.Add("@parImage", SqlDbType.Image,
anImage.Length).Value = anImage
mda.InsertCommand = cmdInsert
mda.InsertCommand.ExecuteNonQuery()
end if


public Function GetImage(ByVal strFilePath as string) as Byte()
Dim fs as FileStream = new FileStream(strFilePath, FileMode.Open,
FileAccess.Read)
dim br as BinaryWriter = new BinaryWriter(fs.Length)
dim theImage() as Byte = br.ReadBytes(fs.Length)

br.close()
fs.Close()

retrun theImage
end function


HTH
Dino
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top