本人初学不久,这代码是在参考前人的基础上,自己写的,通过Upload控件选择上传的EXCEL文件导入SQLserver。但这代码看起来还是有些别扭,希望大家帮忙改进下!!
 protected void Button2_Click(object sender, EventArgs e)
    {        if (FileUpload1.PostedFile.FileName.ToString() != null && FileUpload1.PostedFile.FileName.ToString() != "" && FileUpload1.PostedFile.FileName.EndsWith(".xls"))
        {            
            DataSet ds = null;
                       
            try
            {
                string path = FileUpload1.FileName;                string strFilePath = MapPath("excel") + "\\" + path;                   FileUpload1.PostedFile.SaveAs(strFilePath); //文件是上传了,但用完了怎么把它删除呢?                       ds = ExcelToDS(strFilePath);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)    
                {
                    for (int j = 0; j < 10; j++)
                    {
                        string colvalue = ds.Tables[0].Rows[i].ItemArray[j].ToString();                        if (j < 2 && colvalue == "")     //   检查在所需字段中是否有空值 
                        {
                           Response.Write(" <script >window.alert( '数据导入发生错误!')</script >");
                           
                            // 到这里,数据导入就不再执行,直接跳转到原页面,但如何显示出已经导入了几条数据呢?
                                // 这里是最别扭的地方。                               
                        }
                    }                    int shipID = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]);
                          
                    …………     //  其它数据定义赋值略              
                    string re = ds.Tables[0].Rows[i].ItemArray[10].ToString();
                      
                                         
                    SqlConnection myConnection = new SqlConnection(……);                    String insertCmd = "INSERT INTO [CSSB](…) " + "VALUES (……)";                    SqlCommand myCommand = new SqlCommand(insertCmd, myConnection);                    myCommand.Parameters.Add(new SqlParameter("@shipID", shipID));
                    
                    ……………………                    myCommand.Parameters.Add(new SqlParameter("@re", re));                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    myConnection.Close();                }            }
            catch (SqlException exc)
            {
                // 即使发生了错误,怎么没看见它跳出来过啊?                Response.Write(" <script >'导入数据发生错误:exc.Message'</script>");                  return;
            }            Label1.Text = "已成功导入数据!";            Response.Redirect(Request.RawUrl);    //  此句导致Label控件的显示被忽略!    
        }            else Label1.Text = "请至少选择一个Excel文件导入!";
    }      

解决方案 »

  1.   

    删除文件用File.Delete(strFilePath);就可以了。
    Response.Write("  <script  > '导入数据发生错误:exc.Message ' </script >");
    这句改成:
    Response.Write("  <script  > window.alert('导入数据发生错误:" + exc.Message+ "');</script >");
      

  2.   

    File.Delete(strFilePath);这句话应加在什么位置呢?我试了很多种位置,都说“正由另一进程使用,因此该进程无法访问该文件”。这是为什么呢?
      

  3.   

    已解决,放在finall{}里就可以了.
      

  4.   

    已解决,放在finall{}里就可以了.
      

  5.   

    我放在finally里面还是不成功啊、、、、