国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 石台县| 右玉县| 漳浦县| 平武县| 合山市| 南昌市| 岑溪市| 莲花县| 建水县| 沂源县| 望都县| 大关县| 苏尼特右旗| 盐山县| 固镇县| 沂水县| 腾冲县| 开封县| 游戏| 库尔勒市| 牡丹江市| 雅江县| 宜州市| 桐城市| 元阳县| 镶黄旗| 北碚区| 赤城县| 庆元县| 酒泉市| 阳谷县| 乌鲁木齐市| 白玉县| 开封县| 山东省| 衡水市| 奉新县| 五河县| 鱼台县| 甘谷县| 成安县|