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. inserting an image into a database

inserting an image into a database

Scheduled Pinned Locked Moved C#
databasehelpdata-structuresquestion
2 Posts 2 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.
  • O Offline
    O Offline
    Ofori Boadu
    wrote on last edited by
    #1

    I am trying to save in image into an MS SQl amd keep getting this error "Must Declare the Scalar Variable" below is my code for the insertion; OdbcCommand cmd = new OdbcCommand("INSERT INTO dbo.photo (photo) VALUES (@BLOBData)", myConnection); //Save image from PictureBox into MemoryStream object. MemoryStream ms = new MemoryStream(); picture.Save(ms, ImageFormat.Jpeg); //Read from MemoryStream into Byte array. Byte[] bytBLOBData = new Byte[ms.Length]; ms.Position = 0; ms.Read(bytBLOBData, 0, Convert.ToInt32(ms.Length)); //Create parameter for insert statement that contains image. OdbcParameter prm = new OdbcParameter("@BLOBData", OdbcType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false,0, 0, null, DataRowVersion.Current, bytBLOBData); cmd.Parameters.Add(prm); myConnection.Open(); cmd.ExecuteNonQuery(); myConnection.Close(); } catch (OdbcException ex) { MessageBox.Show(ex.Message); } Any help?

    a novice

    G 1 Reply Last reply
    0
    • O Ofori Boadu

      I am trying to save in image into an MS SQl amd keep getting this error "Must Declare the Scalar Variable" below is my code for the insertion; OdbcCommand cmd = new OdbcCommand("INSERT INTO dbo.photo (photo) VALUES (@BLOBData)", myConnection); //Save image from PictureBox into MemoryStream object. MemoryStream ms = new MemoryStream(); picture.Save(ms, ImageFormat.Jpeg); //Read from MemoryStream into Byte array. Byte[] bytBLOBData = new Byte[ms.Length]; ms.Position = 0; ms.Read(bytBLOBData, 0, Convert.ToInt32(ms.Length)); //Create parameter for insert statement that contains image. OdbcParameter prm = new OdbcParameter("@BLOBData", OdbcType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false,0, 0, null, DataRowVersion.Current, bytBLOBData); cmd.Parameters.Add(prm); myConnection.Open(); cmd.ExecuteNonQuery(); myConnection.Close(); } catch (OdbcException ex) { MessageBox.Show(ex.Message); } Any help?

      a novice

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

      Why are you using ODBC with sql server? Here is how to insert images in sql server database: How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET[^]

      Giorgi Dalakishvili #region signature my articles #endregion

      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