【问题标题】:How to save a PDF file in a SQL Server column using vb.net code如何使用 vb.net 代码将 PDF 文件保存在 SQL Server 列中
【发布时间】:2012-04-21 19:07:49
【问题描述】:

如何使用 vb.net 代码通过内存流将 PDF 文件保存在 SQL Server 列中?

【问题讨论】:

    标签: sql-server vb.net


    【解决方案1】:

    谢谢!!! 我现在的解决方案是:

    public void SaveFile()
    {
        //Try
        OpenFileDialog fd = new OpenFileDialog();
        fd.Filter = "pdf file|*.pdf";
        if (fd.ShowDialog == System.Windows.Forms.DialogResult.OK) {
            //PdfDocument1.FilePath = fd.FileName
    
    
            byte[] filebyte = null;
            SqlConnection con = new SqlConnection("Data Source=LOCALHOS-A4AE79\\LOCALHOST1;Initial Catalog=library_alborz;Integrated Security=True");
    
    
            SqlCommand cmd = default(SqlCommand);
    
            filebyte = System.IO.File.ReadAllBytes(fd.FileName);
    
    
            cmd = new SqlCommand("Insert into pdftbl ( pdffld ) Values(@pdf)", con);
            //cmd.Parameters.Add("@filepath", SqlType.VarChar).Value = txtfilepath.Text
            cmd.Parameters.Add("@pdf", SqlDbType.Binary).Value = filebyte;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            Interaction.MsgBox("File saved into database", MsgBoxStyle.Information);
            //Catch ex As Exception
            //    MsgBox(Err.Description, MsgBoxStyle.Exclamation)
            //End Try
    
        }
    }
    
    
    ------------
    // load pdf file
    
    private void Button2_Click(System.Object sender, System.EventArgs e)
    {
        string strsql = null;
        SqlConnection con = new SqlConnection("Data Source=LOCALHOS-A4AE79\\LOCALHOST1;Initial Catalog=library_alborz;Integrated Security=True");
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
    
        try {
            strsql = "select pdffld from  pdftbl ";
            da = new SqlDataAdapter(strsql, con);
            da.Fill(ds, "tbl");
    
            //Get image data from gridview column.
            byte[] pdfData = Convert.ToByte(ds.Tables["tbl"].Rows[0][0]);
    
            //Initialize pdf  variable
    
            //Read pdf  data into a memory stream
            using (MemoryStream ms = new MemoryStream(pdfData, 0, pdfData.Length)) {
                ms.Write(pdfData, 0, pdfData.Length);
    
                //Set pdf variable value using memory stream.
                PdfDocument1.Load(ms);
                PdfPageView1.Document = PdfDocument1;
                PdfPageView1.Refresh();
    
            }
    
    
        } catch (Exception ex) {
            MessageBox.Show(ex.ToString());
        }
    }
    

    【讨论】:

      【解决方案2】:

      首先,您的数据库表中必须有 varbinary(MAX) 类型的列。它允许您在其中保存字节数组。

      接下来,您可以使用这行代码将 PDF 文件的内容作为字节数组获取:

      IO.File.ReadAllBytes("C:\my.pdf")
      

      【讨论】:

      • ntext , text, and **image** data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.(source)
      • 然后使用 varbinary(MAX)。但请指定 MAX,而不是某个数字,因为 MAX 最多允许使用 2^31-1 个字节。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      相关资源
      最近更新 更多