C#中怎样把RichTextBox控件中的文档内容及格式信息以XML格式存入SQL SERVER 2005数据库的字段中 在c#中怎样把RichTextBox控件中的文档内容及格式信息以XML格式存入SQL SERVER 2005数据库的字段中(该字段类型为TEXT),以及怎样读取SQL SERVER 2005数据库的字段内容?谢谢!可否给出具体的实现代码? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 RichTextBox.Rtf属性是带RTF格式的文本 SqlConnection cn = new SqlConnection("连接你SQL2005的字符串");SqlCommand insertCmd = new SqlCommand("INSERT INTO 表名 (text字段名) VALUE (@TextRtf)", cn);insertCmd.Parameters.Add("@TextRtf", System.Data.SqlDbType.Text).Value = RichTextBox.Rtf;insertCmd.ExecuteNonQuery(); SqlConnection cn = new SqlConnection("连接你SQL2005的字符串");SqlCommand insertCmd = new SqlCommand("INSERT INTO 表名 (text字段名) VALUE (@TextRtf)", cn);insertCmd.Parameters.Add("@TextRtf", System.Data.SqlDbType.Text).Value = RichTextBox.Rtf;insertCmd.ExecuteNonQuery(); SqlConnection cn = new SqlConnection("连接你SQL2005的字符串"); cn.Open(); SqlCommand insertCmd = new SqlCommand("INSERT INTO 表名 (text字段名) VALUES (@TextRtf)", cn); try { insertCmd.Parameters.Add("@TextRtf", System.Data.SqlDbType.Text).Value = this.richTextBox1.Rtf; insertCmd.ExecuteNonQuery(); MessageBox.Show("保存rtf数据成功!", "温馨提示"); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (insertCmd != null) insertCmd.Parameters.Clear(); if (cn != null) cn.Close(); }以上是完整的代码,谢谢shsoft888 读取源代码:richTextBox1.Clear(); SqlConnection cn = null; SqlCommand cmd = null; SqlDataReader reader = null; try { cn = new SqlConnection("Database=数据库名;Integrated Security=true;"); cn.Open(); cmd = new SqlCommand("SELECT top 1 test FROM test", 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(); } c# keybd_event按住某键出问题 怎么样将int型数字转换成7位字符串,不足的时候,前面补0 删除IE的临时文件 100分结贴迅速 VS2008中结构体是在stack上还是在heap上啊? VC#2005写存储过程的问题 100分求水晶报表10.0 ORACLE 中 怎么插入空值(时间类型)谢谢 winform textbox扩展 JAVASCRIPT类型转换问题 如何使用C#删除再word 中悬浮的图片 C# 接口回调问题!急!!!!!!!!!!!!!!!!! C#中使用DirectSound来获取麦克音量
SqlCommand insertCmd = new SqlCommand("INSERT INTO 表名 (text字段名) VALUE (@TextRtf)", cn);
insertCmd.Parameters.Add("@TextRtf", System.Data.SqlDbType.Text).Value = RichTextBox.Rtf;
insertCmd.ExecuteNonQuery();
SqlCommand insertCmd = new SqlCommand("INSERT INTO 表名 (text字段名) VALUE (@TextRtf)", cn);
insertCmd.Parameters.Add("@TextRtf", System.Data.SqlDbType.Text).Value = RichTextBox.Rtf;
insertCmd.ExecuteNonQuery();
cn.Open();
SqlCommand insertCmd = new SqlCommand("INSERT INTO 表名 (text字段名) VALUES (@TextRtf)", cn);
try
{
insertCmd.Parameters.Add("@TextRtf", System.Data.SqlDbType.Text).Value = this.richTextBox1.Rtf;
insertCmd.ExecuteNonQuery();
MessageBox.Show("保存rtf数据成功!", "温馨提示");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (insertCmd != null) insertCmd.Parameters.Clear();
if (cn != null) cn.Close();
}
以上是完整的代码,谢谢shsoft888
richTextBox1.Clear(); SqlConnection cn = null;
SqlCommand cmd = null;
SqlDataReader reader = null;
try
{
cn = new SqlConnection("Database=数据库名;Integrated Security=true;");
cn.Open();
cmd = new SqlCommand("SELECT top 1 test FROM test", 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();
}