Imports System.Data.OleDb
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Private curImage As Image = Nothing
Private curFileName As String = Nothing
Private connectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\AppliedAdoNet.mdb"
Private savedImageName As String _
= "C:\\ImageFromDb.BMP"
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents DB2DataAdapter1 As DDTek.DB2.DB2DataAdapter
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.Button4 = New System.Windows.Forms.Button
Me.DB2DataAdapter1 = New DDTek.DB2.DB2DataAdapter
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(40, 48)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(168, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(264, 40)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(152, 40)
Me.Button1.TabIndex = 1
Me.Button1.Text = "browse image"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(40, 136)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(136, 40)
Me.Button2.TabIndex = 2
Me.Button2.Text = "save image"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(256, 136)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(152, 40)
Me.Button3.TabIndex = 3
Me.Button3.Text = "read image"
'
'PictureBox1
'
Me.PictureBox1.Location = New System.Drawing.Point(8, 184)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(744, 352)
Me.PictureBox1.TabIndex = 4
Me.PictureBox1.TabStop = False
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(472, 48)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(176, 32)
Me.Button4.TabIndex = 5
Me.Button4.Text = "usedatareader"
'
'DB2DataAdapter1
'
Me.DB2DataAdapter1.DeleteCommand = Nothing
Me.DB2DataAdapter1.InsertCommand = Nothing
Me.DB2DataAdapter1.SelectCommand = Nothing
Me.DB2DataAdapter1.UpdateCommand = Nothing
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(760, 534)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.PictureBox1)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub BrowseBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim openDlg As OpenFileDialog = New OpenFileDialog
openDlg.Filter = "All Bitmap files|*.bmp"
Dim filter As String = openDlg.Filter
openDlg.Title = "Open a Bitmap File"
If (openDlg.ShowDialog() = DialogResult.OK) Then
curFileName = openDlg.FileName
TextBox1.Text = curFileName
End If
End Sub
Private Sub SaveImageBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.Text Is String.Empty Then
MessageBox.Show("Browse a bitmap")
Return
End If
' Read a bitmap contents in a stream
Dim fs As FileStream = New FileStream(curFileName, FileMode.OpenOrCreate, FileAccess.Read)
Dim rawData() As Byte = New Byte(fs.Length) {}
fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length))
fs.Close()
' Construct a SQL string and a connection object
Dim sql As String = "SELECT * FROM user"
Dim conn As OleDbConnection = New OleDbConnection
conn.ConnectionString = connectionString
' Open connection
If conn.State <> ConnectionState.Open Then
conn.Open()
End If
' Create a data adapter and data set
Dim adapter As OleDbDataAdapter = New OleDbDataAdapter(sql, conn)
Dim cmdBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(adapter)
Dim ds As DataSet = New DataSet("user")
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
' Fill data adapter
adapter.Fill(ds, "user")
Dim userDes As String = "Mahesh Chand is a founder of C# Corner "
userDes += "Author: 1. A Programmer's Guide to ADO.NET;"
userDes += ", 2. Applied ADO.NET. "
' Create a new row
Dim row As DataRow = ds.Tables("user").NewRow()
row("userid") = "100"
row("UserName") = "karthikeyan"
row("UserPhoto") = rawData
' Add row to the collection
ds.Tables("user").Rows.Add(row)
' Save changes to the database
adapter.Update(ds, "user")
' Clean up connection
If conn Is Nothing Then
If conn.State = ConnectionState.Open Then
conn.Close()
End If
' Dispose connection
conn.Dispose()
End If
MessageBox.Show("Image Saved")
End Sub
Private Sub button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
' Construct a SQL string and a connection object
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; " & "Data Source=C:\AppliedAdoNet.mdb"
Dim sql As String = "SELECT userPhoto FROM user"
Dim conn As OleDbConnection = New OleDbConnection
conn.ConnectionString = connectionString
' Open connection
'If conn.State <> ConnectionState.Open Then
conn.Open()
'End If
Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
Dim fs As FileStream
Dim bw As BinaryWriter
Dim bufferSize As Integer = 300000
Dim outbyte(300000 - 1) As Byte
Dim retval As Long
Dim startIndex As Long = 0
Dim pub_id As String = ""
Dim reader As OleDbDataReader = _
cmd.ExecuteReader(CommandBehavior.SequentialAccess)
' Read first record
reader.Read()
fs = New FileStream(savedImageName, FileMode.OpenOrCreate, FileAccess.Write)
bw = New BinaryWriter(fs)
startIndex = 0
retval = reader.GetBytes(0, 0, outbyte, 0, bufferSize)
bw.Write(outbyte)
bw.Flush()
' Close the output file.
bw.Close()
fs.Close()
reader.Close()
' Display image
curImage = Image.FromFile(savedImageName)
PictureBox1.Image = curImage
PictureBox1.Invalidate()
' Clean up connection
If conn.State = ConnectionState.Open Then
conn.Close()
' Dispose connection
conn.Dispose()
End If
End Sub
End Class