Solved

saving a blob file to mysql using asp.net

Posted on 2003-12-07
4
1,094 Views
Last Modified: 2008-02-01
hi! i am having a problem saving my word document/pdf files to mysql database using asp.net. it is my first time to use the longblob datatype that's why it would be better if you could provide me a detailed solution. hope you can help me solve the problem. thanks a lot!!!!
0
Comment
Question by:john_20
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 250 total points
ID: 9892177
' WriteBLOB demonstrates how to write image data to a SQL
' Server table. This example assumes a database named "Test"
' on the local machine, and a table named "blobTest" with
' an identity column "id," a "fileName" column, and a column
' named "blobData" to store the image data.
Public Sub WriteBLOB(ByVal fileName As String)

    ' Create connection object to SQL Server.
    Dim cn As New System.Data.SqlClient.SqlConnection( _
        "Data Source=(local);Initial Catalog=Test;Integrated Security=SSPI;")

    ' Create the command object that implements the INSERT command.
    Dim cmd As New System.Data.SqlClient.SqlCommand( _
        "INSERT INTO blobTest (blobData, fileName) VALUES (@blobData, @fileName) ", cn)

    ' Open the specified image file from disk.
    Dim fs As New System.IO.FileStream( _
          fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)
          
    Dim b(CType(fs.Length, Integer)) As Byte
    fs.Read(b, 0, b.Length)
    fs.Close()

    ' Create the parameters for the INSERT statement.
    cmd.Parameters.Add("@fileName", fileName)
    cmd.Parameters("@fileName").Direction = System.Data.ParameterDirection.Input

    cmd.Parameters.Add("@blobData", System.Data.SqlDbType.Image)
    cmd.Parameters("@blobData").Direction = System.Data.ParameterDirection.Input
    cmd.Parameters("@blobData").Value = b

    ' Open the connection and insert the data.
    Try
        cn.Open()
        cmd.ExecuteNonQuery()
    Catch e As SqlClient.SqlException
        Throw e
    Finally
        cn.Close()
    End Try

End Sub
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 9892181
Here is another example to save Word doc to Access database

Imports System.Data.OleDb
Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region
#Region " Write to DB "
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        'take this doc and save it to the TestDb  too the table BLOBTable
        WriteDocumentToDB("c:\temp\word.doc")
    End Sub
    Public Sub WriteDocumentToDB(ByVal sFile As String)
        Dim da As New OleDbDataAdapter(fsSQL, fsConn)
        Dim cbDocument As OleDbCommandBuilder = New OleDbCommandBuilder(da)
        Dim ds As New DataSet()


        da.MissingSchemaAction = MissingSchemaAction.AddWithKey

        Dim fs As New FileStream(sFile, FileMode.OpenOrCreate, FileAccess.Read)
        Dim MyData(fs.Length) As Byte

        fs.Read(MyData, 0, fs.Length)
        fs.Close()

        Try
            da.Fill(ds, "BLOBTable")
        Catch e As Exception
            MsgBox(Err.Description)
        End Try
        Dim myRow As DataRow

        myRow = ds.Tables("BLOBTable").NewRow()
        myRow("Description") = "This is description of inserted document"
        myRow("BLOBField") = MyData
        ds.Tables("BLOBTable").Rows.Add(myRow)
        da.Update(ds, "BLOBTable")
        fs = Nothing
        cbDocument = Nothing
        ds = Nothing
        da = Nothing

        MsgBox("File saved to database")

    End Sub
#End Region
#Region " Restore from DB "
    Private Sub btnRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestore.Click
        'check it will restore from the Row 0 of the BLOBTable (change if you want to
        'restore from different rows)
        ReadDocumentFromDB("c:\a\Word.doc")
    End Sub
    Public Sub ReadDocumentFromDB(ByVal sOutFile As String)
        Dim da As New OleDbDataAdapter(fsSQL, fsConn)
        Dim cbDocument As OleDbCommandBuilder = New OleDbCommandBuilder(da)
        Dim ds As New DataSet()

        da.Fill(ds, "BLOBTable")

        Dim myRow As DataRow
        'retrieve from Row(0)
        myRow = ds.Tables("BLOBTable").Rows(0)

        Dim MyData() As Byte

        MyData = myRow("BLOBField")

        Dim K As Long

        K = UBound(MyData)

        Dim fs As New FileStream(sOutFile, FileMode.OpenOrCreate, FileAccess.Write)

        fs.Write(MyData, 0, K)
        fs.Close()
        fs = Nothing
        cbDocument = Nothing
        ds = Nothing
        da = Nothing

        MsgBox("File retrieved")

    End Sub
#End Region
End Class

0
 

Author Comment

by:john_20
ID: 9961632
thanks!
0
 
LVL 4

Expert Comment

by:Moizsaif123
ID: 13545363
I am trying to do the same as above...I posted my question below...


http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21351438.html
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now