求把图片存入数据库和从数据库还原的程序
想把图片转化成流写入数据库,并能从数据库读出并重新转换回图片,但重新转换试了N次都失败,不能转换回原来的团片,请大侠指点一下,具体怎样实现,谢谢
[解决办法]
把图片存入ACCESS数据库二进制字段
OpenFileDialog openfile = new OpenFileDialog();
String FileName = " ";
if (openfile.ShowDialog() == DialogResult.OK)
{
FileName = openfile.FileName;
this.pictureBox1.Image = Image.FromFile(FileName);
FileStream objFileStream;
objFileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read);
byte[] fileData = new byte[objFileStream.Length];
string strConnection = @ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\db1.mdb ";
//把文件流填充到数组
objFileStream.Read(fileData, 0, fileData.Length);
OleDbConnection connection = new OleDbConnection(strConnection);
OleDbCommand cmd = new OleDbCommand( "update My_Main set My_Text= " + "@Image where My_ID=40 ", connection);
cmd.Parameters.Add(new OleDbParameter( "@Image ", fileData));
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
objFileStream.Close();
}
从ACCESS数据库二进制字段还原图片
string strDSN = @ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\db1.mdb ";
string strSQL = "select * from My_Main where My_ID = 40 ";
OleDbConnection myConn = new OleDbConnection(strDSN);
OleDbCommand myCmd = new OleDbCommand(strSQL, myConn);
OleDbDataReader datareader = null;
try
{
myConn.Open();
datareader = myCmd.ExecuteReader();
if (datareader.Read())
{
if (datareader[ "My_Text "] is System.Byte[])
{
ImageConverter imc = new ImageConverter();
pictureBox1.image = imc.ConvertFrom(null, System.Globalization.CultureInfo.CurrentCulture, datareader[ "My_Text "]) as Image;
}
}
}
catch
{
}
finally
{
myConn.Close();
}
[解决办法]
//入SQL Server
using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
class BLOBDemo
{
[STAThread]
static void Main(string[] args)
{
Add( "Test ", "2.jpg ");
}
public static void Add(string categoryName, string filePath)
{
//byte [] photo = GetPhoto(filePath);
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte [] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
SqlConnection cn = new SqlConnection( "Data Source = (local);Integrated Security = SSPI;Initial Catalog=Northwind ");
SqlCommand cmd = new SqlCommand( "INSERT INTO Categories(CategoryName, Picture) VALUES (@CategoryName, @Picture) ", cn);
cmd.Parameters.Add( "@CategoryName ", SqlDbType.NVarChar, 15).Value = categoryName;
cmd.Parameters.Add( "@Picture ", SqlDbType.Image, photo.Length).Value = photo;
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
public static byte [] GetPhoto(string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte [] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
return photo;
}
}
[解决办法]
Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组
Stream StreamObject = UpFile.InputStream; //建立数据流对像
//读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
StreamObject.Read(FileByteArray,0,FileLength);
//建立SQL Server链接
SqlConnection Con = new SqlConnection( "Data Source=Localhost;Initial
Catalog=testdb;User ID=sa;Pwd=; ");
String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType,
ImageDescription, ImageSize) valueS (@Image, @ContentType,
@ImageDescription, @ImageSize) ";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add( "@Image ",SqlDbType.Binary, FileLength).value =
FileByteArray;
CmdObj.Parameters.Add( "@ContentType ", SqlDbType.VarChar,50).value =
UpFile.ContentType; //记录文件类型
//把其它单表数据记录上传
CmdObj.Parameters.Add( "@ImageDescription ", SqlDbType.VarChar,200).value =
txtDescription.Text;
//记录文件长度,读取时使用
CmdObj.Parameters.Add( "@ImageSize ", SqlDbType.BigInt,8).value =
UpFile.ContentLength;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();