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