从sqldatabase中提取pdf并使用c#显示它 [英] extract a pdf from sqldatabase and display it using c#

查看:79
本文介绍了从sqldatabase中提取pdf并使用c#显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将pdf文件上传到我的sql数据库中二进制数据文件。我想要检索此文件并在我的表单上使用Adobe Reader打开它。

请heeeellp

我正在使用visual studio 2015

thnx提前 

我试试这个但仍然没有结果:

推荐答案

你好faten22,

Hi faten22,

我为你的目的做了一个简单的演示,请参考下面的内容:

I made a simple demo for your purpose, please refer to it as below:

        //insert PDF to database
        public void button1_Click(object sender, EventArgs e)
        {
            string fileName = @"C:\Users\[user]\MyTest\test.pdf";
            FileInfo fi = new FileInfo(fileName);
            FileStream fs = fi.OpenRead();
            byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
            SqlConnection cn = new SqlConnection(strConn);
            SqlCommand cm = new SqlCommand();
            cm.Connection = cn;
            cm.CommandType = CommandType.Text;
            if (cn.State == 0) cn.Open();
            cm.CommandText = "insert into Table1 (MyFile) values(@file)";
            SqlParameter spFile = new SqlParameter("@file", SqlDbType.Image);
            spFile.Value = bytes;
            cm.Parameters.Add(spFile);
            cm.ExecuteNonQuery();
        }

        DataSet ds = new DataSet();
        string strConn = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\DAL\MyTest.mdf;Integrated Security=True";
        string filePath;

        //reload PDF from database to form
        private void button2_Click(object sender, EventArgs e)
        {
            filePath = @"C:\Users\[user]\Desktop\MyTest\here.pdf";
            using (SqlConnection cnn = new SqlConnection(strConn))
            {
                cnn.Open();
                using (SqlDataAdapter da = new SqlDataAdapter("select * from Table1", cnn))
                {
                    da.Fill(ds);
                    byte[] bytes = (byte[])ds.Tables[0].Rows[0][1];
                    MemoryStream memStream = new MemoryStream(bytes);
                    File.WriteAllBytes(filePath, bytes);
                }
            }
            
            axAcroPDF.LoadFile(filePath);
        }

问候,

Frankie


这篇关于从sqldatabase中提取pdf并使用c#显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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