Home > Database > Mysql Tutorial > C# 存取SqlServer中的Image类型

C# 存取SqlServer中的Image类型

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:35:00
Original
1792 people have browsed it

有时候我们需要将图片存到数库中的Imgae类型的字段下,以下是存取的两种方法: 存: public void Save() { using(System.IO.FileStream stream = new System.IO.FileStreamfile,System.IO.FileMode.Open,System.IO.FileAccess.Read) { byte[] buffer = new byte

有时候我们需要将图片存到数库中的Imgae类型的字段下,以下是存取的两种方法:

存:

public void Save()

{

using(System.IO.FileStream stream = new System.IO.FileStreamfile,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);
   SqlCommand cmd = new SqlCommand("Insert into Temp(name,photo) values(@name,@image)", sqlConn);
   cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = strName;
   cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer;
   cmd.ExecuteNonQuery();

}

}

取:

public void GetImage()

{

SqlCommand cmd = new SqlCommand(@"SELECT name, photo FROM Temp", sqlConn);
sqlConn.Open();
SqlDataReader reader = cmd .ExecuteReader();
if (reader.Read())
{
image_filename= (string) reader.GetValue(0);
byte[] image_bytes = (byte[]) reader.GetValue(1);
MemoryStream ms = new MemoryStream(image_bytes);
Bitmap bmap = new Bitmap(ms);
return bmap;

}

}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template