Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how I can insert image into sqlserver database with c#

how I can insert image into sqlserver database with c#

Scheduled Pinned Locked Moved C#
csharpdatabase
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yemen_programmer
    wrote on last edited by
    #1

    how I can insert image into sqlserver database with c#.Ineed the code

    G K 2 Replies Last reply
    0
    • Y yemen_programmer

      how I can insert image into sqlserver database with c#.Ineed the code

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      Here you go: How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET[^]

      #region signature my articles #endregion

      1 Reply Last reply
      0
      • Y yemen_programmer

        how I can insert image into sqlserver database with c#.Ineed the code

        K Offline
        K Offline
        Khoramdin
        wrote on last edited by
        #3

        Hello yemen_programmer, As far as I know, there are TWO ways to do so. 1- To store only the location of the image into your database and the image file in a different folder. 2- To store the image file as a BLOB file. The option 1 is stright forward as you only store a text into your database while your image sits in a folder. Therefore, I am going to leave that. The second option can be done with a code similar to the one here. Keep in mind that this line of code uses MySQL database and the command line "INSERT INTO ....." may be different for SQL and you need to check that out. I personaly use MySQL. The following code also includes storing other information about the stored image. But the most important one that you require is ItemImage and ImgeSize. OdbcCom = new System.Data.Odbc.OdbcCommand("INSERT INTO item VALUES (NULL, ?, ?, ?, ?);", OdbcCon); OdbcCom.Parameters.Add("@ItemName", System.Data.Odbc.OdbcType.VarChar); OdbcCom.Parameters["@ItemName"].Value = txtItemName.Text; OdbcCom.Parameters.Add("@ItemImage", System.Data.Odbc.OdbcType.Image); OdbcCom.Parameters["@ItemImage"].Value = ImageArray; OdbcCom.Parameters.Add("@ImageSize", System.Data.Odbc.OdbcType.Int); OdbcCom.Parameters["@ImageSize"].Value = FileSize; OdbcCom.Parameters.Add("@ItemInfo", System.Data.Odbc.OdbcType.VarChar); OdbcCom.Parameters["@ItemInfo"].Value = txtItemInfo.Text; OdbcCom.ExecuteNonQuery(); Keep in mind that before being able to do so, you need to place your image in an Array which in the above code is called ImageArray. I hope that could help you out to get your image into your database. Cheers, Khoramdin -- modified at 14:49 Wednesday 21st November, 2007

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups