在数据库之间存储和检索pdf文件的错误 [英] the error storing and retriving pdf file from and to database

查看:80
本文介绍了在数据库之间存储和检索pdf文件的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击上传按钮后,我被告知将default1引用为default2,
它返回无效的操作异常,而用户代码未对其进行处理
填充:SelectCommand.Connection属性尚未初始化.

Default1

 使用系统;
使用 System.Configuration;
使用 System.Data;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.HtmlControls;
使用使用System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;
使用 System.Data.SqlClient;
使用 System.IO;


命名空间默认
{
    公共 部分  _Default:System.Web .UI.页面
    {
        //  SqlConnection sqlcon = new SqlConnection("PDF_Files"); 
        SqlCommand sqlcmd =  SqlCommand();
        //  SqlDataAdapter da =新的SqlDataAdapter(); 
        DataTable dt =  DataTable();
        受保护的 无效 Page_Load(对象发​​件人,EventArgs e)
        {

            如果(!Page.IsPostBack)
            {
                LoadGrid();
            }
            Label1.Text = " ;

        }

        无效 LoadGrid()
    {
        //  SqlConnection sqlcon = new SqlConnection("PDF_Files"); 
        SqlCommand sqlcmd = 新建 SqlCommand(" );
       SqlDataAdapter da =  SqlDataAdapter(sqlcmd);
        da.Fill(dt);
        如果(dt.Rows.Count >   0 )
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        //  sqlcon.Close(); 
    }

        受保护的 无效 Button1_Click(对象发​​件人,EventArgs e)
        {
            //  PDF将代码上传到SQL SERVER数据库表
            如果(FileUpload1.HasFile)
            {
                流fs = 默认(流);
                fs = FileUpload1.PostedFile.InputStream;
                BinaryReader br1 =  BinaryReader(fs);
                字节 [] pdfbytes = br1.ReadBytes(FileUpload1.PostedFile.ContentLength);
                
                SqlCommand sqlcmd = 新建 SqlCommand(" );
                sqlcmd.Parameters.Add(" ,FileUpload1.FileName);
                sqlcmd.Parameters.Add(" ,pdfbytes);
                sqlcmd.ExecuteNonQuery();
                //  sqlcon.Close(); 
                Label1.Text = " ;
                LoadGrid();
            }

        }


    }
} 





Default2

 使用系统;
使用 System.Collections;
使用 System.Configuration;
使用 System.Data;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.HtmlControls;
使用使用System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;
使用 System.Data.SqlClient;

命名空间默认
{
    公共 部分  Default2:System.Web .UI.页面
    {
        SqlConnection sqlcon =  SqlConnection(ConfigurationManager.ConnectionStrings [" ].ConnectionString);
        SqlCommand sqlcmd =  SqlCommand();
        SqlDataAdapter da =  SqlDataAdapter();
        DataTable dt =  DataTable();
        字符串 qstr;
        字节 [] b = 受保护的 无效 Page_Load(对象发​​件人,EventArgs e)
        {
            如果(!Page.IsPostBack)
            {
                qstr = Request.QueryString [" ];
                // 从DATABASE表pdf字段读取PDF文件
                SqlCommand sqlcmd = 新建 SqlCommand("  + qstr +  '",sqlcon);
                // 使用条件来检索特定的PDF 
                sqlcon.Open();
                da =  SqlDataAdapter(sqlcmd);
                da.Fill(dt);
                如果(dt.Rows.Count >   0 )
                {
                    b =(((字节 [])dt.Rows [ 0 ] [// 从数据库中收集字节并写入网页
                    Response.ContentType = " ;
                    Response.BinaryWrite(b);
                }
            }
        }
    }
} 

解决方案

您需要将sqlcon 代码从Default2添加到Default1-当前SqlCommand不知道要使用什么SqlConnection. /blockquote>

I was told to reference default1 to default2 once upload button has been clicked,
it returns invalid operation exception was unhandled by user code
Fill: SelectCommand.Connection property has not been initialized.

Default1

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;


namespace Defult
{
    public partial class _Default : System.Web.UI.Page
    {
        //SqlConnection sqlcon = new SqlConnection("PDF_Files");
        SqlCommand sqlcmd = new SqlCommand();
        //SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                LoadGrid();
            }
            Label1.Text = "";

        }

        void LoadGrid()
    {
        //SqlConnection sqlcon = new SqlConnection("PDF_Files");
        SqlCommand sqlcmd = new SqlCommand("select * from Files");
       SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        //sqlcon.Close();
    }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //PDF Upload Code to SQL SERVER database table
            if (FileUpload1.HasFile)
            {
                Stream fs = default(Stream);
                fs = FileUpload1.PostedFile.InputStream;
                BinaryReader br1 = new BinaryReader(fs);
                byte[] pdfbytes = br1.ReadBytes(FileUpload1.PostedFile.ContentLength);
                
                SqlCommand sqlcmd = new SqlCommand("insert into Files(pdfname,pdfcontent) values (@pdfname,@pdfcontent)");
                sqlcmd.Parameters.Add("@pdfname", FileUpload1.FileName);
                sqlcmd.Parameters.Add("@pdfcontent", pdfbytes);
                sqlcmd.ExecuteNonQuery();
                //sqlcon.Close();
                Label1.Text = "Successfully pdf upload to SQL Server database.";
                LoadGrid();
            }

        }


    }
}





Default2

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace Defult
{
    public partial class Default2 : System.Web.UI.Page
    {
        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["PDF_Files"].ConnectionString);
        SqlCommand sqlcmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        string qstr;
        byte[] b = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                qstr = Request.QueryString["ID"];
                //Read PDF file from DATABASE table pdf field
                SqlCommand sqlcmd = new SqlCommand("Select pdfcontent from pdfupload where ID='@ID" + qstr + "'", sqlcon); 
                //use condition to retrieve particulatr PDF
                sqlcon.Open();
                da = new SqlDataAdapter(sqlcmd);
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    b = ((byte[])dt.Rows[0][0]);
                    //Collect Bytes from database and write in Webpage
                    Response.ContentType = "application/pdf";
                    Response.BinaryWrite(b);
                }
            }
        }
    }
}

解决方案

You need to add the sqlcon code from Default2 to Default1 - at the moment the SqlCommand does not know what SqlConnection to use.


这篇关于在数据库之间存储和检索pdf文件的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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