Created
June 19, 2015 22:04
-
-
Save gregwiechec/c3e0e586da237a4516f6 to your computer and use it in GitHub Desktop.
Custom Database Provider Factory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Data; | |
using System.Data.Common; | |
using System.Data.Sql; | |
using System.Data.SqlClient; | |
using System.Reflection; | |
using System.Security; | |
using System.Security.Permissions; | |
namespace CustomSqlProvider | |
{ | |
public class ConfigurableSqlClientFactory : DbProviderFactory | |
{ | |
public static readonly ConfigurableSqlClientFactory Instance = new ConfigurableSqlClientFactory(); | |
public override bool CanCreateDataSourceEnumerator | |
{ | |
get { return true; } | |
} | |
static ConfigurableSqlClientFactory() | |
{ | |
} | |
private ConfigurableSqlClientFactory() | |
{ | |
} | |
public override DbCommand CreateCommand() | |
{ | |
var sqlCommand = new SqlCommand {CommandTimeout = 500}; | |
return sqlCommand; | |
} | |
public override DbCommandBuilder CreateCommandBuilder() | |
{ | |
return new SqlCommandBuilder(); | |
} | |
public override DbConnection CreateConnection() | |
{ | |
return new ConfigurableSqlConnection(); | |
} | |
public override DbConnectionStringBuilder CreateConnectionStringBuilder() | |
{ | |
return new SqlConnectionStringBuilder(); | |
} | |
public override DbDataAdapter CreateDataAdapter() | |
{ | |
return new SqlDataAdapter(); | |
} | |
public override DbParameter CreateParameter() | |
{ | |
return new SqlParameter(); | |
} | |
public override CodeAccessPermission CreatePermission(PermissionState state) | |
{ | |
return new SqlClientPermission(state); | |
} | |
public override DbDataSourceEnumerator CreateDataSourceEnumerator() | |
{ | |
return SqlDataSourceEnumerator.Instance; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Data; | |
using System.Data.Common; | |
using System.Data.Sql; | |
using System.Data.SqlClient; | |
using System.Reflection; | |
using System.Security; | |
using System.Security.Permissions; | |
namespace CustomSqlProvider | |
{ | |
public class ConfigurableSqlConnection : DbConnection | |
{ | |
private readonly SqlConnection _connection; | |
public ConfigurableSqlConnection() | |
{ | |
this._connection = | |
(SqlConnection)DbProviderFactories.GetFactory("System.Data.SqlClient").CreateConnection(); | |
} | |
protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) | |
{ | |
return this._connection.BeginTransaction(); | |
} | |
public override void Close() | |
{ | |
this._connection.Close(); | |
} | |
public override void ChangeDatabase(string databaseName) | |
{ | |
this._connection.ChangeDatabase(databaseName); | |
} | |
public override void Open() | |
{ | |
this._connection.Open(); | |
} | |
public override string ConnectionString | |
{ | |
get { return this._connection.ConnectionString; } | |
set { this._connection.ConnectionString = value; } | |
} | |
public override string Database | |
{ | |
get { return this._connection.Database; } | |
} | |
public override ConnectionState State | |
{ | |
get { return this._connection.State; } | |
} | |
public override string DataSource | |
{ | |
get { return this._connection.DataSource; } | |
} | |
public override string ServerVersion | |
{ | |
get { return this._connection.ServerVersion; } | |
} | |
protected override DbCommand CreateDbCommand() | |
{ | |
var dynMethod = this._connection.GetType() | |
.GetMethod("CreateDbCommand", BindingFlags.NonPublic | BindingFlags.Instance); | |
var dbCommand = (SqlCommand)dynMethod.Invoke(this._connection, new object[] { }); | |
dbCommand.CommandTimeout = 500; | |
return dbCommand; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment