欢迎光临
我们一直在努力

把RichTextBox中的文本保存到Sql Server中(C#)-.NET教程,C#语言

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

保存:

private void btnsave_click(object sender, system.eventargs e)

{

filestream stream = null;

sqlconnection conn = null;

sqlcommand cmd = null;

try

{

richtextbox1.savefile( "temp.rtf" );

stream = new filestream("temp.rtf", filemode.open, fileaccess.read);

int size = convert.toint32(stream.length);

byte[] rtf = new byte[size];

stream.read(rtf, 0, size);

conn = new sqlconnection("database=northwind;integrated security=true;");

conn.open();

cmd = new sqlcommand("update employees set photo=@photo where employeeid=1", conn);

sqlparameter paramrtf =

new sqlparameter("@photo",

sqldbtype.image,

rtf.length,

parameterdirection.input,

false,

0,0,null,

datarowversion.current,

rtf);

cmd.parameters.add(paramrtf);

int rowsupdated = convert.toint32(cmd.executenonquery());

messagebox.show(string.format("{0} rows updated", rowsupdated));

}

catch(exception ex)

{

messagebox.show(ex.message);

}

finally

{

if ( stream != null ) stream.close();

if (cmd != null ) cmd.parameters.clear();

if (conn != null) conn.close();

}

}

读取:

private void btnload_click(object sender, system.eventargs e)

{

richtextbox1.clear();

sqlconnection cn = null;

sqlcommand cmd = null;

sqldatareader reader = null;

try

{

cn = new sqlconnection("database=northwind;integrated security=true;");

cn.open();

cmd = new sqlcommand("select photo from employees where employeeid=1", cn);

reader = cmd.executereader();

reader.read();

if (reader.hasrows)

{

if (!reader.isdbnull(0))

{

byte[] rtf = new byte[convert.toint32((reader.getbytes(0, 0, null, 0, int32.maxvalue)))];

long bytesreceived = reader.getbytes(0, 0, rtf, 0, rtf.length);

asciiencoding encoding = new asciiencoding();

richtextbox1.rtf = encoding.getstring(rtf, 0, convert.toint32(bytesreceived));

}

}

}

catch(exception ex)

{

messagebox.show(ex.message);

}

finally

{

if (reader != null ) reader.close();

if (cn != null ) cn.close();

}

}

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

相关推荐

  • 暂无文章