氧气中毒 - Asp.Net

If you don't believe in yourself,Nobody else will!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

c# ,在Oracle 中,对 blob 类型对象的操作

Posted on 2006-03-06 16:56  氧气中毒  阅读(2196)  评论(0)    收藏  举报
尝试的情况是,提供一个TEXT 框,让用户输入,允许输入非常多的值,保存到Oracle 数据库中。 为了能够大量数据保存,因此,对字段类型设置为 blob 型 。 
网络上的类似帮助太复杂了,发现如果只是文件内的操作,还是比较简单的。 
思路: ORACLE中BLOB类型是以二进制流的形式来存储信息,所以数据保存时,要将普通的文本类型转换为二进制.
1。先将text 文本转变成2进制 
byte[] ddd; 
ddd 
= System.Text.Encoding.Unicode.GetBytes(this.textBox1.Text);

2。再将该2进制存入数据库中,发现这种对数据库的访问方法可行。
cmd.Parameters.Add 。。。 
cmd.ExecuteNonQuery(); 

表结构如下:
create table XLUTEST 

HHHH BLOB, 
GGGG 
VARCHAR2(10

tablespace SYSTEM 
pctfree 
10 
pctused 
40 
initrans 
1 
maxtrans 
255 
storage 

initial 64K 
minextents 
1 
maxextents unlimited 


c# 全部WINFORM 代码如下 :
using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
using System.Data.OleDb; 
using System.IO; 

namespace blob 

/// <summary> 
/// Form1 的摘要说明。 
/// </summary> 

public class Form1 : System.Windows.Forms.Form 

private System.Windows.Forms.TextBox textBox1; 
private System.Windows.Forms.Button button1; 
private System.Windows.Forms.Button button2; 
private System.Windows.Forms.Button button3; 
/// <summary> 
/// 必需的设计器变量。 
/// </summary> 

private System.ComponentModel.Container components = null

public Form1() 

// 
// Windows 窗体设计器支持所必需的 
// 
InitializeComponent(); 

// 
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
// 
}
 

/// <summary> 
/// 清理所有正在使用的资源。 
/// </summary> 

protected override void Dispose( bool disposing ) 

if( disposing ) 

if (components != null

components.Dispose(); 
}
 
}
 
base.Dispose( disposing ); 
}
 

Windows 窗体设计器生成的代码 

/// <summary> 
/// 应用程序的主入口点。 
/// </summary> 

[STAThread] 
static void Main() 

Application.Run(
new Form1()); 
}
 

private void button1_Click(object sender, System.EventArgs e) 


string cnnstr = "provider=OraOLEDB.Oracle;data source=yourdb;User Id=xxxx;Password=xxxx;"
OleDbConnection con 
= new OleDbConnection(cnnstr); 
try 

con.Open(); 
}
 
catch 
{} 
OleDbCommand cmd 
= new OleDbCommand(cnnstr,con); 

cmd.CommandType
=CommandType.Text; 
cmd.CommandText
=cnnstr; 

string txvalue = this.textBox1.Text.Trim(); 


//将textBox1.Text转换为二进制类型
byte[] expbyte = System.Text.Encoding.Unicode.GetBytes(txvalue); 

cmd.CommandText 
= " insert into xlutest ( hhhh ) values (:hhhh) "

cmd.Parameters.Add(
"hhhh",System.Data.OleDb.OleDbType.Binary,expbyte.Length); 
cmd.Parameters [
0].Value = expbyte; 

try 

cmd.ExecuteNonQuery(); 
MessageBox.Show(
"OK"); 
}
 
catch ( System.Exception e1 ) 

MessageBox.Show(e1.Message ); 
}
 


}
 


//类型声明里面要规定好是byte[] byteBlob
public void ExecwithReturnBinary(string cmdText,string BinaryContent,byte[] byteBlob) 


string cnnstr = "provider=OraOLEDB.Oracle;data source=yourdb;User Id=xxxx;Password=xxxx;"
OleDbConnection con 
= new OleDbConnection(cnnstr); 
try 

con.Open(); 
}
 
catch 
{} 
OleDbCommand cmd 
= new OleDbCommand(cmdText,con); 

cmd.CommandType
=CommandType.Text; 
cmd.CommandText
=cmdText; 
cmd.Parameters.Add(
"str",System.Data.OleDb.OleDbType.VarChar,10).Value = "sdfsddf"
cmd.Parameters.Add(BinaryContent,System.Data.OleDb.OleDbType.Binary ,byteBlob.Length).Value
= byteBlob; 
cmd.ExecuteNonQuery(); 
}
 


private void save() 

// byte[] abyte; 
// string str = this.textBox1.Text; 
// System.IO.FileStream fs = new FileStream("c:\\xxx.txt",System.IO.FileMode.Open); 
// System.IO.BinaryReader br = new BinaryReader(fs); 
// abyte = br.ReadBytes(fs.Length); 
// br.Close(); 


}
 

private void button2_Click(object sender, System.EventArgs e) 

string strSQL = "select gggg,hhhh from xlutest "
string cnnstr = "provider=OraOLEDB.Oracle;data source=yourdb;User Id=xxxx;Password=xxxx;"
OleDbConnection con 
= new OleDbConnection(cnnstr); 
try 

con.Open(); 
}
 
catch 
{} 
OleDbCommand cmd 
= new OleDbCommand(strSQL,con); 
System.Data.OleDb.OleDbDataReader dr 
= cmd.ExecuteReader(); 
while ( dr.Read()) 



//读取BLOB数据时也要将其转换回文本类型
string dd =dr ["gggg"].ToString(); 
byte[] ooo = (byte[])dr["hhhh"]; 
string str ; 
str 
= System.Text.Encoding.Unicode.GetString(ooo); 
this.textBox1.Text =str; 

}
 
}
 

private void button3_Click(object sender, System.EventArgs e) 

//Decode 
// string str ; 
// str = System.Text.Encoding.Unicode.GetString(ddd); 

string cnnstr = "provider=OraOLEDB.Oracle;data source=yourdb;User Id=xxxx;Password=xxxx;"
OleDbConnection con 
= new OleDbConnection(cnnstr); 

byte[] ddd; 
ddd 
= System.Text.Encoding.Unicode.GetBytes(this.textBox1.Text); 
string strSQL = "update xlutest set hhhh=:ddd "
try 

con.Open(); 
}
 
catch 
{} 
OleDbCommand cmd 
= new OleDbCommand(cnnstr,con); 

cmd.CommandType
=CommandType.Text; 
cmd.CommandText
=strSQL; 
cmd.Parameters.Add(
"ddd",System.Data.OleDb.OleDbType.Binary ,ddd.Length).Value= ddd; 
cmd.ExecuteNonQuery(); 
MessageBox.Show(
"OK!"); 
}
 

}
 
}