Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Image storing in Ms-Access through Vb.Net 2005

G.ManivannanG.Manivannan Posts: 43Member
Hai,
How can I store a "jpg" or "Gif" image in the Ms.Access database using Vb.Net 2005?
G.Manivannan, M.Sc.,[it],
VBA Programmer,
AKUVA Infotech, Bangalore.

Comments

  • jbassmanpjbassmanp Posts: 160Member
    : Hai,
    : How can I store a "jpg" or "Gif" image in the Ms.Access database
    : using Vb.Net 2005?
    : G.Manivannan, M.Sc.,[it],
    : VBA Programmer,
    : AKUVA Infotech, Bangalore.

    You might find it better to store its' location on the hard drive or where-ever in the database. Then when needed load it into a Picturebox for viewing. Less burden on the database and computers' memory that way. Someone else may have a better idea.....

    ex: fieldtextbox.text ="c:arthouseno2.jpg"

    then load the pitcurebox with fieldtextboxt.text ....
  • G.ManivannanG.Manivannan Posts: 43Member
    Hai,
    This causes some problem when the corresponding image folder or file is deleted or curroupted in the system.
    So the another way is storing the image in database.It is not going to be stored as image.
    The image is converted into byte stream and then it stored as 'oleobject' in the database.So the memory space in db will not be used so much.
    If you want to open the image you have convert the byte stream into image and the full image can be opened.There will be no damage.Image will be safe.
    The thing I need is that I want to know how to convert the image to byte stream and from byte stream to Image fromat.
    G.Manivannan, M.Sc.,[it],
    VBA Programmer,
    AKUVA Infotech, Bangalore.
  • HackmanCHackmanC Posts: 441Member
    The best way is store the files in the hard disk. ( - jbassmanp - )

    I don't store images in an Access DB. Only in SQL Server.
    Access corrupts files easily. And you lost all the images at once.
    But some times I need to store them on SQL Server.

    The only files I store there are quick snapshots for a short time, and
    when I dont want to give full (write) access to drives or directories.

    Access 2000...

    [code]
    CREATE TABLE [Tabla1] (
    [ID] [int] NOT NULL PRIMARY KEY, -- Number
    [Nameb] [varchar] (80) NOT NULL , -- Text
    [Imageb] [
    image
    ] NULL , -- OLE
    )
    GO

    Private Sub Insert1()
    'CONNECTION

    Dim OleDbConnection1 As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=C:Documen" & _
    "ts and SettingsAdministratorMy documentsAccess1.mdb;.......")
    OleDbConnection1.Open()
    Dim OleDbInsertCommand1 As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand()

    'Access (OleDb)
    OleDbInsertCommand1.CommandText = "INSERT INTO Table1(ID, Nameb, Imageb) VALUES (@ID, @Nameb, @Imageb)"
    OleDbInsertCommand1.Connection = OleDbConnection1
    OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@ID", System.Data.OleDb.OleDbType.Integer, 4, "ID"))
    OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Nameb", System.Data.OleDb.OleDbType.VarChar, 80, "Nameb"))
    OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Imageb", System.Data.OleDb.OleDbType.VarBinary, 2147483647, "Imageb"))

    Dim FileStream1 As System.IO.FileStream
    Dim FileInfo1 As System.IO.FileInfo

    ' READ THE FILE INTO MEMORY
    FileInfo1 = New System.IO.FileInfo("C:jessica-alba-100204-01.jpg")
    FileStream1 = New System.IO.FileStream("C:jessica-alba-100204-01.jpg", IO.FileMode.Open)
    Dim Array1(CInt(FileInfo1.Length - 1)) As Byte
    Debug.WriteLine(FileStream1.Read(Array1, 0, CInt(FileInfo1.Length)))
    FileStream1.Close()

    Dim String1 As String
    Dim ASCIIEncoding1 As New System.Text.ASCIIEncoding()
    String1 = ASCIIEncoding1.GetString(Array1)

    ' RUN THE COMMAND
    OleDbInsertCommand1.Parameters("@ID").Value = 1
    OleDbInsertCommand1.Parameters("@Nameb").Value = "Jessica_Alba"
    OleDbInsertCommand1.Parameters("@Imageb").Value = Array1
    OleDbInsertCommand1.ExecuteNonQuery()

    OleDbConnection1.Close()
    End Sub

    Private Sub Display1()
    'CONNECTION
    Dim OleDbConnection1 As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=C:Documen" & _
    "ts and SettingsAdministratorMy documentsAccess1.mdb;.......")
    OleDbConnection1.Open()
    Dim OleDbSelectCommand1 As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand()
    Dim OleDbReader1 As System.Data.OleDb.OleDbDataReader

    'Access (OleDb)
    OleDbSelectCommand1.CommandText = "SELECT Imageb FROM Table1 WHERE ID = 1"
    OleDbSelectCommand1.Connection = OleDbConnection1

    ' RUN THE COMMAND
    OleDbReader1 = OleDbSelectCommand1.ExecuteReader()
    OleDbReader1.Read()
    Dim Len1 As Long = OleDbReader1.GetBytes(0, 0, Nothing, 0, 0)
    Dim Array1(CInt(Len1)) As Byte
    OleDbReader1.GetBytes(0, 0, Array1, 0, CInt(Len1))

    OleDbConnection1.Close()

    Dim MemoryStream1 As New System.IO.MemoryStream(Array1)
    Dim Bitmap1 As New System.Drawing.Bitmap(MemoryStream1)

    Dim Form1 As New System.Windows.Forms.Form()
    Dim PictureBox1 As New System.Windows.Forms.PictureBox()

    PictureBox1.Dock = DockStyle.Fill
    PictureBox1.Image = Bitmap1
    Form1.Controls.Add(PictureBox1)
    Form1.ShowDialog()
    End Sub
    [/code]

    SQL Server 2000...

    [code]
    CREATE TABLE [Table1] (
    [ID] [int] NOT NULL ,
    [Name] [varchar] (80) NOT NULL ,
    [
    Image] [image
    ] NULL ,
    CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
    (
    [ID]
    ) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    GO

    Private Sub Insert1()
    'CONNECTION
    Dim SqlConnection1 As New System.Data.SqlClient.SqlConnection("data source=(local);initial catalog=MyDB;password" & _
    "=MyPass;persist security info=True;user id=sa;workstation id=SRCDEV;packet size=4096")
    SqlConnection1.Open()
    Dim SqlInsertCommand1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()

    'SQL (SqlServer)
    SqlInsertCommand1.CommandText = "INSERT INTO Table1(ID, Name, Image) VALUES (@ID, @Name, @Image); SELECT ID, Name, Image FROM Table1 WHERE (ID = @ID)"
    SqlInsertCommand1.Connection = SqlConnection1
    SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ID", System.Data.SqlDbType.Int, 4, "ID"))
    SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Name", System.Data.SqlDbType.VarChar, 80, "Name"))
    SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Image", System.Data.SqlDbType.VarBinary, 2147483647, "Image"))

    Dim FileStream1 As System.IO.FileStream
    Dim FileInfo1 As System.IO.FileInfo

    ' READ THE FILE INTO MEMORY
    FileInfo1 = New System.IO.FileInfo("C:jessica-alba-100204-01.jpg")
    FileStream1 = New System.IO.FileStream("C:jessica-alba-100204-01.jpg", IO.FileMode.Open)
    Dim Array1(CInt(FileInfo1.Length - 1)) As Byte
    Debug.WriteLine(FileStream1.Read(Array1, 0, CInt(FileInfo1.Length)))
    FileStream1.Close()

    Dim String1 As String
    Dim ASCIIEncoding1 As New System.Text.ASCIIEncoding()
    String1 = ASCIIEncoding1.GetString(Array1)

    ' RUN THE COMMAND
    SqlInsertCommand1.Parameters("@ID").Value = 1
    SqlInsertCommand1.Parameters("@Name").Value = "Jessica_Alba"
    SqlInsertCommand1.Parameters("@Image").Value = Array1
    SqlInsertCommand1.ExecuteNonQuery()

    SqlConnection1.Close()
    End Sub

    Private Sub Display1()
    'CONNECTION
    Dim SqlConnection1 As New System.Data.SqlClient.SqlConnection("data source=(local);initial catalog=MyDB;password" & _
    "=MyPass;persist security info=True;user id=sa;workstation id=SRCDEV;packet size=4096")
    SqlConnection1.Open()
    Dim SqlSelectCommand1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()
    Dim SqlReader1 As System.Data.SqlClient.SqlDataReader

    'SQL (SqlServer)
    SqlSelectCommand1.CommandText = "SELECT Image FROM Table1 WHERE ID = 1"
    SqlSelectCommand1.Connection = SqlConnection1

    ' RUN THE COMMAND
    SqlReader1 = SqlSelectCommand1.ExecuteReader()
    SqlReader1.Read()
    Dim Len1 As Long = SqlReader1.GetBytes(0, 0, Nothing, 0, 0)
    Dim Array1(CInt(Len1)) As Byte
    SqlReader1.GetBytes(0, 0, Array1, 0, CInt(Len1))

    SqlConnection1.Close()

    Dim MemoryStream1 As New System.IO.MemoryStream(Array1)
    Dim Bitmap1 As New System.Drawing.Bitmap(MemoryStream1)

    Dim Form1 As New System.Windows.Forms.Form()
    Dim PictureBox1 As New System.Windows.Forms.PictureBox()

    PictureBox1.Dock = DockStyle.Fill
    PictureBox1.Image = Bitmap1
    Form1.Controls.Add(PictureBox1)
    Form1.ShowDialog()
    End Sub
    [/code]

    [red]Good luck![/red]
    [blue]Hackman[/blue]
  • G.ManivannanG.Manivannan Posts: 43Member
    Hai,
    Your coding is very useful .
    Thanks a lot.

    I had use the codes of ms-access 2000 for storing the image.For storing the image there is no problem.It is perfectly working.

    But there is a error oocured when I tried to open the image using its id from the database where I had stored.

    I had just changed the Connetion statement and Query in the coding.

    the error that I am getting is: "PARAMETER IS NOT VALID"
    It is occuring only at the runtime.
    It is occuring in the following line :

    "Dim Bitmap1 As New System.Drawing.Bitmap(MemoryStream1)"

    right now I am working on Vb.net 2005 and Ms-Access 2003 but the database format in Access 2003 is Access 2000 only.

    How can i overcome this problem?

    G.Manivannan, M.Sc.,[it],
    VBA Programmer,
    AKUVA Infotech, Bangalore.
  • ratnagiriratnagiri Posts: 1Member
    : : Hai,
    : : How can I update a "jpg" or "Gif" image in the Ms.Access database
    : : using Vb.Net 2005?
    : : k. Ratnagiri, M.Sc.,[is],
    : : VBA Programmer,
    : : .
    :
    :
Sign In or Register to comment.