Word、Excel、PDF存入oracle 数据库 联系客服

发布时间 : 星期一 文章Word、Excel、PDF存入oracle 数据库更新完毕开始阅读b63bb3534531b90d6c85ec3a87c24028915f8532

Word、Excel、PDF存入oracle 数据库

Oracle字段类型BLOB就是用来存储大的二进制文件的,我们可以用此来把Word、Excel、PDF文件直接存入数据库,下面以PDF文件为例: 代码如下:

private void btnUp_Click(object sender, EventArgs e) { try {

//打开对话框

OpenFileDialog dialog = new OpenFileDialog(); if (dialog.ShowDialog() == DialogResult.OK) {

this.txtFileAddress.Text = dialog.FileName;

FileInfo info = new FileInfo(txtFileAddress.Text); //获得文件大小

fileSize = info.Length;

//提取文件名,三步走

int index = info.FullName.LastIndexOf(\ fileName = info.FullName.Remove(index);

fileName = fileName.Substring(fileName.LastIndexOf(@\1);

//获得文件扩展名

fileType = info.Extension.Replace(\ //把文件转换成二进制流

fl = new byte[Convert.ToInt32(fileSize)];

FileStream file = new FileStream(txtFileAddress.Text, FileMode.Open, FileAccess.Read);

read = new BinaryReader(file);

read.Read(fl, 0, Convert.ToInt32(fileSize)); } }

catch (Exception ex) {

MessageBox.Show(\选择文件时候发生了 \ fl = null; } }

private void UpDataBase()

{

if (this.txtFileAddress.Text.Length <= 0) {

MessageBox.Show(this, \请选择一个文件\文件地址不合法\MessageBoxButtons.OK, MessageBoxIcon.Information,

MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading); return; }

if (fileSize > 0) {

string[] type = { \

bool exists = ((IList)type).Contains(fileType.ToLower());

if (!exists) {

MessageBox.Show(\文档格式不对!只能为pdf格式。\提示对话框\MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } try {

string connString = \Source=orcl;\

OracleConnection conn = new OracleConnection(connString); conn.Open();

OracleTransaction trans = conn.BeginTransaction(); OracleCommand OrlCommand = conn.CreateCommand(); OrlCommand.Transaction = trans;

OrlCommand.CommandText = \where cotrid = :cotrid\

OrlCommand.Parameters.Clear();

OrlCommand.Parameters.Add(\ OrlCommand.Parameters.Add(\OracleType.VarChar).Value = this.txtCotrID.Text;

if (OrlCommand.ExecuteNonQuery() > 0) {

MessageBox.Show(fileName + \保存成功!\ trans.Commit(); conn.Close(); conn.Dispose(); } else

{

MessageBox.Show(\发生错误!!\ trans.Rollback(); conn.Close(); } }

catch (Exception ex) {

MessageBox.Show(\保存 \+ fileName + \时候发生了 \+ ex.Message); } }

此方法主要是二进制流直接存入oracle数据库BLOB类型字段,占用内存,其实还可以保存路径,然后需要时就调用路径,读取pdf等文件。