Inserting image into SQL database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • OuTCasT
    Contributor
    • Jan 2008
    • 374

    Inserting image into SQL database

    Can someone please tell me how to save an image into an SQL DATABASE ?
    never done it before and want to try it out.
    using web forms.
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    1.Try to use LOB as field type in database.
    2.read the image file.
    3.convert the file to binary stream .
    4 store in database.

    Comment

    • OuTCasT
      Contributor
      • Jan 2008
      • 374

      #3
      Originally posted by debasisdas
      1.Try to use LOB as field type in database.
      2.read the image file.
      3.convert the file to binary stream .
      4 store in database.

      i have no idea where to start
      lol

      Comment

      • debasisdas
        Recognized Expert Expert
        • Dec 2006
        • 8119

        #4
        Originally posted by OuTCasT
        i have no idea where to start
        lol
        There is nothing to lol here.

        Just follow the steps as suggested in previous post.

        Comment

        • dwadish
          New Member
          • Nov 2006
          • 129

          #5
          Code:
          
          Atalasoft dotImage has streaming capabilities that can be used jointly with ADO.NET to read and write images directly to a database without saving to a temporary file. The following code snippets demonstrates this in C# and VB.NET.
          
          Write to a Database
          C#
          private void SaveToSqlDatabase(AtalaImage image){SqlConnection myConnection = null;    try    {        // Save image to byte array.        byte[] imagedata = image.ToByteArray(new Atalasoft.Imaging.Codec.JpegEncoder(75));                // Create the SQL statement to add the image data.        myConnection = new SqlConnection(CONNECTION_STRING);        SqlCommand myCommand = new SqlCommand                ("INSERT INTO Atalasoft_Image_Database (Caption, ImageData) VALUES ('" + txtCaption.Text + "', @Image)", myConnection);        SqlParameter myParameter = new SqlParameter("@Image", SqlDbType.Image, imagedata.Length);        myParameter.Value = imagedata;        myCommand.Parameters.Add(myParameter);                // Open the connection and execture the statement.        myConnection.Open();        myCommand.ExecuteNonQuery();    }    finally    {        myConnection.Close();    }}Visual Basic.NET
          Private Sub SaveToSqlDatabase(ByVal image As AtalaImage)    Dim myConnection As SqlConnection = Nothing    Try        ' Save image to byte array.        Dim imagedata() As Byte = image.ToByteArray(New Atalasoft.Imaging.Codec.JpegEncoder(75))                ' Create the SQL statement to add the image data.        myConnection = New SqlConnection(CONNECTION_STRING)        Dim myCommand As SqlCommand = New SqlCommand                ("INSERT INTO Atalasoft_Image_Database (Caption, ImageData) VALUES ('" + txtCaption.Text + "', @Image)", myConnection)        Dim myParameter As SqlParameter = New SqlParameter("@Image", SqlDbType.Image, imagedata.Length)        myParameter.Value = imagedata        myCommand.Parameters.Add(myParameter)                ' Open the connection and execture the statement.        myConnection.Open()        myCommand.ExecuteNonQuery()    Finally        myConnection.Close()    End TryEnd SubRead from a Database
          C#
          private AtalaImage OpenFromSqlDatabase(){    SqlConnection myConnection = null;    try    {        // Establish connection and SELECT statement.        myConnection = new SqlConnection(CONNECTION_STRING);        SqlCommand myCommand = new SqlCommand                ("SELECT ImageData FROM Atalasoft_Image_Database WHERE Caption = '" + txtCaption.Text + "'", myConnection);        myConnection.Open();                // Get the image from the database.        byte[] imagedata = (byte[])myCommand.ExecuteScalar();        if (imagedata != null)        {            AtalaImage image = AtalaImage.FromByteArray(imagedata);            return image;        }        else        {            MessageBox.Show("Image does not exist in database.");            return null;        }    }    finally    {        myConnection.Close();    }}Visual Basic .NET
          Private Function OpenFromSqlDatabase() As AtalaImage    Dim myConnection As SqlConnection = Nothing    Try        ' Establish connection and SELECT statement.        myConnection = New SqlConnection(CONNECTION_STRING)        Dim myCommand As SqlCommand = New SqlCommand                ("SELECT ImageData FROM Atalasoft_Image_Database WHERE Caption = '" + txtCaption.Text + "'", myConnection)        myConnection.Open()                ' Get the image from the database.        Dim imagedata() As Byte = CType(myCommand.ExecuteScalar(), Byte())        If (Not imagedata Is Nothing) Then            Dim image As AtalaImage = AtalaImage.FromByteArray(imagedata)            Return image        Else            MessageBox.Show("Image does not exist in database.")            Return Nothing        End If    Finally        myConnection.Close()    End TryEnd Function
          Just use it

          Comment

          • OuTCasT
            Contributor
            • Jan 2008
            • 374

            #6
            Originally posted by dwadish
            Code:
            
            Atalasoft dotImage has streaming capabilities that can be used jointly with ADO.NET to read and write images directly to a database without saving to a temporary file. The following code snippets demonstrates this in C# and VB.NET.
            
            Write to a Database
            C#
            private void SaveToSqlDatabase(AtalaImage image){SqlConnection myConnection = null;    try    {        // Save image to byte array.        byte[] imagedata = image.ToByteArray(new Atalasoft.Imaging.Codec.JpegEncoder(75));                // Create the SQL statement to add the image data.        myConnection = new SqlConnection(CONNECTION_STRING);        SqlCommand myCommand = new SqlCommand                ("INSERT INTO Atalasoft_Image_Database (Caption, ImageData) VALUES ('" + txtCaption.Text + "', @Image)", myConnection);        SqlParameter myParameter = new SqlParameter("@Image", SqlDbType.Image, imagedata.Length);        myParameter.Value = imagedata;        myCommand.Parameters.Add(myParameter);                // Open the connection and execture the statement.        myConnection.Open();        myCommand.ExecuteNonQuery();    }    finally    {        myConnection.Close();    }}Visual Basic.NET
            Private Sub SaveToSqlDatabase(ByVal image As AtalaImage)    Dim myConnection As SqlConnection = Nothing    Try        ' Save image to byte array.        Dim imagedata() As Byte = image.ToByteArray(New Atalasoft.Imaging.Codec.JpegEncoder(75))                ' Create the SQL statement to add the image data.        myConnection = New SqlConnection(CONNECTION_STRING)        Dim myCommand As SqlCommand = New SqlCommand                ("INSERT INTO Atalasoft_Image_Database (Caption, ImageData) VALUES ('" + txtCaption.Text + "', @Image)", myConnection)        Dim myParameter As SqlParameter = New SqlParameter("@Image", SqlDbType.Image, imagedata.Length)        myParameter.Value = imagedata        myCommand.Parameters.Add(myParameter)                ' Open the connection and execture the statement.        myConnection.Open()        myCommand.ExecuteNonQuery()    Finally        myConnection.Close()    End TryEnd SubRead from a Database
            C#
            private AtalaImage OpenFromSqlDatabase(){    SqlConnection myConnection = null;    try    {        // Establish connection and SELECT statement.        myConnection = new SqlConnection(CONNECTION_STRING);        SqlCommand myCommand = new SqlCommand                ("SELECT ImageData FROM Atalasoft_Image_Database WHERE Caption = '" + txtCaption.Text + "'", myConnection);        myConnection.Open();                // Get the image from the database.        byte[] imagedata = (byte[])myCommand.ExecuteScalar();        if (imagedata != null)        {            AtalaImage image = AtalaImage.FromByteArray(imagedata);            return image;        }        else        {            MessageBox.Show("Image does not exist in database.");            return null;        }    }    finally    {        myConnection.Close();    }}Visual Basic .NET
            Private Function OpenFromSqlDatabase() As AtalaImage    Dim myConnection As SqlConnection = Nothing    Try        ' Establish connection and SELECT statement.        myConnection = New SqlConnection(CONNECTION_STRING)        Dim myCommand As SqlCommand = New SqlCommand                ("SELECT ImageData FROM Atalasoft_Image_Database WHERE Caption = '" + txtCaption.Text + "'", myConnection)        myConnection.Open()                ' Get the image from the database.        Dim imagedata() As Byte = CType(myCommand.ExecuteScalar(), Byte())        If (Not imagedata Is Nothing) Then            Dim image As AtalaImage = AtalaImage.FromByteArray(imagedata)            Return image        Else            MessageBox.Show("Image does not exist in database.")            Return Nothing        End If    Finally        myConnection.Close()    End TryEnd Function
            Just use it
            THanks for the code, what imports are needed for atala image ?

            Comment

            • OuTCasT
              Contributor
              • Jan 2008
              • 374

              #7
              Originally posted by OuTCasT
              THanks for the code, what imports are needed for atala image ?
              Went onto the website.....no worries

              Comment

              Working...