기본적으로 Excel 파일에 많은 데이터를 삽입해야합니다. OleDB 연결을 만드는 것이 가장 빠른 방법 인 것처럼 보이지만 메모리 문제가있는 것으로 나타났습니다. INSERT 쿼리를 실행하면 프로세스에서 사용하는 메모리가 계속 증가하는 것처럼 보입니다. Excel 파일로 출력 할 때만 메모리를 축소했습니다 (메모리가 Excel에 출력되지 않고 안정적으로 유지됩니다). 나는 닫고 각 워크 시트 사이의 연결을 다시 열지 만 이것은 Dispose()와 마찬가지로 메모리 사용에 영향을 미치지 않습니다. 상대적으로 작은 데이터 세트로 확인할 수 있기 때문에 데이터가 성공적으로 기록됩니다. 누군가 통찰력을 가지고 있다면, 그것은 인정 될 것입니다.ADO.NET + 방대한 INSERT + Excel + C# = "나쁜 시간"입니까?
initializeADOConn는()이 생성자
initADOConnInsertComm (에서 호출)는 삽입 매개 변수 삽입 쿼리를
writeRecord을() 새로운 레코드가 기록 될 때마다가 호출 만듭니다. 필요에 따라 새 워크 시트가 만들어집니다.
public bool initializeADOConn()
{
/* Set up the connection string and connect.*/
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + this.destination + ";Extended Properties=\"Excel 8.0;HDR=YES;\"";
//DbProviderFactory factory =
//DbProviderFactories.GetFactory("System.Data.OleDb");
conn = new OleDbConnection(connectionString);
conn.ConnectionString = connectionString;
conn.Open();
/* Intialize the insert command. */
initADOConnInsertComm();
return true;
}
public override bool writeRecord(FileListerFileInfo file)
{
/* If all available sheets are full, make a new one. */
if (numWritten % EXCEL_MAX_ROWS == 0)
{
conn.Close();
conn.Open();
createNextSheet();
}
/* Count this record as written. */
numWritten++;
/* Get all of the properties of the FileListerFileInfo record and add
* them to the parameters of the insert query. */
PropertyInfo[] properties = typeof(FileListerFileInfo).GetProperties();
for (int i = 0; i < insertComm.Parameters.Count; i++)
insertComm.Parameters[i].Value = properties[i].GetValue(file, null);
/* Add the record. */
insertComm.ExecuteNonQuery();
return true;
}
편집 :
는아니, 난 전혀 Excel을 사용하지 마십시오. Interop.Excel의 성능 저하로 인해 의도적으로 Interop.Excel을 피할 수 있습니다.
이 응답을 읽은 후 (그리고 내 상사를 연결하는 데 약 12 가지 이유가있는 이유는 무엇입니까?), 나는 경영진이 그 질문에 의존하지 않을 것이라고 확신했습니다. 이미 출력 옵션 배열이 있으므로 중요하지 않습니다. – llamaoo7