将RichTextBox的内容直接写入数据库:

 1 将RichTextBox的内容直接写入数据库:
 2 private void button1_Click(object sender, EventArgs e)
 3 {
 4    System.IO.MemoryStream mstream = new System.IO.MemoryStream();
 5    this.richTextBox1.SaveFile(mstream, RichTextBoxStreamType.RichText);
 6    //将流转换成数组
 7    byte[] bWrite = mstream.ToArray();
 8    //将数组写入数据库
 9    System.Data.SqlClient.SqlParameter[] pram ={
10           sqlHelper.MakeInParam("@XX",System.Data.SqlDbType.Image)
11    };
12    pram[0].Value = bWrite;
13    sqlHelper.RunSql("insert into XXX (XX) values (@XX)", pram);
14 }
15 将数据库中的RTF读出并填充到RichTextBox
16 private void button2_Click(object sender, EventArgs e)
17 {
18    //从数据库中读出数据
19    DataTable dt=sqlHelper.GetDataTable("select XX from XXX where .....");
20    byte[] bWrite = (byte[])dt.Rows[0][0];
21    //将数组转换成stream
22    System.IO.MemoryStream mstream = new System.IO.MemoryStream(bWrite, false);
23    //将stream填充到RichTextBox
24    this.richTextBox1.LoadFile(mstream, RichTextBoxStreamType.RichText);
25 }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将 C# 中 RichTextBox 的文本内容导入到数据库中,您可以使用 ADO.NET 并将 RichTextBox 的 Text 属性值插入到数据库表中。以下是一个示例代码: ```csharp using System; using System.Data.SqlClient; using System.Windows.Forms; class Program { static void Main() { string connectionString = "your_connection_string"; string tableName = "YourTable"; // 创建一个 RichTextBox 控件实例 RichTextBox richTextBox = new RichTextBox(); // 将数据库中的文本内容读取到 richTextBox 中 using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = connection.CreateCommand(); command.CommandText = $"SELECT TextContent FROM {tableName} WHERE Id = @id"; command.Parameters.AddWithValue("@id", 1); // 假设您的数据存储在 Id 为 1 的记录中 SqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { if (!reader.IsDBNull(0)) { richTextBox.Text = reader.GetString(0); } } } // 将 RichTextBox 的文本内容导入到数据库中 using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = connection.CreateCommand(); command.CommandText = $"UPDATE {tableName} SET TextContent = @content WHERE Id = @id"; command.Parameters.AddWithValue("@id", 1); // 假设您要更新 Id 为 1 的记录 command.Parameters.AddWithValue("@content", richTextBox.Text); int rowsAffected = command.ExecuteNonQuery(); if (rowsAffected > 0) { Console.WriteLine("RichTextBox 的文本内容已成功导入到数据库中。"); } else { Console.WriteLine("导入失败。"); } } } } ``` 请确保将 `your_connection_string` 替换为实际的数据库连接字符串,将 `YourTable` 替换为实际的表名和列名,以及根据您的数据结构和需求进行修改。 在上述示例中,我们首先从数据库中读取文本内容并将其加载到 RichTextBox 控件中。然后,我们将 RichTextBox 的 Text 属性值导入到数据库中的指定记录中。 这只是一个简单的示例,您可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值