/// <summary>
/// 向SQLSERVER数据库中写入图片
/// </summary>
/// <param name="fileStream">文件流</param>
/// <returns></returns>
public int WriteImage(FileStream fileStream)
{
try
{
var buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, (int)fileStream.Length);
fileStream.Close();
string strSql = "INSERT INTO DemoTab VALUES(@imageData)";
SqlCommand cmd = new SqlCommand(strSql,sqlConn);
cmd.Parameters.Add("@imageData", SqlDbType.Image).Value = buffer;
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Log.WriteLog(ex.Message);
return -1;
}
finally
{
CloseConn();
}
}
/// <summary>
/// 读取SQLSERVER表中Image类型的数据
/// </summary>
/// <returns></returns>
public Bitmap ReadImage()
{
try
{
string strSql = "SELECT TOP 1 ImageData FROM DemoTab";
SqlCommand sqlCmd = new SqlCommand(strSql,sqlConn);
var byteImg = (byte[]) sqlCmd.ExecuteScalar();
MemoryStream memoryStream = new MemoryStream(byteImg);
Bitmap bitmap = new Bitmap(memoryStream);
return bitmap;
}
catch (Exception ex)
{
Log.WriteLog(ex.Message);
return null;
}
finally
{
CloseConn();
}
}