pull/1/head
a.bozhenov 5 years ago
parent 61163cf910
commit 1c3328173f

@ -18,5 +18,6 @@ namespace ZeroLevel.SqlServer
int ExecuteNonResult(string query); int ExecuteNonResult(string query);
int ExecuteNonResult(string query, DbParameter[] par); int ExecuteNonResult(string query, DbParameter[] par);
void LazySelect(string query, DbParameter[] par, Func<DbDataReader, bool> readHandler, int timeout); 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);
} }
} }

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.Common; using System.Data.Common;
using System.Data.SqlClient; using System.Data.SqlClient;
using System.Linq;
namespace ZeroLevel.SqlServer namespace ZeroLevel.SqlServer
{ {
@ -215,6 +216,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 #endregion
#region ExistsTable #region ExistsTable

Loading…
Cancel
Save

Powered by TurnKey Linux.