熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

用ado.net對word,excel進行存取

2013-11-13 10:07:34  來源: .NET編程 
    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
   


From:http://tw.wingwit.com/Article/program/net/201311/12630.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.