Access保存图片
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data.OleDb;
namespace Files2Access
{
/// <summary>
/// 任意文件保存到access数据库中
/// 注意:数据库格式!!!
/// </summary>
public class File2Access
{
private string accessName;
/// <summary>
/// 指定access路径
/// </summary>
/// <param name="s">路径名</param>
public File2Access(string s)
{
this.accessName = s;
}
/// <summary>
///使用默认access路径
/// </summary>
public File2Access()
{
this.accessName =@"E:\mine\tiku.mdb";
}
/// <summary>
/// 将文件保存到access数据库中
/// </summary>
/// <param name="access">access数据库的路径</param>
/// <param name="file">要保存的文件</param>
/// <returns></returns>
public bool insert2access(FileInfo file)
{
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+accessName);
if (file.Exists)
{
byte[] bdata = null;
using (FileStream fs = file.OpenRead())
{
bdata = new byte[file.Length];
int nReadLength = fs.Read(bdata, 0, (int)file.Length);
}
string strQuery = "INSERT INTO StorageQ "
+ " ( FileName, Content) "
+ " VALUES "
+ " ( @FileName, @Content ) ";
OleDbCommand comm = new OleDbCommand(strQuery, conn);
comm.Parameters.Add("@FileName", file.Name);
comm.Parameters.Add("@Content", bdata);
conn.Open();
comm.ExecuteNonQuery();
return true;
}
else
{
return false;
}
}
}
}
读出文件
源自:
http://www.net0and1.com/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data.OleDb;
namespace Files2Access
{
public class access2file
{
private string fileName;
private string accessName;
public access2file(string s1,string s2)
{
fileName = s1;
accessName = s2;
}
public access2file()
{
fileName = @"c:\noname";
this.accessName = @"E:\mine\tiku.mdb";
}
public bool extractFile(string uNeedFileName)
{
try
{
byte[] bData = null;
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + accessName);
string strQuery = "SELECT Content FROM StorageQ "
+ " WHERE FileName = " +"'"+ uNeedFileName+"'";
conn.Open();
OleDbCommand comm = new OleDbCommand(strQuery, conn);
OleDbDataReader dr = comm.ExecuteReader();
while (dr.Read())
{
bData = (byte[])dr["Content"];
}
conn.Close();
dr.Close();
if (bData != null)
{
// Save file
FileInfo fi = new FileInfo(fileName);
if (!fi.Exists)
{
//Create the file.
using (FileStream fs = fi.Create())
{
fs.Write(bData, 0, bData.Length);
}
}
else
{
//Create the file.
using (FileStream fs = fi.OpenWrite())
{
fs.Write(bData, 0, bData.Length);
}
}
}
return true;
}
catch (Exception e)
{
return false;
}
}
}
}