如何使用vb.net 2010将PDF,Xls文件存储和检索到数据库中 [英] How to store and retrieve PDF, Xls files into database using vb.net 2010

查看:136
本文介绍了如何使用vb.net 2010将PDF,Xls文件存储和检索到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,



能告诉我,如何使用vb.net将pdf和xls文件保存在sql server数据库中



2010.以下是我用于将图像存储到数据库中的代码。



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.

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





告诉我如何将pdf文件保存到数据库中。



数据库中列的数据类型应该是什么?



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

database?

推荐答案

将文件转换为Byte(),然后在SQL数据库中使用varbinary数据类型来存储文件。



试试这个:< br $> b $ b

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

Try this:

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()


这篇关于如何使用vb.net 2010将PDF,Xls文件存储和检索到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆