欢迎光临
我们一直在努力

c# ,在Oracle 中,对 blob 类型对象的操作-.NET教程,C#语言

建站超值云服务器,限时71元/月

尝试的情况是,提供一个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!");

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » c# ,在Oracle 中,对 blob 类型对象的操作-.NET教程,C#语言
分享到: 更多 (0)

相关推荐

  • 暂无文章