【问题标题】:Rhino ETL - loading large pipe-delimited filesRhino ETL - 加载大型管道分隔文件
【发布时间】:2012-07-18 10:25:03
【问题描述】:
我们必须加载大型管道分隔文件。使用 Rhino ETL(依赖 FileHelpers)将这些加载到 SQL Server DB 时,是否必须提供记录类?
我们必须将文件加载到有几十列的不同表中——生成它们可能需要一整天。我想我们可以编写一个小工具来从 SQL Server 表中生成记录类。
另一种方法是为 FileStream 编写 IDataReader 包装器并将其传递给 SqlBulkCopy。
SqlBulkCopy 也需要列映射,但它确实允许列序号 - 这很简单。
有什么想法/建议吗?
谢谢。
【问题讨论】:
标签:
c#
sql-server
sqlbulkcopy
filehelpers
rhino-etl
【解决方案1】:
我对 Rhino ETL 了解不多,但 FileHelpers 有一个ClassBuilder,它允许您在运行时生成记录类。有关示例,请参阅the documentation。
因此很容易生成具有以下内容的类:
SqlCommand command = new SqlCommand("SELECT TOP 1 * FROM Customers;", connection);
connection.Open();
// get the schema for the customers table
SqlDataReader reader = command.ExecuteReader();
DataTable schemaTable = reader.GetSchemaTable();
// create the FileHelpers record class
// alternatively there is a 'FixedClassBuilder'
DelimitedClassBuilder cb = new DelimitedClassBuilder("Customers", ",");
cb.IgnoreFirstLines = 1;
cb.IgnoreEmptyLines = true;
// populate the fields based on the columns
foreach (DataRow row in schemaTable.Rows)
{
cb.AddField(row.Field<string>("ColumnName"), row.Field<Type>("DataType"));
cb.LastField.TrimMode = TrimMode.Both;
}
// load the dynamically created class into a FileHelpers engine
FileHelperEngine engine = new FileHelperEngine(cb.CreateRecordClass());
// import your records
DataTable dt = engine.ReadFileAsDT("testCustomers.txt");