My aim is to insert images into my sql server database. image column in database defined as varbinary(max) type but when I execute the command, the Image column contains only the first value in the byte array!
Insert Image Script:
public void InsertImage()
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
Image img = Image.FromFile(@"D:\MyProjects\Registry Biometrics Insight\FingerPrintImages\1_2.bmp");
byte[] arr;
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
arr = ms.ToArray();
}
DBLayer dblayer = new DBLayer(CS);
SqlParameter[] sqlParams = new SqlParameter[1];
sqlParams[0] = new SqlParameter("@Image", SqlDbType.VarBinary);
sqlParams[0].Value = arr;
dblayer.M_ExecuteSQLCommand("prc_Fingers_InsertImage", sqlParams);
}
ExecuteSQLCommand Script:
public int M_ExecuteSQLCommand(string StoredProcedureName, SqlParameter[] Parameters)
{
SqlCommand comm;
try
{
using (SqlConnection conn = new SqlConnection(_P_ConnectionString))
{
comm = new SqlCommand();
comm.Connection = conn;
comm.CommandType = System.Data.CommandType.StoredProcedure;
comm.CommandText = StoredProcedureName;
comm.CommandTimeout = 0;
comm.Parameters.Clear();
for (int i = 0; i < Parameters.Length; i++)
{
comm.Parameters.Add( Parameters[i] );
}
conn.Open();
comm.ExecuteNonQuery();
return 1;
}
}
catch (Exception ex)
{
throw ex;
}
}
My Sorted Procedure:
Procedure [dbo].[prc_Fingers_InsertImage]
(@Image varbinary = null)
as
begin
INSERT INTO [dbo].[Fingers]
([Image])
VALUES
(@Image)
end
Why did I get this result instead of saving all values from byte array??