2015-11-02 83 views
1

我正在使用下面的代码将Excel数据导入到DB表中。该代码在我的本地工作正常,当我将此代码移动到服务器时,导入失败。此外,我没有收到任何错误消息,并且我成功保存了一条消息数据。无法将Excel导入到服务器中的DB表中

例如:excel有75,000个数据,只有13,500条记录被插入,并且excel文件的大小为5 MB。

有关可能的问题的任何建议?

CS:

protected void btnImportData_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      string strCS = string.Empty; ; 
      string strFileType = Path.GetExtension(FileUploadExcel.FileName).ToLower(); 
      string query = ""; 
      lblError.Text = ""; 
      string FileName = string.Empty; 
      FileName = Path.GetFileName(FileUploadExcel.PostedFile.FileName); 
      string Extension = Path.GetExtension(FileUploadExcel.PostedFile.FileName); 
      string FolderPath = ConfigurationManager.AppSettings["FolderPath"]; 
      string path = Path.GetFileName(Server.MapPath(FileUploadExcel.FileName)); 
      System.IO.File.Delete(Server.MapPath(FolderPath) + path);  
      FileUploadExcel.SaveAs(Server.MapPath(FolderPath) + path); 
      string filePath = Server.MapPath(FolderPath) + path; 

      if (strFileType != String.Empty) 
      { 
       if (strFileType.Trim() == ".xls") 
       { 
        strCS = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\""; 
       } 
       else if (strFileType.Trim() == ".xlsx") 
       { 
        strCS = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; 
       } 
       else 
       { 
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please upload the correct file format')", true); 
        return; 
       } 
       try 
       { 
        OleDbConnection conn = new OleDbConnection(strCS); 
        if (conn.State == ConnectionState.Closed) 
         conn.Open(); 
        System.Data.DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
        string sheetname = dt.Rows[0]["Table_Name"].ToString(); 
        query = "SELECT * FROM [" + sheetname + "]"; 
        OleDbCommand cmd = new OleDbCommand(query, conn); 
        OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
        DataSet ds = new DataSet(); 
        da.Fill(ds); 
        if (conn.State == ConnectionState.Open) 
        { 
         conn.Close(); 
         conn = null; 
        } 
        string strSqlTable = "TABLENAME"; 
        string sclearsql = "delete from " + strSqlTable; 
        SqlConnection sqlconn = new SqlConnection(strCon); 
        SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn); 
        sqlconn.Open(); 
        sqlcmd.ExecuteNonQuery(); 
        sqlconn.Close(); 
        OleDbConnection oledbconn = new OleDbConnection(strCS); 
        oledbconn.Open(); 
        OleDbCommand oledbcmd = new OleDbCommand(query, oledbconn); 
        oledbcmd.CommandTimeout = 120; 
        OleDbDataReader dReader = oledbcmd.ExecuteReader(); 
        SqlBulkCopy bulkCopy = new SqlBulkCopy(strCon); 
        bulkCopy.DestinationTableName = strSqlTable; 
        bulkCopy.BulkCopyTimeout = 120; 
        bulkCopy.BatchSize = 1000; 
        bulkCopy.WriteToServer(dReader); 
        oledbconn.Close(); 
        ClientScript.RegisterStartupScript(Page.GetType(), "alert", "alert('Data saved successfully');window.location='Panel.aspx';", true); 
       } 
       catch (Exception ex) 
       { 
        lblError.Text = "Upload status: The file could not be uploaded due to following reasons.Please check: " + ex.Message; 
       } 
      } 
      else 
      { 
       ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please select a file to import the data.')", true); 
      } 
     } 
     catch (Exception ex) 
     { 
      lblError.Text = "Upload status: The file could not be uploaded due to following reasons.Please check: " + ex.Message; 
     } 
    } 

回答

0

喜删除数据读取器上你代码读取Excel文件的两倍,并使用数据集来填写你的SQL表:

请看下图:

DataTable dtexcel = new DataTable(); 

      using (OleDbConnection conn = new OleDbConnection(strCS)) 
      { 
       conn.Open(); 

       DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); 

       //Looping a first Sheet of Xl File 
       DataRow schemaRow = schemaTable.Rows[0]; 
       string sheet = schemaRow["TABLE_NAME"].ToString(); 

       if (!sheet.EndsWith("_")) 
       { 
        string query = "SELECT * FROM [" + sheet + "]"; 
        OleDbDataAdapter daexcel = new OleDbDataAdapter(query, conn); 
        dtexcel.Locale = CultureInfo.CurrentCulture; 
        daexcel.Fill(dtexcel); 
       } 
      } 

      using (SqlConnection sqlconn = new SqlConnection(strCon)) 
      { 
       SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn); 
       sqlconn.Open(); 
       sqlcmd.ExecuteNonQuery(); 
      } 


      using (SqlConnection sqlconn = new SqlConnection(strCon)) 
      { 
       using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(sqlconn)) 
       { 
        //Set the database table name 
        sqlBulkCopy.DestinationTableName = strSqlTable; 

        sqlconn.Open(); 

        sqlBulkCopy.WriteToServer(dtexcel); 

       } 
      } 
+0

我实际上最初使用这个代码,这不起作用。我想知道什么时候它在本地工作,为什么这不起作用的服务器。任何兼容性问题? – Michael

+0

请参阅我的更新代码。你能发布一个你用来连接服务器的虚拟sql连接吗?我使用该代码来导入350k记录,而且我没有任何问题。 – BizApps

+0

public static String strCon = ConfigurationManager.ConnectionStrings [“ConnectionString”] .ConnectionString;'IN WEB.CONFIG FILE' ' – Michael

0

您是否尝试过使用dts向导?如果你有一个Excel数据表,它是一个很好的工具。
您可以通过打开命令提示符来使用它。 windowsbutton + r,并写入cmd
并从这里你写dtswizard
然后向导在一个新的窗口,您可以选择要插入Excel表格中打开。谷歌有很多教程。
希望你能使用这个

+0

问题是需要在.net UI中完成,否则我可以在SSIS中轻松完成此操作。 – Michael