c#

位置:IT落伍者 >> c# >> 浏览文章

用ado.net对word,excel进行存取


发布日期:2022年02月09日
 
用ado.net对word,excel进行存取
blob表

id int

name char

blob image

type char

saveFileaspxcs

PRivate void Button_Click(object sender SystemEventArgs e)

{

Stream imgdatastream = FilePostedFileInputStream;

int imgdatalen = FilePostedFileContentLength;

string imgtype = FilePostedFileContentType;

string name = thisgetFileNameByURL(thisFilePostedFileFileName)

byte[] imgdata = new byte[imgdatalen];

int n = imgdatastreamRead(imgdataimgdatalen)

string connstr = workstation id=OVERMIND;packet size=;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(nametypeblob) VALUES ( @imgtitle @type@blob ) connection )

SqlParameter paramTitle = new SqlParameter(@imgtitle SqlDbTypeVarChar

paramTitleValue = name;

commandParametersAdd(paramTitle)

SqlParameter paramData = new SqlParameter( @blob SqlDbTypeImage )

paramDataValue = imgdata;

commandParametersAdd( paramData )

SqlParameter paramType = new SqlParameter( @type SqlDbTypeVarChar

paramTypeValue = imgtype;

commandParametersAdd( paramType )

wztjdebugTestSQLTraceErrorSql(INSERT INTO blob(nametypeblob) VALUES ( @imgtitle @type@blob )commandParameters)

connectionOpen()

int numRowsAffected = commandExecuteNonQuery()

connectionClose()

}

listFileaspx//这个东西主要用来列表把已经有的东西列出来

<asp:HyperLinkColumn DataNavigateUrlField=id HeaderText=产品名称 DataNavigateUrlFormatString=/getFileaspx?ID={} DataTextField=name DataTextFormatString={} ItemStyleHorizontalAlign=Center ItemStyleWidth=px>

listFileaspxcs

string connstr=workstation id=OVERMIND;packet size=;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 )

connectionOpen()

SqlDataAdapter adaptor = new SqlDataAdapter(command)

DataSet ds = new DataSet()

adaptorFill(dsblob

connectionClose()

thisDataGridDataSource=dsTables[blob]DefaultView;

thisDataGridDataBind()

getFileaspxcs//这个文件比较重要负责把村道数据库里面的文件按照格式按照名称给传输出来

private void Page_Load(object sender SystemEventArgs e)

{

string imgid =thisRequestQueryStringGet(ID

//RequestQueryString[imgid];

string connstr=workstation id=OVERMIND;packet size=;user id=sa;password=sa;data source=OVERMIND;persist security info=False;initial catalog=wztj;

string sql=SELECT nameblob type FROM blob WHERE id = + imgid;

SqlConnection connection = new SqlConnection(connstr)

SqlCommand command = new SqlCommand(sql connection)

connectionOpen()

SqlDataReader dr = commandExecuteReader()

if(drRead())

{

ResponseClear()

ResponseBuffer= true;

ResponseCharset=GB;

ResponseContentEncoding=SystemTextEncodingGetEncoding(GB//设置输出流为简体中文

//ResponseContentType = application/msword;//设置输出文件类型为word文件

ResponseContentType = dr[type]ToString()

ResponseBinaryWrite( (byte[]) dr[blob] )

string FileName = dr[name]ToString()Trim()

FileName=SystemWebHttpUtilityUrlEncode(FileNameSystemTextEncodingUTF

ResponseAppendHeader(ContentDisposition attachment;filename=+FileName)

}

connectionClose()

}

这里要说的有两点第一就是把文件的名称getFileaspx变成我们想要的名称

ResponseAppendHeader(ContentDisposition attachment;filename=+FileName)

第二就是把指定的名称变成我们想要的值是标准的中文而不是中文的乱码

FileName=SystemWebHttpUtilityUrlEncode(FileNameSystemTextEncodingUTF

               

上一篇:浅析C#语言实现堆和队列数据结构

下一篇:C# 4.0中泛型协变性和逆变性详解