how to store images in database through vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • diarehman
    New Member
    • Dec 2007
    • 1

    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
  • jella
    New Member
    • Feb 2008
    • 1

    #2
    Originally posted by diarehman
    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 SampleImageTabl e

    Create a table SampleImageTabl e with the field sampleimage of data type
    image. Use the following SQL Satement to create the table.
    create table SampleImageTabl e (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(@im gsamgetimage as image) asinsert into SampleImageTabl e (sampleimage) values (@imgsamgetimag e)


    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_Cli ck(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnseltfile.Cli ck 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.EventArg s) Handles btnUpload.Click Try If Trim(txtImg.Tex t) = "" 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.Sql Client.SqlConne ction("data source=mt5;init ial catalog=master; user id=sa;password= mms") con.Open() Dim cmd As New System.Data.Sql Client.SqlComma nd("UploadImage ") cmd.Connection = con cmd.CommandType = CommandType.Sto redProcedure cmd.Parameters. Add("@imgsamget image", Data) cmd.ExecuteNonQ uery() con.Close() fs.Close() Catch ex As System.Data.Sql Client.SqlExcep tion MsgBox(ex.Messa ge) 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_Cli ck(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnDownload.Cli ck Dim con As New System.Data.Sql Client.SqlConne ction("data source=mt5;init ial catalog=master; user id=sa;password= mms") con.Open() Dim cmd As New System.Data.Sql Client.SqlComma nd("select * from SampleImageTabl e") cmd.Connection = con cmd.CommandType = CommandType.Tex t Dim da As New System.Data.Sql Client.SqlDataA dapter(cmd) Dim ds As New DataSet() da.Fill(ds) Dim bits As Byte() = CType(ds.Tables (0).Rows(0).Ite m(0), Byte()) Dim memorybits As New MemoryStream(bi ts) Dim bitmap As New Bitmap(memorybi ts) PictureBox1.Ima ge = bitmapEnd Sub

    Comment

    • peerraghu
      New Member
      • Nov 2007
      • 30

      #3
      Hi just check this link which is very simple
      http://aspalliance.com/articleViewer.a spx?aId=138&pId =

      Comment

      Working...