How do you import a .bmp file into an sql database?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to save a logo in an sql database but have been unable to find any examples of how to do this through VB

Keeping the logo file on a local C:\ folder is not allowed by laboratory policies

Thanks in advance for your help

Ji
 
Hi Jim,

A complete answer

Private abyt() As Byte
Private fo As New OpenFileDialog
Private sf As New SaveFileDialog

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Reading a picture and put it in a bytearray
If fo.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(fo.FileName, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
abyt = br.ReadBytes(CInt(fs.Length))
br.Close()
'just to show the sample without a fileread error
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button2.Click
'writing a picture from a bytearray
If sf.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(sf.FileName, _
IO.FileMode.CreateNew)
Dim bw As New IO.BinaryWriter(fs)
bw.Write(abyt)
bw.Close()
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button3.Click
'writing a bytearray to a dataset
Dim ds As New DataSet
ds.Tables.Add(New DataTable("Photo"))
ds.Tables(0).Columns.Add(New DataColumn("Sample"))
ds.Tables(0).Columns(0).DataType =
System.Type.GetType("System.Byte[]")
ds.Tables(0).Rows.Add(ds.Tables(0).NewRow)
ds.Tables(0).Rows(0)(0) = abyt
Dim sf As New SaveFileDialog
If sf.ShowDialog = DialogResult.OK Then
ds.WriteXml(sf.FileName, XmlWriteMode.WriteSchema)
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button4.Click
'reading a picture from a dataset
Dim ds As New DataSet
If fo.ShowDialog = DialogResult.OK Then
ds.ReadXml(fo.FileName)
End If
abyt = CType(ds.Tables(0).Rows(0)(0), Byte())
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End Sub

I hope this helps a little bit?

Cor
I want to save a logo in an sql database but have been unable to find any
examples of how to do this through VB.
 
Cor

This code executes without error in my app through inserting the .bmp file into the dataset. Your example shows writing the data subsequently to an xml file. The problem now is getting the data written from the dataset to the database. I don't believe I need to write the data to an xml file

My code creates a dataadapter which has been initialized with the database connection string and an insert command which looks like this

genadapter.InsertCommand.CommandText = "INSERT INTO Logo(Logo, Branch, EnteredBy) VALUES (" + ds.Tables("logo").Rows(0)(0) + "," + Chr(39) + Branch + Chr(39) + "," + Trim(Str(staffid)) + ")

After putting the code inside a try block, this gives the following error when it executes

Error 13 building insert command strin
Operator is not valid for command "insert into Logo(log,branch,E" and type Byte(

The code for the entire subroutine is below

Thanks for the help again

Public Sub convertlablogo(
Dim logofile As Strin
Dim ds As New System.Data.DataSe
Dim abyte As Byte(
Dim genadapter As New System.Data.SqlClient.SqlDataAdapte
Dim insertcommand As New System.Data.SqlClient.SqlComman

ds.Tables.Add("logo"
ds.Tables("logo").Columns.Add("logo") ', GetType(String)
ds.Tables("logo").Columns(0).DataType = System.Type.GetType("System.Byte[]"
ds.Tables("logo").Columns.Add("branch", GetType(String)
ds.Tables("logo").Columns.Add("EnteredBy", GetType(Int16)
ds.Tables("logo").Clear(
logofile = Dir(drive + "lablogo.bmp"
If logofile = "LABLOGO.BMP" The
Dim fs As New IO.FileStream(drive + "lablogo.bmp", IO.FileMode.Open
Dim br As New IO.BinaryReader(fs
abyte = br.ReadBytes(CInt(fs.Length)
br.Close(
End I
ds.Tables("logo").Rows.Add(ds.Tables(0).NewRow
ds.Tables("logo").Rows(0)(0) = abyt
genadapter.InsertCommand = insertcomman
genadapter.InsertCommand.Connection = btbd

' ******************* ERROR OCCURS HERE *******************************************
Tr
genadapter.InsertCommand.CommandText = "INSERT INTO Logo(Logo, Branch, EnteredBy) VALUES (" + ds.Tables("logo").Rows(0)(0) + "," + Chr(39) + Branch + Chr(39) + "," + Trim(Str(staffid)) + ")
Catch ex As Exceptio
MsgBox("Error " + Err.Number.ToString + " building insert command string" + Chr(10) + genadapter.InsertCommand.CommandText + Chr(10) + ex.Message
End Tr

' genadapter.UpdateCommand.CommandText = "UPDATE Logo SET Logo = @Logo, Branch = @Branch, EntryDate = @EntryDate, EnteredBy = @EnteredBy WHERE (LogoBtBID = @Original_LogoBtBID) AND (Branch = @Original_Branch) AND (EnteredBy = @Original_EnteredBy) AND (EntryDate = @Original_EntryDate); SELECT Logo, Branch, EntryDate, EnteredBy, LogoBtBID FROM Logo WHERE (LogoBtBID = @LogoBtBID)

Tr
genadapter.Update(ds.Tables("logo")
Catch ex As Exceptio
MsgBox("Error " + Err.Number.ToString + " inserting logo into database " + Chr(10) + ex.Message
Exit Su
End Tr
MsgBox("lablogo loaded"
End Su
 
Hi Jim,
ds.Tables("logo").Rows.Add(ds.Tables(0).NewRow)
ds.Tables("logo").Rows(0)(0) = abyte

dim cb as new SqlCommandbuilder(genadapter)
(You can try this to see if it works, and then when you wish you can chage
it with those insert, delete and update commands).
genadapter.Update(ds.Tables("logo"))


I did not check your complete program, so if you have errors message again.

Cor
 
Back
Top