SqlBulkCopy在我的本地机器上工作得很好,但在服务器上使用相同的xlsx文件
外部表格不符合预期格式。
或
没有可用的错误消息,结果代码:E_FAIL(0x80004005)。
这个excel文件有超过20,000行。但是,如果我尝试4000行,它是有效的。
我的连接字符串如下:
<add name = "Excel07+ConString" connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES'"/>
这是主要代码
string conString = string.Empty;
conString = ConfigurationManager.ConnectionStrings["Excel07+ConString"].ConnectionString;
conString = string.Format(conString, xlsFilePath);
using (OleDbConnection excel_con = new OleDbConnection(conString))
{
excel_con.Open();
string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
DataTable dtExcelData = new DataTable();
dtExcelData.Columns.AddRange(new DataColumn[12] {
new DataColumn("Class", typeof(string)),
new DataColumn("Dia", typeof(double)),
new DataColumn("Unit", typeof(int)),
new DataColumn("Train", typeof(string)),
new DataColumn("Seq", typeof(string)),
new DataColumn("RevisionNo", typeof(int)),
new DataColumn("Fluid", typeof(string)),
new DataColumn("EngineeringTransCode", typeof(string)),
new DataColumn("Insulation", typeof(string)),
new DataColumn("PaintCode", typeof(string)),
new DataColumn("Pid", typeof(int)),
new DataColumn("RalCode", typeof(string))
});
using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con))
{
oda.Fill(dtExcelData);
}
excel_con.Close();
excel_con.Dispose();
string consString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
//Set the database table name
sqlBulkCopy.DestinationTableName = "dbo.XlsWlbIsometric";
//[OPTIONAL]: Map the Excel columns with that of the database table
//sqlBulkCopy.ColumnMappings.Add("Id", "PersonId");
sqlBulkCopy.ColumnMappings.Add("Class", "Class");
sqlBulkCopy.ColumnMappings.Add("Dia", "Dia");
sqlBulkCopy.ColumnMappings.Add("Unit", "Unit");
sqlBulkCopy.ColumnMappings.Add("Train", "Train");
sqlBulkCopy.ColumnMappings.Add("Seq", "Seq");
sqlBulkCopy.ColumnMappings.Add("RevisionNo", "RevisionNo");
sqlBulkCopy.ColumnMappings.Add("Fluid", "Fluid");
sqlBulkCopy.ColumnMappings.Add("EngineeringTransCode", "EngineeringTransCode");
sqlBulkCopy.ColumnMappings.Add("Insulation", "Insulation");
sqlBulkCopy.ColumnMappings.Add("PaintCode", "PaintCode");
sqlBulkCopy.ColumnMappings.Add("Pid", "Pid");
sqlBulkCopy.ColumnMappings.Add("RalCode", "RalCode");
con.Open();
sqlBulkCopy.WriteToServer(dtExcelData);
sqlBulkCopy.Close();
con.Close();
con.Dispose();
}
}
}
这是我收到的例外情况。对不起,但我可以用字符串格式得到它。
System.Data.OleDb.OleDbException(0x80004005):外部表格不是预期的格式。 System.Data.ProviderBase上的System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions选项,DbConnectionPoolKey poolKey,Object poolGroupProviderInfo,DbConnectionPool池,DbConnection owningObject)上的System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr,OleDbConnection连接) .DbConnectionFactory.CreateConnection在System.Data.ProviderBase(DbConnectionOptions选项,DbConnectionPoolKey poolKey,对象poolGroupProviderInfo,池类DBConnectionPool,的DbConnection owningConnection,DbConnectionOptions USEROPTIONS)在System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(的DbConnection owningConnection,DbConnectionPoolGroup poolGroup,DbConnectionOptions USEROPTIONS)。 System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection,DbConnectionFactory con)中的DbConnectionFactory.TryGetConnection(DbConnection owningConnection,TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1次重试,DbConnectionOptions userOptions)在GenWork.Areas.Xls.Controllers.XlsIsometricController的System.Data.OleDb.OleDbConnection.Open()处的System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory)处的nectionFactory,TaskCompletionSource`1 retry,DbConnectionOptions userOptions。 ParseImportedExcel(String xlsFilePath,Int32 xlsWlbIsometricMasterId)
答案 0 :(得分:0)
如果没有更多信息,我认为问题在于您没有批处理操作。 SqlBulkCopy具有您可以设置的批处理大小属性。您没有发布任何代码,因此如果您正在设置它,则不清楚。如果不是,请尝试将其设置为5,000-10,000,然后重试。
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.batchsize.aspx
或者在第4,000行之后可能存在数据问题。如果您不这样做,请将您的SqlBulkCopy代码包装在try / catch中,以查看内部异常中是否有更多详细信息。使用交易也可能是一个好主意。
https://msdn.microsoft.com/en-us/library/tchktcdk(v=vs.110).aspx