MS SQL Serverデータベースに3000行のExcelファイルをインポートする必要があります。エンティティフレームワークでC#を使用していますが、操作は15〜20分ほど長くかかります。 何か間違っているのですか、これは正常ですか?C#エンティティフレームワークでSQLにExcelファイルをインポートする
はここでパスがファイルをExcelにパスがどこにあるか私のコードからのforeachループです:
foreach (var item in paths)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(item.Value);
Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.UsedRange;
try
{
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
string GenDateString = xlRange.Cells[3, 6].Value2.ToString();
string GenDate = GenDateString.Substring(0, 10);
string Client = xlRange.Cells[3, 1].Value2.ToString();
string User = xlRange.Cells[3, 2].Value2.ToString();
string error;
if (IsCorrectDate(item.Key, xlRange))
{
CultureInfo provider = CultureInfo.InvariantCulture;
Import import = new Import()
{
Month = month,
year = year,
EndDate = item.Key[1],
GenerateDate = DateTime.ParseExact(GenDate, "dd.MM.yyyy", provider),
StartDate = item.Key[0],
FileName = item.Value.ToString()
};
if (db.Imports.Any(i => i.StartDate == import.StartDate || i.EndDate == import.EndDate))
{
try
{
progressbar.ReleaseBar();
}
catch (Exception)
{
}
SAPbouiCOM.Framework.Application.SBO_Application.SetStatusBarMessage("ასეთი ფაილი უკვე შეიმპორტებულია", SAPbouiCOM.BoMessageTime.bmt_Short, true);
//cleanup
GC.Collect();
GC.WaitForPendingFinalizers();
//rule of thumb for releasing com objects:
// never use two dots, all COM objects must be referenced and released individually
// ex: [somthing].[something].[something] is bad
//release com objects to fully kill excel process from running in the background
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
//close and release
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
//quit and release
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
return;
}
for (int i = 8; i <= rowCount; i++)
{
Debug.WriteLine(i);
var bpCode = xlRange.Cells[i, 1].Value2.ToString();
if (bpCode == "რეპორტის დასასრული")
break;
i++;
Mediator mediator = new Mediator()
{
BPCode = bpCode,
Import = import,
};
for (int j = 0; j < clicks.Count(); j++)
{
progressbar.Up();
rowsc--;
var clickName = xlRange.Cells[i + j, 1].Value2.ToString();
Statistic SmeStatistic = new Statistic()
{
ClickNameID = clicks.FirstOrDefault(c => c.Name == clickName).ID,
ReqPerson = int.Parse(xlRange.Cells[i + j, 2].Value2.ToString()),
ReqBussiness = int.Parse(xlRange.Cells[i + j, 3].Value2.ToString()),
ResPerson = int.Parse(xlRange.Cells[i + j, 5].Value2.ToString()),
ResBussiness = int.Parse(xlRange.Cells[i + j, 6].Value2.ToString()),
Mediator = mediator,
};
//var xxx = clicks.FirstOrDefault(c => c.Name == clickName).ID;
db.Statistics.Add(SmeStatistic);
}
i += clicks.Count() - 1;
}
}
else
{
//int iReturnValue = SAPbouiCOM.Framework.Application.SBO_Application.MessageBox(item.Value + " ფაილის თარიღი არასწორია.", 1, "&Ok");
try
{
progressbar.ReleaseBar();
}
catch (Exception)
{
}
SAPbouiCOM.Framework.Application.SBO_Application.SetStatusBarMessage("ფაილის თარიღები არ ემთხვევა არჩეულ პერიოდს", SAPbouiCOM.BoMessageTime.bmt_Short, true);
//cleanup
GC.Collect();
GC.WaitForPendingFinalizers();
//rule of thumb for releasing com objects:
// never use two dots, all COM objects must be referenced and released individually
// ex: [somthing].[something].[something] is bad
//release com objects to fully kill excel process from running in the background
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
//close and release
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
//quit and release
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
return;
}
}
catch (Exception ex)
{
SAPbouiCOM.Framework.Application.SBO_Application.MessageBox(ex.Message);
}
finally
{
//cleanup
GC.Collect();
GC.WaitForPendingFinalizers();
//rule of thumb for releasing com objects:
// never use two dots, all COM objects must be referenced and released individually
// ex: [somthing].[something].[something] is bad
//release com objects to fully kill excel process from running in the background
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
//close and release //
//xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
//quit and release
//xlApp.Quit();
//Marshal.ReleaseComObject(xlApp);
}
}
オームズは* * ETLジョブには適していません:私はちょうどループのどこかで使用するデータテーブルを埋める - ここ
は、あなたの代わりにジェット機を使用する場合がありますどのようにおおよその目安です。このシナリオでは、何も提供しません。ビジネスエンティティやルールはなく、データストリームと変換ストリームだけです。 SqlBulkCopyを使用して、できるだけ早くデータをデータベースにインポートします。 Excel interopを使用すると非常に遅くなります。 Jet OLEDBプロバイダを使用してExcelファイルを読み込むか、Excelファイルを直接読み取ることができるEPPlusなどのライブラリを使用します。ジェットドライバーが最も速いですが、 –
ありがとうございます。ジェットドライバーが本当に助けてくれました。今では、各ファイルをインポートするのに1分しかかかりません! –
@ Mark Fitzgeraldクライアントがこのエラー「システムリソースを超過するまで、私は喜んでOLEDBを使用していました。 Excelファイルは約100kbと2.2k行です。同じファイルで、それは私のPCや私のオフィスの他のPC上でうまく動作します、アドバイス私何ですか? –