数据库中的pdf文件 [英] pdf file in database

查看:202
本文介绍了数据库中的pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在sql数据库中存储"pdf"文件的数据类型是什么?

what is datatype for store "pdf" file in sql database

推荐答案



如果要将文件存储在sql服务器数据库中,则可以为此使用Varbinary类型.

您可以根据自己的目的尝试以下代码和表结构,希望它符合您的要求.



表结构:
Hi,

if you want to store your file in sql server database, you can use Varbinary type for this.

you can try following piece of code and table structure for your purpose, I hope it matches your requirements.



Table structure:
CREATE TABLE [dbo].[PdfFiles](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL,
    [Type] [varchar](50) NULL,
    [Data] [varbinary](max) NULL
) ON [PRIMARY]




按钮上的代码单击:




Code on button click:

protected void btnUpload_Click(object sender, EventArgs e)
        {
            string filePath = FileUpload1.PostedFile.FileName;          // getting the file path of uploaded file
            string filename = Path.GetFileName(filePath);               // getting the file name of uploaded file
            string ext = Path.GetExtension(filename);                      // getting the file extension of uploaded file
            string type = String.Empty;

            if (!FileUpload1.HasFile)
            {
                Label2.ForeColor = System.Drawing.Color.Red;
                Label2.Text = "Please Select a File";
                
            }
            else if (FileUpload1.HasFile)
            {
                switch (ext)
                {
                    case ".pdf":
                        type = "application/pdf";
                        break;
                    case ".txt":
                        type = "application/txt";
                        break;
                    case ".docx":
                        type = "application/docx";
                        break;
                    

                }

                if (type != string.Empty)
                {
                    SqlConnection conn = new SqlConnection(con);

                    Stream fs = FileUpload1.PostedFile.InputStream;
                    BinaryReader br = new BinaryReader(fs);                                 //reads the   binary files
                    Byte[] bytes = br.ReadBytes((Int32)fs.Length);                           //counting the file length into bytes

                    string query = "insert into PdfFiles (Name,type,data)" + " values (@Name, @type, @Data)";   //insert query
                    SqlCommand com = new SqlCommand(query, conn);
                    com.Parameters.AddWithValue("@Name", filename);
                    com.Parameters.AddWithValue("@type", type);
                    com.Parameters.AddWithValue("@Data", bytes);
                    conn.Open();
                    com.ExecuteNonQuery();
                    conn.Close();
                    Label2.ForeColor = System.Drawing.Color.Green;
                    Label2.Text = "File Uploaded Successfully";

                }
                else
                {
                    Label2.ForeColor = System.Drawing.Color.Red;
                    Label2.Text = "Please select a pdf/txt/word file";

                }
            }



        }






快乐的编码:)






Happy coding :)


您可以使用varbinary数据类型.您必须将PDF文件转换为字节数组,请参阅-> http://stackoverflow.com/questions/14770228/how-to-convert-a-file-of-any-type-in​​to-byte-array [
You can use varbinary data type. You have to convert PDF file into byte array, please refer -> http://stackoverflow.com/questions/14770228/how-to-convert-a-file-of-any-type-into-byte-array[^]


Pass this byte array to varbinarycolumn.
you can use simple sql statement for insert/ updated pdf file.


检查以下链接

Check the following link

http://social.msdn.microsoft.com/Forums/sqlserver/en-US/4e23b5eb-2dce-41eb-9db0-7999f205f18f/store-pdf-file-in-sql-or-oracle-database-?forum=sqldatabaseengine[^]


这篇关于数据库中的pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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