Rhino ETL - 加载大型管道分隔文件

时间:2012-07-18 10:25:03

标签: c# sql-server sqlbulkcopy filehelpers rhino-etl

我们必须加载大型管道分隔文件。使用Rhino ETL(依赖FileHelpers)将这些加载到SQL Server数据库中时,是否必须提供记录类? 我们必须将文件加载到具有数十列的不同表中 - 我们可能需要一整天才能生成它们。我想我们可以编写一个小工具来从SQL Server表中生成记录类。

另一种方法是为FileStream编写IDataReader包装器并将其传递给SqlBulkCopy。

SqlBulkCopy确实需要列映射,但它确实允许列序数 - 这很容易。

有任何想法/建议吗?

感谢。

1 个答案:

答案 0 :(得分: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");