c# ,在Oracle 中,對 blob 類型對象的操作
2024-07-21 02:18:28
供稿:網友
本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。嘗試的情況是,提供一個text 框,讓用戶輸入,允許輸入非常多的值,保存到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 );
}
#region windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void initializecomponent()
{
this.textbox1 = new system.windows.forms.textbox();
this.button1 = new system.windows.forms.button();
this.button2 = new system.windows.forms.button();
this.button3 = new system.windows.forms.button();
this.suspendlayout();
//
// textbox1
//
this.textbox1.anchor = system.windows.forms.anchorstyles.top;
this.textbox1.location = new system.drawing.point(40, 48);
this.textbox1.multiline = true;
this.textbox1.name = "textbox1";
this.textbox1.size = new system.drawing.size(576, 272);
this.textbox1.tabindex = 0;
this.textbox1.text = "textbox1";
//
// button1
//
this.button1.location = new system.drawing.point(240, 336);
this.button1.name = "button1";
this.button1.tabindex = 1;
this.button1.text = "save";
this.button1.click += new system.eventhandler(this.button1_click);
//
// button2
//
this.button2.location = new system.drawing.point(408, 344);
this.button2.name = "button2";
this.button2.tabindex = 2;
this.button2.text = "read";
this.button2.click += new system.eventhandler(this.button2_click);
//
// button3
//
this.button3.location = new system.drawing.point(88, 336);
this.button3.name = "button3";
this.button3.tabindex = 3;
this.button3.text = "update";
this.button3.click += new system.eventhandler(this.button3_click);
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(648, 397);
this.controls.add(this.button3);
this.controls.add(this.button2);
this.controls.add(this.button1);
this.controls.add(this.textbox1);
this.name = "form1";
this.text = "form1";
this.resumelayout(false);
}
#endregion
/// <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();
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 );
}
}
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())
{
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!");
}
}
}