Click here to Skip to main content
15,867,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hey guys,

Can you please tell me, how can I save pdf and xls files in sql server database using vb.net

2010. Following is the code I have used to store images into database.

VB
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim cmd As New SqlCommand("INSERT INTO Info VALUES(@name,@photo)", con)
        cmd.Parameters.AddWithValue("@name", TextBox1.Text)
        Dim ms As New MemoryStream()
        PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat)
        Dim data As Byte() = ms.GetBuffer()
        Dim p As New SqlParameter("@photo", SqlDbType.Image)
        p.Value = data
        cmd.Parameters.Add(p)
        cmd.ExecuteNonQuery()
        MessageBox.Show("Name & Image has been saved", "Save", MessageBoxButtons.OK)

    End Sub


Tell me how to save pdf files into database. What should be the datatype of columns in

database?
Posted
Comments
[no name] 13-Apr-14 10:17am    
http://www.google.com/search?q=save+pdf+and+xls+files+in+sql+server+database+using+vb.net+

1 solution

Convert the file to Byte() then use varbinary datatype in the SQL Database to store the file.

Try this:

VB
Dim pdfBytes As Byte() = Nothing

pdfbBytes = System.IO.File.ReadAllBytes("C:/test.pdf")

- - - - - - - - - - - - - -
Dim connection As SqlConnection = Nothing
Dim command As SqlCommand = Nothing
Dim queryString As String = String.Empty


queryString = "INSERT INTO dbo.tbl_test (pdf_file) VALUES (@pdfFile)"
connection = New SqlConnection(_connectionString)
connection.Open()

command = New SqlCommand(queryString, connection)
command.Parameters.AddWithValue("pdfFile", pdfBytes )
command.ExecuteNonQuery()
 
Share this answer
 
v2
Comments
Amiet_Mhaske 17-Apr-14 5:05am    
Yes sir.. but can you please post some demo code for me please?

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