Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating a file to a sql server database table

Status
Not open for further replies.

pink2070

Programmer
Joined
Jan 29, 2004
Messages
23
Location
US
Hello,

I'm trying to figure out how to save a file (.pfs) to a sql server database table, I've had plenty of experience saving text information but never a file.

Could someone please give me an example of how to do this, or a reference to an example of how to do this.

Thanks

Pink
 
What are you using as a client platform?

osql?
dos?
dot net?
vb?
java?
EnterpriseManager?

Rob
 
SORRY - I thought I was looking thru the SQLServer newsgroup.

I will get a sample and post back :)
 
Try this (I haven't run this specificly, but it should do it)
Needs a storedproc called "StoreFiles" that has 3 parameters,
1 being for an image datatype (binary data)
1 for size a
and 1 for filename.

The size and filename really aren't necessary, but are usefull when searching back files

any problmes - jsut post back here an i will be watching the thread.

HTH

Rob



Code:
    Sub savestuff()
        Dim sr As New System.IO.StreamReader("c:\test.txt")

        Dim length As Integer = sr.BaseStream.Length()

        Dim Content As Byte()
        ReDim Content(length)
        sr.BaseStream.Read(Content, 0, length)
        UseStoreFiles(Content, "test", "c:\test.txt", length)


    End Sub

    Private Function UseStoreFiles(ByVal Data As Byte(),  ByVal FileName As String, ByVal Size As Integer) As Boolean
        Dim con As New SqlClient.SqlConnection("server=(local);database=northwind;trusted_connection=yes")
        Dim cmd As New SqlClient.SqlCommand("StoreFiles", con)
        Dim da As New SqlClient.SqlDataAdapter(cmd)
        Dim dt As New DataTable
        With cmd
            .CommandType = CommandType.StoredProcedure
            With .Parameters
                .Add("@Data", SqlDbType.Image).Value = Data
                .Add("@Size", SqlDbType.VarChar, 300).Value = Size
                .Add("@FileName", SqlDbType.VarChar, 1000).Value = FileName
            End With
            con.Open()
            Try
                cmd.ExecuteNonQuery() ' For a stored proc with no records...
            Catch ex As Exception
                MsgBox(ex.Message)
                Return False
                Exit Function
            Finally
                con.Close()
                cmd.Dispose()
            End Try
        End With
        Return True

    End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top