zoukankan
html css js c++ java
把RichTextBox中的文本保存到Sql Server中
保存:
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();
}
}
查看全文
相关阅读:
剖析VC++函数调用约定转
C++的坑真的多吗?转
An Introduction to LockFree Programming转
__cdecl __stdcall区别转
学习PHP感谢帅哥分享O(∩_∩)O~
28个Unix/Linux的命令行神器转
C++ 对象的内存布局(上)转
一个fork的面试题转
20本最好的Linux免费书籍转
谁说外国人都很文明
原文地址:https://www.cnblogs.com/dahuzizyd/p/27787.html
最新文章
Event事件鼠标键盘举例
javascriptTextUtil对象封装(文本框验证)
javascript制作的菜单
javaScriptHttp对象封装
javascriptListUtil对象封装 (下拉框)
javascript Formutil对象封装
javascript之XML DOM对象
单件模式(Singleton Pattern )
非静态类的静态成员与非静态成员的初始化顺序
HDU1282+水题+模拟
热门文章
HDU1285+拓扑排序
HDU1072+BFS
CF295A
HDU3507+DP斜率优化
HDU2412+树形DP
HDU1281+二分图
HDU1698+线段树
Java+大数+高精度
HDU1272+BFS
C++ 对象的内存布局(下)转
Copyright © 2011-2022 走看看