如何利用一个FILE控件,一次上传多个附件到数据库

来源:互联网 发布:mac 安卓开发环境搭建 编辑:程序博客网 时间:2024/04/29 18:41
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Configuration;

 

namespace bjoffice
{
 /// <summary>
 /// upFILEs 的摘要说明。
 /// </summary>
 public class upFILEs : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.WebControls.Button Button2;
  protected System.Web.UI.WebControls.DropDownList FILEs;
  protected System.Web.UI.HtmlControls.HtmlInputFILE myFILE;
  protected System.Web.UI.WebControls.Button Button3;
  protected static DataTable upFILEs1;
  protected static SqlDataAdapter upFILEs2;

  public void CreatTable()
  {
   SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
   SqlDataAdapter myCommand = new SqlDataAdapter("select FILEName,FILEType,FILELen,FILEData,[ID] from Accessories where [ID]<-1", myConnection);   
   DataSet mydataset = new DataSet();
   myCommand.Fill(mydataset, "Accessoriestable");
   upFILEs2 = myCommand;
   upFILEs1 = mydataset.Tables["Accessoriestable"];
  }

  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Button2.Click += new System.EventHandler(this.Button2_Click);
   this.Button3.Click += new System.EventHandler(this.Button3_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Button1_Click(object sender, System.EventArgs e)
  {
   if (upFILEs1 == null)
   {
    CreatTable();
   }
   if( myFILE.PostedFILE.FILEName != "" )
   {
    string FILEname1 = Path.GetFILEName(myFILE.PostedFILE.FILEName) ;
    //if(upFILEs1.Rows.Find(FILEname)==null)
    DataRow[] myrow=upFILEs1.Select("FILEName='"+FILEname1.ToString()+"'");
     if(myrow.Length ==0)
    {
     int FILElen = myFILE.PostedFILE.ContentLength;
     string FILEtype = myFILE.PostedFILE.ContentType;
     Stream fstream = myFILE.PostedFILE.InputStream;
     byte[] FILEdata = new byte[FILElen];         
     fstream.Read(FILEdata,0,FILElen);

     DataRow dr = upFILEs1.NewRow();
     dr[0] = FILEname1;
     dr[1] = FILEtype;
     dr[2] = FILElen;
     dr[3] = FILEdata;
     upFILEs1.Rows.Add( dr );
     FILEs.DataSource = upFILEs1;
     FILEs.DataBind();

    }
   }
   else
   {
    Page.RegisterStartupScript("script", "<script>alert('请选择文件!');</script>");
   }

  }

  private void Button2_Click(object sender, System.EventArgs e)
  {
   if(FILEs.SelectedIndex>=0)
   {
    string fname=FILEs.SelectedItem.Text;
    DataRow[] myrow=upFILEs1.Select("FILEName='"+fname+"'");
    if(myrow.Length>0)
     upFILEs1.Rows.Remove(myrow[0]);
    FILEs.DataSource=upFILEs1;
    FILEs.DataBind();
   }
   else
   {
    Page.RegisterStartupScript("script", "<script>alert('请选择文件!');</script>");
   }

  }

  private void Button3_Click(object sender, System.EventArgs e)
  {
   if( upFILEs1.Rows.Count > 0 )
   {
       SqlCommandBuilder myscb =new SqlCommandBuilder(upFILEs2);
                upFILEs2.InsertCommand=myscb.GetInsertCommand();
                upFILEs2.Update(upFILEs1);
   }
   upFILEs1 = null;
  }

  }
 }

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击