SqlBulkCopy perfekt auf meinem lokalen Rechner funktioniert, aber mit der gleichen xlsx-Datei auf dem Server iImportieren eine Excel-Datei mit SqlBulkCopy nicht
Externe Tabelle nicht im erwarteten Format bin immer ist.
oder
Keine Fehlermeldung vorhanden, Ergebniscode: E_FAIL (0x80004005).
Diese Excel-Datei hat mehr als 20.000 Zeilen. Aber wenn ich mit 4000 Zeilen versuche, funktioniert es.
Meine Verbindungszeichenfolge ist wie folgt:
<add name = "Excel07+ConString" connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES'"/>
Hier ist der Hauptcode
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();
}
}
}
Und hier ist die Ausnahme, die ich erhalten. Sorry, aber ich kann das im String-Format bekommen.
System.Data.OleDb.OleDbException (0x80004005): Externe Tabelle ist nicht im erwarteten Format. bei System.Data.OleDb.OleDbConnectionInternal..ctor (OleDbConnectionString-Konstrukt, OleDbConnection-Verbindung) bei System.Data.OleDb.OleDbConnectionFactory.CreateConnection (DbConnectionOptions-Optionen, DbConnectionPoolKey-PoolKey, Objekt poolGroupProviderInfo, DbConnectionPool-Pool, DbConnection OwningObject) bei System.Data.ProviderBase .DbConnectionFactory.CreateConnection (DbConnectionOptions Optionen, DbConnectionPoolKey poolKey, Objekt poolGroupProviderInfo, DbConnectionPool Pool, DbConnection owningConnection, DbConnectionOptions Useroptions) bei System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection (DbConnection owningConnection, DbConnectionPoolGroup Poolgroup, DbConnectionOptions Useroptions) bei System.Data.ProviderBase. 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 Wiederholung, DbConnectionOptions userOptions) bei System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection (DbConnection outerConnection , DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) bei System.Data.ProviderBase.DbConnectionInternal.OpenConnection (DbConnection outerConnection, DbConnectionFactory connectionFactory) bei System.Data.OleDb.OleDbConnection.Open() bei GenWork.Areas.Xls.Controllers. XlsIsometricController.ParseImportedExcel (String xlsFilePath, Int32 xlsWlbIsometricMasterId)
Wo tritt die Ausnahme auf? * Warum * denkst du, dass es mit der Anzahl der Zeilen zu tun hat? Sie haben es mit zwei verschiedenen Dateien versucht und die Nachricht klagt über das * Dateiformat *. Bitte posten Sie die * full * -Ausnahme einschließlich ihres Aufruf-Stacks.Sie können es einfach mit 'exception.ToString()' –
BTW das ist eine * winzige * Anzahl von Zeilen, auch für ein Excel-Blatt. Was * ist * das Dateiformat und wie wurde es produziert? Ist es eine echte Excel-Datei oder eine CSV/HTML-Datei mit einer Excel-Erweiterung, die aus einer Anwendung generiert wurde? –
Wie [diese doppelte Frage] (http://stackoverflow.com/questions/1139390/excel-external-table-is-not-in-the-expected-format) zeigt, tritt dieser Fehler beim Versuch auf, eine Excel-Datei zu laden Verwenden Sie die falsche Versionsnummer. Versuchen Sie eine 'xls' Datei zu laden? –