pull/1/head
a.bozhenov 5 years ago
parent f43e2dca93
commit a064b5972d

@ -62,15 +62,22 @@ namespace ZeroLevel.SqlServer
}
dbConnectionString = new SqlConnectionStringBuilder(_currentConnectionString);
dbConnectionString.Pooling = true;
dbConnectionString.MaxPoolSize = 50;
}
public SqlConnection CreateConnection()
{
try
{
var connection = new SqlConnection(ConnectionString);
connection.Open();
return connection;
}
catch (Exception ex)
{
Log.Error(ex, $"[SqlDbConnectionFactory.CreateConnection] {ConnectionString}");
throw;
}
}
#region Helpers

@ -7,7 +7,7 @@ using System.Data.SqlClient;
namespace ZeroLevel.SqlServer
{
public class DbReader
:IDisposable
: IDisposable
{
private readonly DbConnection _connection;
private readonly IDbCommand _command;
@ -76,6 +76,7 @@ namespace ZeroLevel.SqlServer
cmd.ExecuteNonQuery();
}
}
connection.Close();
}
}
@ -87,12 +88,19 @@ namespace ZeroLevel.SqlServer
public int ExecuteNonResult(string query, DbParameter[] par)
{
using (DbConnection connection = _factory.CreateConnection())
{
try
{
using (var cmd = CreateCommand(connection, query, par, Timeout))
{
return cmd.ExecuteNonQuery();
}
}
finally
{
connection.Close();
}
}
}
public T Insert<T>(string insert_query, SqlParameter[] par)
@ -108,6 +116,7 @@ namespace ZeroLevel.SqlServer
}
finally
{
connection.Close();
connection.Dispose();
}
}
@ -148,6 +157,7 @@ namespace ZeroLevel.SqlServer
da.Fill(ds);
}
}
connection.Close();
}
return ds;
}
@ -164,6 +174,7 @@ namespace ZeroLevel.SqlServer
da.Fill(ds);
}
}
connection.Close();
}
return ds;
}
@ -173,23 +184,37 @@ namespace ZeroLevel.SqlServer
public object ExecuteScalar(string query)
{
using (var connection = _factory.CreateConnection())
{
try
{
using (var cmd = CreateCommand(connection, query, null, Timeout))
{
return cmd.ExecuteScalar();
}
}
finally
{
connection.Close();
}
}
}
public object ExecuteScalar(string query, DbParameter[] par)
{
using (var connection = _factory.CreateConnection())
{
try
{
using (var cmd = CreateCommand(connection, query, par, Timeout))
{
return cmd.ExecuteScalar();
}
}
finally
{
connection.Close();
}
}
}
#endregion
@ -197,6 +222,8 @@ namespace ZeroLevel.SqlServer
public int ExecProcedure(string name)
{
using (var connection = _factory.CreateConnection())
{
try
{
using (var command = new SqlCommand(name, connection)
{
@ -207,6 +234,11 @@ namespace ZeroLevel.SqlServer
return command.ExecuteNonQuery();
}
}
finally
{
connection.Close();
}
}
}
#endregion
@ -281,7 +313,7 @@ namespace ZeroLevel.SqlServer
return new SqlParameter[0];
}
private static SqlCommand CreateCommand(DbConnection connection, string query, DbParameter[] parameters, int timeout)
public static SqlCommand CreateCommand(DbConnection connection, string query, DbParameter[] parameters, int timeout)
{
var command = connection.CreateCommand();
command.CommandText = query;

Loading…
Cancel
Save

Powered by TurnKey Linux.