hi all
plz help me. i have to store images in database(access at backend) through vb.net. if u have any information plz reply as soon as possible.
thanks
------------------------------------------------------------------------------
Step 1: Creating the Table SampleImageTable
Create a table SampleImageTable with the field sampleimage of data type
image. Use the following SQL Satement to create the table.
create table SampleImageTable (sampleimage image)
Step 2: Creating a Stored Procedure
Create a stored procedure UploadImage which is used to store the images. Use the following SQL Query.
create procedure UploadImage(@imgsamgetimage as image) asinsert into SampleImageTable (sampleimage) values (@imgsamgetimage)
Step 3: Creating the Sample Project
Create a Project "ImageDemo"
Add a new Windows Form and name it as "IamgeDemo"
Add One Textbox to the form and name it as txtImg
Add One OpenFileDialog to the form
Add three Command Buttons to the form and name them as btnseltfile, btnUpload, and btnDownload
Add one to the form
Design the form as shown below
Step 4: Writing the Code for save and reteriving the files
Write the following code in the btnseltfile click event
Private Sub btnseltfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnseltfile.Click OpenFileDialog1.ShowDialog() txtImg.Text = OpenFileDialog1.FileNameEnd Sub
Explaination for the code.
The above is used to display the OpenFileDialog to get the Image file name which needs to be uploaded. Write the following code in the btnUpload button click event
Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click Try If Trim(txtImg.Text) = "" Then MsgBox("Please select a image.") Exit Sub End If Dim fs As New FileStream(Trim(txtImg.Text), FileMode.Open) Dim Data() As Byte = New [Byte](fs.Length) {} fs.Read(Data, 0, fs.Length) Dim con As New System.Data.SqlClient.SqlConnection("data source=mt5;initial catalog=master; user id=sa;password=mms") con.Open() Dim cmd As New System.Data.SqlClient.SqlCommand("UploadImage") cmd.Connection = con cmd.CommandType = CommandType.StoredProcedure cmd.Parameters.Add("@imgsamgetimage", Data) cmd.ExecuteNonQuery() con.Close() fs.Close() Catch ex As System.Data.SqlClient.SqlException MsgBox(ex.Message) End TryEnd Sub
Explaination for the above code.
The above code is used to get the file content in a bit array and store the image content in the SQL Server database.
Write the following code in the btndownload button click event
Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click Dim con As New System.Data.SqlClient.SqlConnection("data source=mt5;initial catalog=master; user id=sa;password=mms") con.Open() Dim cmd As New System.Data.SqlClient.SqlCommand("select * from SampleImageTable") cmd.Connection = con cmd.CommandType = CommandType.Text Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd) Dim ds As New DataSet() da.Fill(ds) Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte()) Dim memorybits As New MemoryStream(bits) Dim bitmap As New Bitmap(memorybits) PictureBox1.Image = bitmapEnd Sub