Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
'The below code are convert the doc/docx/pdf to binary format and store into sqlserver.

VB
Private Sub btn_up_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_up.Click
        Try
            Dim fs As FileStream

            fs = New FileStream(sfile, FileMode.Open, FileAccess.Read)

            Dim docByte As Byte() = New Byte(fs.Length - 1) {}

            fs.Read(docByte, 0, System.Convert.ToInt32(fs.Length))

            fs.Close()
            'Insert statement for sql query
            Dim sqltxt As String
            sqltxt = "insert into test values('" & TextBox1.Text & "',@fdoc)"

            'store doc as Binary value using SQLParameter
            Dim docfile As New SqlParameter
            docfile.SqlDbType = SqlDbType.Binary
            docfile.ParameterName = "fdoc"
            docfile.Value = docByte
            sqlcmd = New SqlCommand(sqltxt, con)
            sqlcmd.Parameters.Add(docfile)
            sqlcmd.ExecuteNonQuery()
            MsgBox("Data Saved Successfully")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub



"Pls any of them tel how to retrieve the original file from database using vb.net"

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 24-Nov-12 21:16pm
v2

1 solution

The first thing to do is to change the way you are saving the data: there are two problems with your insert statement.
The first is that you should explicitly list the fields into which you want to save the data - anonymous entry relies on your DB not changing in the future.
VB
sqltxt = "insert into test values('" & TextBox1.Text & "',@fdoc)"

Becomes:
VB
sqltxt = "insert into test (fileName, fileData) values('" & TextBox1.Text & "',@fdoc)"


The second is that you really, really, should use the parametrized query you are setting up to specify the text as well - passing the TextBox content by string concatenation is opening your database up for an accidental or deliberate Sql Injection Attack. You are passing the file data as a parameter, so why not pass the TextBox content as well?

To retrieve it is exactly the same process you use for any other database access:
VB
Using con As New SqlConnection(strConnect)
	con.Open()
	Using com As New SqlCommand("SELECT fileName, fileData FROM test", con)
		Using reader As SqlDataReader = com.ExecuteReader()
			While reader.Read()
				Dim fileName As String = DirectCast(reader("fileName"), String)
				Dim fileData As Byte() = DirectCast(reader("fileData"), Byte())
				...
			End While
		End Using
	End Using
End Using
 
Share this answer
 
Comments
Priyanka Jain 29-May-13 5:02am    
hi..
this code work properly..please give me Vb.Net code for convert a byte array to a pdf file...
please help me...
Sumit Authankar 18-Aug-14 6:58am    
Hope this helps. :)
http://chiragrdarji.wordpress.com/2007/08/31/storing-and-retrieving-docpdfxls-files-in-sql-server/
OriginalGriff 24-Nov-17 7:11am    
After 5 years, and my code not containing any variable called "sfile" I'm not surprised...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900