?
Solved

saving a blob file to mysql using asp.net

Posted on 2003-12-07
4
Medium Priority
?
1,107 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 1000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

649 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