Ado.net access to Word and Excel

Source: Internet
Author: User
Tags tostring urlencode

Blob table

3 ID int 4 0

0 Name Char 50 1

0 BLOB Image 16 1

0 Type char 60 1

SaveFile.aspx.cs

PRivate void Button1_Click (object sender, System.EventArgs e)

{

Stream imgdatastream = File1.PostedFile.InputStream;

int imgdatalen = File1.PostedFile.ContentLength;

string imgtype = File1.PostedFile.ContentType;

String name = This.getfilenamebyurl (this.File1.PostedFile.FileName);

byte[] Imgdata = new Byte[imgdatalen];

int n = imgdatastream. Read (Imgdata,0,imgdatalen);

String connstr = "Workstation Id=overmind;packet size=4096;user id=sa;password=sa;data source=overmind;persist Security Info=false;initial CATALOG=WZTJ ";

SqlConnection connection = new SqlConnection (CONNSTR);

SqlCommand command = new SqlCommand ("INSERT into blob (NAME,TYPE,BLOB) VALUES (@imgtitle, @type, @blob)", connection);

SqlParameter paramtitle = new SqlParameter ("@imgtitle", sqldbtype.varchar,50);

Paramtitle.value = name;

Command. Parameters.Add (Paramtitle);

SqlParameter paramdata = new SqlParameter ("@blob", sqldbtype.image);

Paramdata.value = Imgdata;

Command. Parameters.Add (Paramdata);

SqlParameter paramtype = new SqlParameter ("@type", sqldbtype.varchar,50);

Paramtype.value = Imgtype;

Command. Parameters.Add (Paramtype);

WZTJ.DEBUG.TESTSQL.TRACEERRORSQL ("INSERT into blob (NAME,TYPE,BLOB) VALUES (@imgtitle, @type, @blob)", command. Parameters);

Connection. Open ();

int numrowsaffected = command. ExecuteNonQuery ();

Connection. Close ();

}

listfile.aspx//this thing is mainly used to list, the things that have been listed

<asp:hyperlinkcolumn datanavigateurlfield= "id" headertext= "Product name datanavigateurlformatstring="./getFile.aspx? id={0} "datatextfield=" name datatextformatstring= "{0}" itemstyle-horizontalalign= "Center" itemstyle-width= "160px" >

ListFile.aspx.cs

String connstr= "Workstation Id=overmind;packet size=4096;user id=sa;password=sa;data source=overmind;persist Security Info=false;initial CATALOG=WZTJ ";

SqlConnection connection = new SqlConnection (CONNSTR);

SqlCommand command = new SqlCommand ("SELECT * from blob", connection);

Connection. Open ();

SqlDataAdapter adaptor = new SqlDataAdapter (command);

DataSet ds = new DataSet ();

Adaptor. Fill (ds, "blob");

Connection. Close ();

This. Datagrid1.datasource=ds. tables["Blob"]. DefaultView;

This. Datagrid1.databind ();

getfile.aspx.cs//This file is more important responsible for the village road database inside the file, according to the format, according to the name, to transmit out

private void Page_Load (object sender, System.EventArgs e)

{

String Imgid =this. Request.QueryString.Get ("ID");

request.querystring["Imgid"];

String connstr= "Workstation Id=overmind;packet size=4096;user id=sa;password=sa;data source=overmind;persist Security Info=false;initial CATALOG=WZTJ ";

String sql= "SELECT Name,blob, type from blob WHERE id =" + imgid;

SqlConnection connection = new SqlConnection (CONNSTR);

SqlCommand command = new SqlCommand (sql, connection);

Connection. Open ();

SqlDataReader dr = command. ExecuteReader ();

if (Dr. Read ())

{

Response.Clear ();

Response.buffer= true;

Response.charset= "GB2312";

Response.contentencoding=system.text.encoding.getencoding ("GB2312");/set output stream to Simplified Chinese

Response.ContentType = "Application/ms-word";//Set Output file type is Word file.

Response.ContentType = dr["type"]. ToString ();

Response.BinaryWrite ((byte[]) dr["blob"]);

String FileName = dr["Name"]. ToString (). Trim ();

Filename=system.web.httputility.urlencode (Filename,system.text.encoding.utf8);

Response.appendheader ("Content-disposition", "attachment;filename=" +filename);

}

Connection. Close ();

}

Here to say there are two points, first, is the file name getfile.aspx into the name we want.

Response.appendheader ("Content-disposition", "attachment;filename=" +filename);

Second, the specified name into the value we want, is the standard Chinese, not the Chinese garbled.

Filename=system.web.httputility.urlencode (Filename,system.text.encoding.utf8);

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.