当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  用ado.net对word,excel进行存取

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

点击次数:30 次 发布日期:2008-11-26 15:55:22 作者:源代码网
源代码网推荐      blob表
源代码网推荐  
源代码网推荐  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//这个东西主要用来列表,把已经有的东西列出来
源代码网推荐  
源代码网推荐  <asp:HyperLinkColumn DataNavigateUrlField="id" HeaderText="产品名称" 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//这个文件比较重要负责把村道数据库里面的文件,按照格式,按照名称,给传输出来
源代码网推荐  
源代码网推荐   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");//设置输出流为简体中文
源代码网推荐   //Response.ContentType = "application/ms-word";//设置输出文件类型为word文件。
源代码网推荐   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();
源代码网推荐   }
源代码网推荐  
源代码网推荐  
源代码网推荐  这里要说的有两点,第一,就是把文件的名称getFile.aspx变成我们想要的名称。
源代码网推荐  
源代码网推荐   Response.AppendHeader("Content-Disposition", "attachment;filename="+FileName);
源代码网推荐  
源代码网推荐  第二,就是把指定的名称变成我们想要的值,是标准的中文,而不是中文的乱码。
源代码网推荐  
源代码网推荐   FileName=System.Web.HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8 );
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华