473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

how to store images in database through vb.net

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
Jan 16 '08 #1
2 7850
jella
1
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
Feb 11 '08 #2
Hi just check this link which is very simple
http://aspalliance.com/articleViewer.aspx?aId=138&pId=
Feb 11 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: cover | last post by:
The question is, we have two options to store images, either in a Database (MySQL, Postgres, ...) like blob data, or in the hard disk the file and the path in database. Which option is better?...
3
by: DL | last post by:
Hi, Many questions have already been asked and answered about images in Access... But despite having searched, I have not found an answer to the following question... In my DB, several forms...
2
by: Tim Zych | last post by:
How would I go about storing data files on my web host? I have an Access database driven website and I would like to save files and be able to download them. Can somebody point me in the right...
1
by: john_20_28_2000 | last post by:
Using asp.net, c#, mysql or SQL Server. I have a image uploading and wanted it to be "secure." Each user has their own section to login and upload images. Should I store the images in a database...
6
by: Rudy | last post by:
Hello all! I am amazed how many posts I have read to store an image in SQL, and just as many against it. So I learned how to store an image in a SQL db and retrieve the image. A little tricky,...
8
by: romayankin | last post by:
I'm making some kind of news-board site on PHP (tthat's why I guess it's the right place to post the question here). Each entry would have a small picture (less then 100kb). Taking into...
0
by: Skiran | last post by:
Hi to everybody, I am handling a large-scale project of MIS (Marketing information System). I am using Vb 6 as front end, Ms Access as back end and Crystal report 8 as reporting tool. 1)...
4
by: vunet.us | last post by:
I want to know if this practice is effective and secure: I have been thinking about storing some data, which my users upload, in text files rather than database, since often I do not know how much...
6
by: Keith Hughitt | last post by:
Hi all, I've run into a strange error while trying to store some PNG images in a MySQL database using MySQLdb. When I try to insert smaller images (< 64kb or so) everything seems to work fine....
2
by: gm000 | last post by:
hi i m using radiobuttonlist with images like this <asp:RadioButtonList ID="rbtnthumb1" runat="server"> <asp:ListItem Value ="News_icon.jpg"> <img...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.