1. 将图片转换成Byte[] //Open file into a filestream and //read data in a byte array. byte[] ReadFile(string sPath) { //Initialize byte array with a null value initially. byte[] data = null; //Use FileInfo object to get file size. FileInfo fInfo = new FileInfo(sPath); long numBytes = fInfo.Length; //Open FileStream to read file FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read); //Use BinaryReader to read file stream into byte array. BinaryReader br = new BinaryReader(fStream); //When you use BinaryReader, you need to //supply number of bytes to read from file. //In this case we want to read entire file. //So supplying total number of bytes. data = br.ReadBytes((int)numBytes); return data; } 2. 保存图片到数据库private void cmdSave_Click(object sender, EventArgs e) { try { //Read Image Bytes into a byte array byte[] imageData = ReadFile(txtImagePath.Text); //Initialize SQL Server Connection SqlConnection CN = new SqlConnection(txtConnectionString.Text); //Set insert query string qry = "insert into ImagesStore (OriginalPath,ImageData) _ values(@OriginalPath, @ImageData)"; //Initialize SqlCommand object for insert. SqlCommand SqlCom = new SqlCommand(qry, CN); //We are passing Original Image Path and //Image byte data as sql parameters. SqlCom.Parameters.Add(new SqlParameter("@OriginalPath", (object)txtImagePath.Text));
SqlCom.Parameters.Add(new SqlParameter("@ImageData", (object)imageData)); //Open connection and execute insert query. CN.Open(); SqlCom.ExecuteNonQuery(); CN.Close(); //Close form and return to list or images. this.Close(); } 3. 读取图片数据并写到文件里 SqlConnection con = new SqlConnection("Server=Darkover;uid=<username>;pwd=<strong password>;database=northwind"); SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con); SqlCommandBuilder MyCB = new SqlCommandBuilder(da); DataSet ds = new DataSet("MyImages"); byte[] MyData= new byte[0];
//read data in a byte array.
byte[] ReadFile(string sPath)
{
//Initialize byte array with a null value initially.
byte[] data = null; //Use FileInfo object to get file size.
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length; //Open FileStream to read file
FileStream fStream = new FileStream(sPath, FileMode.Open,
FileAccess.Read); //Use BinaryReader to read file stream into byte array.
BinaryReader br = new BinaryReader(fStream); //When you use BinaryReader, you need to
//supply number of bytes to read from file.
//In this case we want to read entire file.
//So supplying total number of bytes.
data = br.ReadBytes((int)numBytes);
return data;
}
2. 保存图片到数据库private void cmdSave_Click(object sender, EventArgs e)
{
try
{
//Read Image Bytes into a byte array
byte[] imageData = ReadFile(txtImagePath.Text); //Initialize SQL Server Connection
SqlConnection CN = new SqlConnection(txtConnectionString.Text); //Set insert query
string qry = "insert into ImagesStore (OriginalPath,ImageData) _
values(@OriginalPath, @ImageData)"; //Initialize SqlCommand object for insert.
SqlCommand SqlCom = new SqlCommand(qry, CN); //We are passing Original Image Path and
//Image byte data as sql parameters.
SqlCom.Parameters.Add(new SqlParameter("@OriginalPath",
(object)txtImagePath.Text));
SqlCom.Parameters.Add(new SqlParameter("@ImageData",
(object)imageData)); //Open connection and execute insert query.
CN.Open();
SqlCom.ExecuteNonQuery();
CN.Close(); //Close form and return to list or images.
this.Close();
}
3. 读取图片数据并写到文件里
SqlConnection con = new SqlConnection("Server=Darkover;uid=<username>;pwd=<strong password>;database=northwind");
SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImages"); byte[] MyData= new byte[0];
da.Fill(ds, "MyImages");
DataRow myRow;
myRow=ds.Tables["MyImages"].Rows[0];
MyData = (byte[])myRow["imgField"];
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0); FileStream fs = new FileStream(@"C:\winnt\Gone Fishing2.BMP", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0,ArraySize);
fs.Close();