RhinoETL上的许多示例代码都谈到从csv文件加载并插入数据库。如何执行从SQL Server数据库的读取并将其行输出到控制台?您是否有示例代码来执行简单的dbread和批处理读取?
author's blog显示了一些示例代码,但是它似乎不完整,或者我遗漏了一些东西。
我的无效代码
public class ReadBuildInfos : ConventionInputCommandOperation
{
public ReadBuildInfos(ConnectionStringSettings csSettings) : base(csSettings)
{
Command = "SELECT Key, Value FROM dbo.BuildInfos WITH (NOLOCK);";
}
public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
{
foreach (Row row in rows)
{
yield return row;
}
}
}
答案 0 :(得分:1)
直接用Rhino.Etl调试后,发现我犯了2个错误:
ConnectionStringSettings
应该包含providerName
。 RhinoETL对此提出了例外,但我不知何故
var csString = new ConnectionStringSettings("myConnection2",
"Server=yourdatabasehere.database.windows.net;Database=MyDbName;User ID=youruser;Password=yourpassword",
"System.Data.SqlClient"); //I was missing this.
Register(new ReadBuildInfosConvention(csString));
作者的博客已完成。 Execute
方法不应被覆盖。 SQL Reader如下所示:
public class ReadBuildInfosConvention : ConventionInputCommandOperation
{
public ReadBuildInfosConvention(ConnectionStringSettings csSettings) : base(csSettings)
{
Command = "SELECT [Key], [Value] FROM BuildInfos WITH (NOLOCK);";
}
}
因为基本的Execute
方法已经可以正确处理数据库读取
public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
{
using (IDbConnection connection = Use.Connection(ConnectionStringSettings))
using (IDbTransaction transaction = BeginTransaction(connection))
{
using (currentCommand = connection.CreateCommand())
{
currentCommand.Transaction = transaction;
PrepareCommand(currentCommand);
using (IDataReader reader = currentCommand.ExecuteReader())
{
while (reader.Read())
{
yield return CreateRowFromReader(reader);
}
}
}
if (transaction != null) transaction.Commit();
}
}
这里是工作中的VS solution with logging。