Oracle数据库中保存文件(C#)

Oracle中有Blob和Clob可以保存大数据量。其中Blob是指二进制大对象也就是英文Binary Large Object的缩写,用来存储大量二进制数据。而Clob是指大字符对象是英文Character Large Object的缩写,用来存储大量文本数据。

1 数据库表

-- 创建文件表
create table tb_file
(
  id              number(20) not null,
  file_name       nvarchar2(100),
  file_content    blob,
  constraint pk_tb_file primary key (id)
)
tablespace mydb storage(
  initial 64K
  minextents 1
  maxextents unlimited
);

--设置tb_file主键自增
create sequence seq_tb_file    --创建自增序列
minvalue 1
maxvalue 9999999999999999999
start with 1
increment by 1
nocache
order;

create or replace trigger ai_tb_file  --创建触发器,当插入新记录时自增主键id
before insert on tb_file
for each row
when (new.id is null)
begin
select seq_tb_file.nextval into :new.id from dual;
end;

2 在数据库中保存和读取二进制文件

//添加命名空间
using System;
using System.Data.OracleClient;
using System.IO;
using System.Data;

/// <summary>
/// 把文件插入数据库中
/// </summary>
/// <param name="filePath">文件名,包含路径,如c:\temp.rar</param>
/// <returns></returns>
public int Insert(string filePath)
{
    string connStr = "data source=orcl;user=mydbuser;password=mydbuser;";
    OracleConnection conn = new OracleConnection(connStr);
    OracleCommand cmd = conn.CreateCommand();

    //读取文件
    FileStream fs = File.OpenRead(filePath);
    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer, 0, buffer.Length);

    OracleParameter paramFileContent = cmd.CreateParameter();
    paramFileContent.OracleType = OracleType.Blob;
    paramFileContent.ParameterName = "FileContent";
    paramFileContent.Direction = ParameterDirection.Input;
    paramFileContent.Value = buffer;
    cmd.Parameters.Add(paramFileContent);

    OracleParameter paramFileName = cmd.CreateParameter();
    paramFileName.OracleType = OracleType.VarChar;
    paramFileName.ParameterName = "FileName";
    paramFileName.Direction = ParameterDirection.Input;
    paramFileName.Value = Path.GetFileName(filePath);
    cmd.Parameters.Add(paramFileName);

    string sqlInsert = "insert into tb_file (file_name, file_content) values (:FileName, :FileContent)";
    cmd.CommandText = sqlInsert;
    cmd.CommandType = CommandType.Text;

    conn.Open();
    int result = cmd.ExecuteNonQuery();
    conn.Close();

    return result;
}


/// <summary>
/// 根据文件名从数据库中获取文件
/// </summary>
/// <param name="fileName">数据库中的文件名</param>
/// <param name="savePath">文件的保存路径,包括文件名,如c:\file.rar</param>
public void Select(string fileName, string savePath)
{
    string connStr = "data source=orcl;user=mydbuser;password=mydbuser;";
    OracleConnection conn = new OracleConnection(connStr);
    OracleCommand cmd = conn.CreateCommand();

    string sqlSelect = "select file_name, file_content from tb_file where file_name=:FileName";
    cmd.CommandText = sqlSelect;
    cmd.CommandType = CommandType.Text;

    OracleParameter paramFileName = cmd.CreateParameter();
    paramFileName.OracleType = OracleType.VarChar;
    paramFileName.ParameterName = "FileName";
    paramFileName.Direction = ParameterDirection.Input;
    paramFileName.Value = fileName;
    cmd.Parameters.Add(paramFileName);            

    conn.Open();
    OracleDataReader dr = cmd.ExecuteReader();
    dr.Read();
    byte[] buffer = (byte[])dr["file_content"];
    dr.Close();
    conn.Close();

	//把文件保存到指定路径
    File.WriteAllBytes(savePath, buffer);
}


你可能感兴趣的:(Oracle数据库中保存文件(C#))