sql server .. pdf格式 [英] sql server .. pdf format

查看:66
本文介绍了sql server .. pdf格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
谁能告诉我如何在SQL Server中创建表,最后一栏包含扩展名为pdf格式的文件??? :doh:

Hi everyone!
Can anyone tell me how I can create a table in SQL-Server and the final column contains a file with extension of pdf format??? :doh:

Thanks!

推荐答案

要存储文件,可以在sql表中使用varbinary(max)类型.然后首先将文件读入内存流:
For storing the file, you can use varbinary(max) type in sql table. Then first you read file into a memory stream:
FileStream st = new FileStream(@"C:\pdfname.pdf", FileMode.Open);
            byte[] buffer = new byte[st.Length];
            st.Read(buffer, 0, (int)st.Length);
            st.Close();


然后将该流放入字节数组,然后将字节数组插入sql:


then put that stream into byte array, and then insert the byte array to sql:

SqlConnection conn = new SqlConnection("...");
            SqlCommand cmd = new SqlCommand("UPDATE SomeTable SET pdf=@pdf WHERE ID = 1", conn);
            cmd.Parameters.AddWithValue("@pdf", buffer);
            conn.Open();
            int i = cmd.ExecuteNonQuery();
            conn.Close();



如果要记住"文件名,则应在表中打开另一列用于存储文件名.要检索文件,只需执行相反的操作:



If you want to "remember" the file names, you should open up another column in the table for storing file names. For retrieving files, just do the reverse:

SqlConnection connection = new SqlConnection ("...");
        connection.Open ();
        SqlCommand command = new 
          SqlCommand ("select pdf from Table", connection);
        byte[] buffer = (byte[]) command.ExecuteScalar ();
        connection.Close();
        FileStream fs = new FileStream(@"C:\test.pdf", FileMode.Create);
        fs.Write(buffer, 0, buffer.Length);
        fs.Close();


如果文件太大而无法使用整数处理,则应实现缓冲机制;即将字节保存在可管理的部分中.
可以在此处


If your file is too big to handle with integers, you should implement buffer mechanism;i.e. save bytes in manageable portions.
Examples for this and further information can be found here


如果要存储整个PDF文件,则可以使用BLOB或等效字段.
If you want to store the entire PDF file, then you can use a BLOB or equivalent field.


这篇关于sql server .. pdf格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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