namespace WindowsApplication1
{
/// <summary>
/// Blob 的摘要说明。
/// </summary>
public class Blob
{
public Blob()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void InsertBlob() //插入Blob内容
{
//获取提交数据库的文件内容。
OracleLob blob = this.GetTempBLobs();
//文件内容是否为空
if(blob.IsNull)
{return;}
//向数据库提交内容。
OracleConnection conn = new OracleConnection(ConfigInfo.GetConnectionString());
if(conn.State == ConnectionState.Closed)
{
conn.Open();
}
OracleTransaction transaction =conn.BeginTransaction();
string Create="insert into tablewithlobs(a,b) values(tablewithlobs_seq.nextval,:Blob)";
OracleParameter[] param=new OracleParameter[1];
param[0]=new OracleParameter("Blob",OracleType.Blob);
param[0].Value = blob;
OracleHelper.ExecuteNonQuery(transaction,CommandType.Text,Create,param);
transaction.Commit();
conn.Close();
}
private OracleLob GetTempBLobs() //获取提交数据库的文件内容
{
//输入文件内容
Stream stream = Stream.Null;
OpenFileDialog openFileDialog = new OpenFileDialog();
string fileName = openFileDialog.FileName;
openFileDialog.CheckFileExists = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
stream = openFileDialog.OpenFile();
}
else
{
return OracleLob.Null;
}
stream.Position =0 ;
byte[] byte_buffer = new byte[stream.Length];
for(int i=0;i<stream.Length;i++)
{
byte_buffer[i]=(byte)stream.ReadByte();
}
//创建临时Blob变量
OracleType lobtype=OracleType.Blob;
string CreateTempBlob= "DECLARE A " + lobtype + "; "+
"BEGIN "+
"DBMS_LOB.CREATETEMPORARY(A, FALSE); "+
":LOC := A; "+
"END;";
OracleConnection conn = new OracleConnection(ConfigInfo.GetConnectionString());
if(conn.State == ConnectionState.Closed)
{
conn.Open();
}
OracleTransaction transaction = conn.BeginTransaction();
OracleParameter[] param=new OracleParameter[1];
param[0]=new OracleParameter("LOC",OracleType.Blob);
OracleHelper.CacheParameter(CreateTempBlob,param);
param[0].Direction = ParameterDirection.Output;
OracleHelper.ExecuteNonQuery(transaction,CommandType.Text,CreateTempBlob,param);
OracleLob tempLob = (OracleLob)param[0].Value;
//将文件内容传入Blob变量
tempLob.BeginBatch(OracleLobOpenMode.ReadWrite);
tempLob.Write(byte_buffer,0,byte_buffer.Length);
tempLob.Position = 0;
conn.Close();
return tempLob;
}
public void ReadBLob(decimal A) //读出Blob内容
{
//从数据库读出内容
string Read = "select b from tablewithlobs where a =:a";
OracleParameter[] param=OracleHelper.GetCacheParameter(Read);
if(param==null)
{
param=new OracleParameter[1];
param[0]=new OracleParameter("a",OracleType.Number);
OracleHelper.CacheParameter(Read,param);
}
param[0].Value = A;
OracleDataReader dr = OracleHelper.ExecuteReader(ConfigInfo.GetConnectionString(),CommandType.Text,Read,param);
OracleLob blob = OracleLob.Null;
while(dr.Read())
{
blob = dr.GetOracleLob(0);
}
//打开保存内容的文件
string filename =string.Empty;
byte[] buffer=null;
SaveFileDialog saveFileDialog = new SaveFileDialog();
if(saveFileDialog.ShowDialog() == DialogResult.OK)
{
filename = saveFileDialog.FileName;
}
if(filename == string.Empty)
{
MessageBox.Show("请确定保存的文件路径!");
return;
}
//将Blob类型的内容转化成文件流类型
buffer = new byte[blob.Length];
blob.Read(buffer,0,buffer.Length);
//将内容写入文件
FileStream fs= new FileStream(filename,FileMode.OpenOrCreate);
fs.Write(buffer,0,buffer.Length);
fs.Close();
}
public void UpdateBlob(decimal A) //更新Blob内容
{
//提交更新的文件内容
Stream stream = Stream.Null;
OpenFileDialog openFileDialog = new OpenFileDialog();
string fileName = openFileDialog.FileName;
openFileDialog.CheckFileExists = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
stream = openFileDialog.OpenFile();
}
else
{
return;
}
//获取要更新的数据库内容
string Update = "select b from tablewithlobs where a =:a for update";
OracleConnection conn = new OracleConnection(ConfigInfo.GetConnectionString());
conn.Open();
OracleTransaction transaction = conn.BeginTransaction();
OracleParameter[] param=OracleHelper.GetCacheParameter(Update);
if(param==null)
{
param=new OracleParameter[1];
param[0]=new OracleParameter("a",OracleType.Number);
OracleHelper.CacheParameter(Update,param);
}
param[0].Value = A;
OracleDataReader dr = OracleHelper.ExecuteReader(transaction,CommandType.Text,Update,param);
OracleLob blob = OracleLob.Null;
while(dr.Read())
{
blob = dr.GetOracleLob(0);
}
//向数据库写入更新内容
stream.Position =0 ;
byte[] byte_buffer = new byte[stream.Length];
for(int i=0;i<stream.Length;i++)
{
byte_buffer[i]=(byte)stream.ReadByte();
}
blob.Write(byte_buffer,0,byte_buffer.Length);
transaction.Commit();
conn.Close();
stream.Close();
}
public void DeleteBlob(decimal A) //删除Blob内容
{
OracleConnection conn = new OracleConnection(ConfigInfo.GetConnectionString());
conn.Open();
OracleTransaction transaction = conn.BeginTransaction();
string Delete="delete tablewithlobs where a=:a";
OracleParameter[] param=OracleHelper.GetCacheParameter(Delete);
if(param==null)
{
param=new OracleParameter[1];
param[0]=new OracleParameter("a",OracleType.Number);
OracleHelper.CacheParameter(Delete,param);
}
param[0].Value=A;
OracleHelper.ExecuteNonQuery(transaction,CommandType.Text,Delete,param);
transaction.Commit();
conn.Close();
MessageBox.Show("删除成功!");
}
}
}