VB.net Richtextbox保存到数据库表与读取实例

Imports System.IO
Imports System.Data.SqlClient

窗口资源:Text25富文本框、CmdSaveDOC、CmdReadDoc按钮,Text1 普通文本框(查询条件)

数据表:Shantytowns_HouseLevy_PlacementAgreement_H、字段名:RichText 数据类型:varbinary(MAX),SysID CHAR(50)关键字段

Private Sub CmdSaveDOC_Click(sender As Object, e As EventArgs)’保存到数据表
        Try
            Dim StrSysID As String = Trim(Me.Text1.Text)
            If StrSysID = "" Then Exit Sub
            Dim mstream As System.IO.MemoryStream = New System.IO.MemoryStream()
            Me.Text25.SaveFile(mstream, RichTextBoxStreamType.RichText)
            Dim bWrite() As Byte = mstream.ToArray()
            Dim cmd As New SqlCommand("Update Shantytowns_HouseLevy_PlacementAgreement_H  Set RichText=@RtfData  Where SysID='" & StrSysID & "' ", SQLConn)
            cmd.Parameters.Add("@RtfData", SqlDbType.VarBinary).Value = bWrite
            cmd.ExecuteNonQuery()
            cmd.Dispose()
            cmd = Nothing
        Catch ex As Exception
            MsgBox("保存文本出错!" + ex.ToString, vbOKOnly + 64, "系统提示")
        End Try
    End Sub

’读取到窗口richtextbox文本框中:Text25
    Private Sub CmdReadDoc_Click(sender As Object, e As EventArgs)
        Try
            Dim StrSysID As String = Trim(Me.Text1.Text)
            If StrSysID = "" Then Exit Sub
            Dim query As String = "Select RichText From Shantytowns_HouseLevy_PlacementAgreement_H Where SysID='" & StrSysID & "' "
            Dim Adatmp As New SqlDataAdapter(query, SQLConn)
            Dim DStmp As New DataSet
            Adatmp.Fill(DStmp, "TB")
            If IsDBNull(DStmp.Tables("TB").Rows(0).Item("RichText")) = False Then
                Dim data As Byte() = DStmp.Tables("Tb").Rows(0)("RichText") '定义二进制字段类型
                Dim ms As New MemoryStream(data)’创建内存数据流
                Me.Text25.LoadFile(ms, RichTextBoxStreamType.RichText)
            End If
            Adatmp.Dispose()
            Adatmp = Nothing
            DStmp.Dispose()
            DStmp = Nothing
        Catch ex As Exception
            MsgBox("读取文本出错!" + ex.ToString, vbOKOnly + 64, "系统提示")
        End Try
    End Sub

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wkchh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值