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