日期:2014-05-17  浏览次数:20338 次

excel导入SQL数据库,的Button事件问题
excel导入SQL数据库,有2个按钮事件,Button1_Click把数据在表中显示下,不对数据库进行操作,Button2_Click对数据库进行操作,但是总是出错,无效参数,如果单独执行Button2_Click对数据库进行操作是可以的,执行Button1_Click也可以。但是先Button1_Click在Button2_Click就无效参数了。是不是执行2次
C# code
string sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + FileUpload1.PostedFile.FileName.ToString() + ";" + "Extended Properties=\"Excel 8.0;\"";
        OleDbConnection objConn = new OleDbConnection(sConnectionString);
        objConn.Open();
        OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
        OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
        objAdapter1.SelectCommand = objCmdSelect;
        DataSet objDataset1 = new DataSet();
        objAdapter1.Fill(objDataset1);

不行啊,那位高手帮解决下,2天了

SqlConnection conn = new SqlConnection("Data Source=.;Database=hhxy_gzcx;User ID=sa;PWD=11");

  protected void Page_Load(object sender, EventArgs e)
  {
   
  }

  protected void Button1_Click(object sender, EventArgs e)
  {
  string sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + FileUpload1.PostedFile.FileName.ToString() + ";" + "Extended Properties=\"Excel 8.0;\"";
  OleDbConnection objConn = new OleDbConnection(sConnectionString);
  objConn.Open();
  OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
  OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
  objAdapter1.SelectCommand = objCmdSelect;
  DataSet objDataset1 = new DataSet();
  objAdapter1.Fill(objDataset1);
  GridView1.DataSource = objDataset1;
  GridView1.DataBind();  
  }


  protected void Button2_Click(object sender, EventArgs e)
  {
  string sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + FileUpload1.PostedFile.FileName.ToString() + ";" + "Extended Properties=\"Excel 8.0;\"";
  OleDbConnection objConn = new OleDbConnection(sConnectionString);
  objConn.Open();
  OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
  OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
  objAdapter1.SelectCommand = objCmdSelect;
  DataSet objDataset1 = new DataSet();
  objAdapter1.Fill(objDataset1);
  DataTable dt = objDataset1.Tables[0];
  DataView myView = new DataView(dt);
  SqlCommand cmd = conn.CreateCommand();
  cmd.CommandText = "INSERT INTO user_table values(@姓名,@密码)";
  int count=0;
  try
  {
  foreach (DataRowView myDrv in myView)
  {
  count++;
  if (conn.State.ToString() != "Closed")
  conn.Close();
  cmd.Parameters.Clear();

  SqlParameter 姓名 = cmd.Parameters.Add("@姓名", SqlDbType.Char);
  SqlParameter 密码 = cmd.Parameters.Add("@密码", SqlDbType.Char);

  姓名.Direction = ParameterDirection.Input;
  密码.Direction = ParameterDirection.Input;

  姓名.Value