在数据库C#中插入PDF [英] Insert PDF in database C#

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

问题描述

在数据库中插入PDF



这是好方法吗?



我有什么试过:



 private void Button2_Click(object sender,EventArgs e)//将pdf保存在数据库中
{
byte [] filedata = null;
MemoryStream ms = new MemoryStream();
filedata = ms.GetBuffer();
axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());



使用(SqlConnection openCon = new SqlConnection(cs))
{

string saveStaff =declare @maxNo integer = 0从[dbo]中选择@maxNo = isnull(max(number),0)。[documents];设置@ maxNo = @ maxNo + 1; INSERT到dbo.documents(number,file_location,pdf_file)VALUES(@ maxNo,@ file_location ,@ pdf_file);

使用(SqlCommand querySaveStaff = new SqlCommand(saveStaff))
{
querySaveStaff.Connection = openCon;

querySaveStaff.Parameters.Add(@ file_location,SqlDbType.VarChar,255).Value = file_locationTextBox.Text;

querySaveStaff.Parameters.AddWithValue(@ pdf_file,SqlDbType.VarBinary).Value = filedata;

openCon.Open();
querySaveStaff.ExecuteNonQuery();
openCon.Close();


}

}

}

解决方案

虽然PDF字节数组的实际存储大致正确,但您的ID生成存在根本问题。不要自己手动添加ID,而是考虑将此字段更改为自动增量并让它自行处理。就目前而言,您冒着让两个字段尝试在多用户系统中添加相同ID的风险,因为无法保证您在下一个字段开始之前完成插入序列。


< blockquote>这段代码应该做什么?

  byte  [] filedata = ;  //   filedata不包含任何内容 
MemoryStream ms = MemoryStream(); // 新的空内存流
filedata = ms.GetBuffer(); // filedata仍然不包含任何内容
axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray( )); // 不知道应该做什么

// ...

querySaveStaff.Parameters.AddWithValue( @ pdf_file,SqlDbType.VarBinary).Value = filedata;
// 但你永远不会向filedata添加任何内容,因此它会向数据库添加0x0。


Insert PDF in database

Is this good way?

What I have tried:

private void Button2_Click(object sender, EventArgs e)   //Save pdf in database
        {
            byte[] filedata = null;
            MemoryStream ms = new MemoryStream();
            filedata = ms.GetBuffer();
            axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());



                    using (SqlConnection openCon = new SqlConnection(cs))
                    {

                        string saveStaff = "declare @maxNo integer = 0 select @maxNo = isnull(max(number), 0) from [dbo].[documents]; Set @maxNo=@maxNo+1; INSERT into dbo.documents (number,file_location, pdf_file) VALUES (@maxNo,@file_location,@pdf_file)";

                        using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
                        {
                            querySaveStaff.Connection = openCon;

                            querySaveStaff.Parameters.Add("@file_location", SqlDbType.VarChar, 255).Value = file_locationTextBox.Text;

                            querySaveStaff.Parameters.AddWithValue("@pdf_file", SqlDbType.VarBinary).Value = filedata;

                            openCon.Open();
                            querySaveStaff.ExecuteNonQuery();
                            openCon.Close();                            


                        }

                    }

}

解决方案

While the actual storing of the PDF byte array is roughly correct, you have a fundamental problem in your ID generation. Rather than manually adding an ID yourself, consider changing this field to an autoincrement and let that take care of itself. As it stands, you run the risk of having two fields attempt to add the same ID in a multi-user system because there's no guarantee that you will complete the insert sequence before the next one commences.


What is this code supposed to do?

            byte[] filedata = null;   // filedata contains nothing
            MemoryStream ms = new MemoryStream(); // a new empty memory stream
            filedata = ms.GetBuffer();  // filedata still contains nothing
            axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray()); // no idea what this is supposed to do

// ...

querySaveStaff.Parameters.AddWithValue("@pdf_file", SqlDbType.VarBinary).Value = filedata;
// but you never add any content to filedata, so it adds 0x0 to the database.


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

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