RichTextBox與二進制Word

    '將RichTextBox的內容直接寫入數據庫:
    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim mstream As System.IO.MemoryStream = New System.IO.MemoryStream()
        Me.richTextBox1.SaveFile(mstream, RichTextBoxStreamType.RichText)
        '將流轉換成數組
        Dim bWrite() As Byte = mstream.ToArray()
        '將數組寫入數據庫
        Dim pram() As System.Data.SqlClient.SqlParameter = {sqlHelper.MakeInParam("@XX", System.Data.SqlDbType.Image)}


        pram(0).Value = bWrite
        sqlHelper.RunSql("insert into XXX (XX) values (@XX)", pram)
    End Sub


    '將數據庫中的RTF讀出並填充到RichTextBox
    Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
        '從數據庫中讀出數據
        Dim dt As DataTable = sqlHelper.GetDataTable("select XX from XXX where .....")
        Dim bWrite() As Byte = CType(dt.Rows(0)(0), Byte())
        '將數組轉換成stream
        Dim mstream As System.IO.MemoryStream = New System.IO.MemoryStream(bWrite, False)
        '將stream填充到RichTextBox
        Me.richTextBox1.LoadFile(mstream, RichTextBoxStreamType.RichText)
    End Sub

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章