上一篇:c# ArrayList 的 Sort()方法的使用 >>
把RichTextBox中的文本保存到Sql Server中(C#)
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();
}
}
下一篇:MapObjects2.2 在C#中的应用(基本地图功能 >>
相关文章:
- · c#v2.0 扩展特性 翻译(1)
- · C#陷阱:int i = 10; i += i++; i = ?
- · C# 编码规范
- · 如何动态加载控件以及插件编程思想(C#)
- · Implementing the Singleton Pattern in C#
- · C#事件机制学习
- · 如何才能学好C#(转载)
- · C#实现的基本算法
- · C#事件机制归纳(下)
- · C#事件机制归纳(上)
- · (C#)利用反射动态调用类成员
- · IDesign C#编码规范(之五)
- · C#中的正则表达式(1)
- · The C# Programming Language Notes
- · The C# Programming Language Notes
- · 给图片添加版权信息(C#)
- · c#中int 转string 16进制和16转double的方法
- · C#,结构成员是引用,会发生什么
- · 用C#轻松地在DOTNET中实现缩略图
- · AOP C#在行动(续2)
- · IDesign C#编码规范(之五)
- · IDesign C#编程规范(之四)
- · IDesign C#编码规范(之三)
- · IDesign C#编程规范(二)
- · IDesign C#编程规范(一)
- · AOP C#在行动(续1)
- · C#中Plugin的实践
- · C#截取屏幕↑
- · The C# Programming Language Notes
- · AOP C#在行动
- · Visual C# 2005 Express Edition Beta的第一天使用
- · 基类和子类的调用顺序(C#,java)
- · 哲学家就餐问题的C#实现
- · 走迷宫C#版(二)
- · 走迷宫C#版(一)
- · C#中奇妙的操作符重载
- · C#实现查看文本框(如*号密码框)
- · 关于c#中的消息处理函数和vc中的消息处理函数区别
