简体   繁体   中英

Error in Bulk Upload in SQL server from Excel 2007 using C#

I am trying to insert in SQL using Bulk upload. It is working well when I am using from local, even working on Test live URL too. But when I am uploading on Live it breaks after uploading 10000 rows.

Below is my code:

public bool ExportExcelToSql (DataTable DT)
    {
        bool result = false;
        tableColumns = "*";
        try
        {
            using (var connection = new OleDbConnection(_excelConnectionString))
            {
                connection.Open();
                DataTable dt = null;
                dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                _excelSheetName = dt.Rows[0]["TABLE_NAME"].ToString();
                var command = new OleDbCommand("Select " + tableColumns + " FROM [" + _excelSheetName + "]", connection);
                command.CommandTimeout = 6000;
                using (DbDataReader dr = command.ExecuteReader())
                {
                    string conString = _sqlConnectionString;
                    var sqlConn = new SqlConnection(conString);
                    sqlConn.Open();

                    using (var bulkCopy = new SqlBulkCopy(sqlConn))
                    {
                        bulkCopy.BulkCopyTimeout = 6000;
                        bulkCopy.DestinationTableName = tableName.ToString();
                        for (int i = 0; i < DT.Rows.Count; i++)
                        {
                            bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(DT.Rows[i][0].ToString(), DT.Rows[i][1].ToString(),));
                        }
                        bulkCopy.WriteToServer(dr);
                    }
                    result = true;
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return result;
    }

I would split the file in chunks and import them one at a time. No, there is no such limit after 100,000 rows. I would suspect that the problem is that the file does not match the format file or that your using something like SQL Express 2008 R2 is limited to 4GB database size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM