请问怎么把RichTextBox 中的数据和格式一起存在Sql Server 中,和如何读出显示到RichTextBox
解决方案 »
- 有没有用过水晶报表的呀?使用数据集提示让登录。
- 64位 windows servser 2008上面 excel控件报错.
- [WINFORM]TreeView,TreeNode如何始终显示前面的+号?
- 请教一个无级树的问题,treeview
- C#如何让树排序
- 求实例化委托时,传入的方法具体是什么类型的参数?
- ◎◎◎请教各位高手WinForm中关于ListView的SelectedIndexChanged事件的处理◎◎◎
- 为什么我的TreeView进入页面时每次都要刷新一下才可以显示出来
- 关于C#的类的属性
- C#中如何遍历文件夹和文件夹中的文件呢!?
- xml错误的问题,急,,
- 困惑了很久的一个问题!
{ 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();
} }