I have a jpeg file in my C drive from my vb.net application I want to binary read it and store it in SQL server. How can I do that? Thanks
1 Answer
You can read a file into a byte array by calling File.ReadAllBytes.
You can then put the byte array into SQL Server using a SqlParameter.
For example:
Using command As New SqlCommand("INSERT INTO sometable VALUES(@image)", connection)
command.Parameters.AddWithValue("image", File.ReadAllBytes(path))
command.ExecuteNonQuery()
End Using
2 Comments
acadia
Thanks sLaks. One question. the image is not in my C drive it is in a location on the server how can I read it like My.Computer.FileSystem.ReadAllBytes _ ("C:/Documents and Settings/selfportrait.jpg") This is the location of the files Server.MapPath("images\Signatures\") & Session("NetworkID").ToString() & ".jpeg")
SLaks
As long as it's on a local disk, you can still read it. If it was uploaded by the user, you can also get a byte array by reading
Request.Files[0].InputStream or the FileBytes property of the FileUpload control.