I am saving a image into database with byte[]
property like this:
ProfileModel:
private byte[] _imageData;
public byte[] ImageData
{
get
{
return _imageData;
}
set
{
_imageData = value;
}
}
When I am trying to read image i have problem with converting of byte. I dont know how to convert reader into byte[]
. I thought that its enough to convert.ToByte, but it doesnt works.
ThreadModel:
private byte _imageData;
public byte ImageData
{
get
{
return _imageData;
}
set
{
_imageData = value;
}
}
Its my method where I am reading image information:
cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = @"SELECT ImageData, "
+ " ContentType, "
+ " ImageName "
+ " FROM UsersImage "
+ " WHERE UserName = @UserName ";
cmd.Parameters.AddWithValue("@UserName", ThreadUserName);
reader = cmd.ExecuteReader();
if (reader.Read())
{
if (reader["ContentType"] != DBNull.Value)
{
ContentType = Convert.ToString(reader["ContentType"]);
}
if (reader["ImageName"] != DBNull.Value)
{
ImageName = Convert.ToString(reader["ImageName"]);
}
if (reader["ImageData"] != DBNull.Value)
{
ImageData = Convert.ToByte(reader["ImageData"]);
}
int affectedRows = cmd.ExecuteNonQuery();
if (affectedRows != 1)
{
}
}
reader.Close();