pull/1/head
a.bozhenov 5 years ago
parent 2ddc814142
commit f9f1b6f34f

@ -20,6 +20,7 @@ namespace ZeroLevel.SqlServer
DbReader ExecuteReader(string query, DbParameter[] par);
void LazySelect(string query, DbParameter[] par, Func<DbDataReader, bool> readHandler);
void LazySelect(string query, DbParameter[] par, Func<DbDataReader, bool> readHandler, int timeout);
void LazySelectWithParameters(string query, IEnumerable<KeyValuePair<string, object>> par, Func<DbDataReader, bool> readHandler, int timeout);
T Read<T>(DbDataReader reader, int index);
T Read<T>(DbDataReader reader, string name);

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
namespace ZeroLevel.SqlServer
{
@ -282,6 +283,43 @@ namespace ZeroLevel.SqlServer
}
}
}
public void LazySelectWithParameters(string query, IEnumerable<KeyValuePair<string, object>> par, Func<DbDataReader, bool> readHandler, int timeout = Timeout)
{
using (var connection = _factory.CreateConnection())
{
using (var cmd = CreateCommand(connection, query, null, Timeout))
{
if (par != null && par.Any())
{
foreach (var p in par)
{
cmd.Parameters.AddWithValue(p.Key, p.Value);
}
}
using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
try
{
while (reader.Read())
{
if (false == readHandler(reader))
break;
}
}
catch (Exception ex)
{
Log.Error(ex, "Error executing query {0}.", cmd.CommandText);
}
finally
{
// Always call Close when done reading.
reader.Close();
}
}
}
}
}
#endregion
#region ExistsTable

Loading…
Cancel
Save

Powered by TurnKey Linux.