Dim con As New SqlConnection _
("Server YourServer;uid ;pwd ;database northwind")
Dim da As New SqlDataAdapter _
("Select * From MyImages" con)
Dim MyCB As SqlCommandBuilder New SqlCommandBuilder(da)
Dim ds As New DataSet()
da.MissingSchemaAction MissingSchemaAction.AddWithKey
Dim fs As New FileStream _
("C:winntGone Fishing.BMP" FileMode.OpenOrCreate _
FileAccess.Read)
Dim MyData(fs.Length) As Byte
fs.Read(MyData 0 fs.Length)
fs.Close()
con.Open()
da.Fill(ds "MyImages")
Dim myRow As DataRow
myRow ds.Tables("MyImages").NewRow()
myRow("Description") "This would be description text"
myRow("imgField") MyData
ds.Tables("MyImages").Rows.Add(myRow)
da.Update(ds "MyImages")
fs Nothing
MyCB Nothing
ds Nothing
da Nothing
con.Close()
con Nothing
MsgBox ("Image saved to database")
Double-click Button2 and then add the following code to the Button2_Click
event handler:
Note You must change uid and pwd to the correct values before you run this
code. Make sure that User ID has the appropriate permissions to perform this
operation on the database.
Dim con As New SqlConnection _
("Server YourServer;uid ;pwd ;database northwind")
Dim da As New SqlDataAdapter _
("Select * From MyImages" con)
Dim MyCB As SqlCommandBuilder New SqlCommandBuilder(da)
Dim ds As New DataSet()
con.Open()
da.Fill(ds "MyImages")
Dim myRow As DataRow
myRow ds.Tables("MyImages").Rows(0)
Dim MyData() As Byte
MyData myRow("imgField")
Dim K As Long
K UBound(MyData)
Dim fs As New FileStream _
("C:winntGone Fishing2.BMP" FileMode.OpenOrCreate _
FileAccess.Write)
fs.Write(MyData 0 K)
fs.Close()
fs Nothing
MyCB Nothing
ds Nothing
da Nothing
con.Close()
con Nothing
MsgBox ("Image retrieved")