如何使用 RhinoETL 框架从 SQL 服务器 table 读取数据?
How to read from SQL Server table with RhinoETL framework?
RhinoETL 上的许多示例代码都谈到从 csv 文件加载并插入数据库。如何从 SQL 服务器数据库执行读取并将其行输出到控制台?您有执行简单的 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;
}
}
}
直接用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
方法不应被覆盖。一个SQLReader就这么简单:
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。
RhinoETL 上的许多示例代码都谈到从 csv 文件加载并插入数据库。如何从 SQL 服务器数据库执行读取并将其行输出到控制台?您有执行简单的 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;
}
}
}
直接用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
方法不应被覆盖。一个SQLReader就这么简单: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。