PDF文件使用asp.net从DataBase检索 [英] PDF File retrieving from DataBase using asp.net

查看:73
本文介绍了PDF文件使用asp.net从DataBase检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  string  SQL =   SELECT * FROM DPSpdf WHERE intpdfId =' + txtdanceid.Text +  '; 
SqlCommand myCommand = new SqlCommand(SQL,(SqlConnection)Application.Get( DPS));
myCommand.Parameters.AddWithValue( @ intpdfId,txtdanceid.Text);

SqlDataReader myReader = myCommand.ExecuteReader();

if (myReader.Read())
{
Response.ContentType = myReader [ ContentType]。ToString();
Response.BinaryWrite(( byte [])myReader [ strpdfurl]);

}

myReader.Close();
Response.End();





我哪里错了?

解决方案





您一次传递内联参数和命令参数。你需要选择其中一个,更好的选择是传递命令参数而不是内联值。



请参阅下面修改过的sql语句,



  string  SQL =  < span class =code-string> SELECT * FROM DPSpdf WHERE intpdfId = @ intpdfId; 
SqlCommand myCommand = new SqlCommand(SQL,(SqlConnection)Application.Get( DPS));
myCommand.Parameters.AddWithValue( @ intpdfId,txtdanceid.Text);
SqlDataReader myReader = myCommand.ExecuteReader();
if (myReader.Read())
{
Response.ContentType = myReader [ ContentType]。ToString();
Response.BinaryWrite(( byte [])myReader [ strpdfurl]);
}

myReader.Close();
Response.End();


试用此代码





 System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); 
byte [] data =(byte [])myReader [strpdfurl];
System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;

Response.Charset =;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = myReader [ContentType]。ToString();
Response.AddHeader(content-disposition,attachment; filename =+Test+。pdf);
Response.BinaryWrite(data);
Response.Flush();
Response.End();


服务器错误   '  / WebSite2'Application。 
ExecuteReader:连接属性 已初始化。


string SQL = "SELECT * FROM DPSpdf WHERE intpdfId='" + txtdanceid.Text + "'";
        SqlCommand myCommand = new SqlCommand(SQL, (SqlConnection)Application.Get("DPS"));
        myCommand.Parameters.AddWithValue("@intpdfId", txtdanceid.Text);

        SqlDataReader myReader = myCommand.ExecuteReader();

        if (myReader.Read())
        {
            Response.ContentType = myReader["ContentType"].ToString();
            Response.BinaryWrite((byte[])myReader["strpdfurl"]);

        }

        myReader.Close();
        Response.End();



Where i did mistake?

解决方案

Hi,

You are passing inline parameter and command parameter at a time. You need to choose one of them, better choice is passing command parameter instead of inline value.

Please see the modified sql statement below,

string SQL = "SELECT * FROM DPSpdf WHERE intpdfId=@intpdfId";
SqlCommand myCommand = new SqlCommand(SQL, (SqlConnection)Application.Get("DPS"));
myCommand.Parameters.AddWithValue("@intpdfId", txtdanceid.Text);
SqlDataReader myReader = myCommand.ExecuteReader();
if (myReader.Read())
{
   Response.ContentType = myReader["ContentType"].ToString();
   Response.BinaryWrite((byte[])myReader["strpdfurl"]);
}

myReader.Close();
Response.End();


Try this code


System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] data = (byte[])myReader["strpdfurl"];
System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;

Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = myReader["ContentType"].ToString();
Response.AddHeader("content-disposition", "attachment;filename=" + "Test" + ".pdf");
Response.BinaryWrite(data);
Response.Flush();
Response.End();


Server Error in '/WebSite2' Application.
ExecuteReader: Connection property has not been initialized.


这篇关于PDF文件使用asp.net从DataBase检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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