国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > 綜合 > 正文

把RichTextBox中的文本保存到Sql Server中(C#)

2024-07-21 02:19:36
字體:
供稿:網(wǎng)友


保存:

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 [email protected] 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();
}

}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 二手房| 衡阳市| 罗定市| 浮梁县| 富宁县| 若羌县| 宁强县| 伊宁市| 邓州市| 宜兰县| 汨罗市| 金塔县| 靖宇县| 资兴市| 施甸县| 崇左市| 横山县| 贵阳市| 安义县| 楚雄市| 丹阳市| 阿拉善右旗| 神池县| 寿阳县| 图木舒克市| 望城县| 南和县| 华容县| 梁平县| 天全县| 垫江县| 定西市| 五原县| 四平市| 凤翔县| 双峰县| 错那县| 河南省| 余姚市| 沙河市| 启东市|