![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//存:
public void addphoto2(string file)
{
//将文件名为file的文件读入到buffer中
System.IO.FileStream stream = new System.IO.FileStream(file,System.IO.FileMode.Open,System.IO.FileAccess.Read);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
stream.Close();
string strName = System.IO.Path.GetFileNameWithoutExtension(file);
//name是图片的名称,photo是image类型的
SqlCommand cmd = new SqlCommand("Insert into test(name,photo) values(@name,@image)", sqlConn);
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = strName;
cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer;
cmd.ExecuteNonQuery();
}
//取:
string image_filename=string.Empty;
public Bitmap image_query()
{
SqlCommand cmd = new SqlCommand(@"SELECT name, photo FROM test", sqlConn);
// Open the connection and read data into the DataReader.
sqlConn.Open();
SqlDataReader image_reader = cmd .ExecuteReader();
if (image_reader.Read())
{
image_filename= (string) image_reader.GetValue(0);
byte[] image_bytes = (byte[]) image_reader.GetValue(1);
MemoryStream ms = new MemoryStream(image_bytes);
Bitmap bmap = new Bitmap(ms);
return bmap;
}
}