Skip to content

Instantly share code, notes, and snippets.

@BryanWilhite
Created September 23, 2015 23:15
Show Gist options
  • Save BryanWilhite/b8d9d0155938644c9e16 to your computer and use it in GitHub Desktop.
Save BryanWilhite/b8d9d0155938644c9e16 to your computer and use it in GitHub Desktop.
OracleManagedDataSourceExtensions
public static class OracleManagedDataSourceExtensions
{
public static string ToConnectionString(this OracleManagedDataSource source, string userName, string password)
{
var connectionStringFormat = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL={0})(HOST={1})(PORT={2}))(CONNECT_DATA=(SERVICE_NAME={3})));User Id={4};Password={5};";
return string.Format(connectionStringFormat,
source.Protocol,
source.ServerName,
source.Port,
source.ServiceName,
userName, password);
}
public static OracleCommand ToOracleCommand(this OracleConnection oracleConnection, string commandText)
{
if (oracleConnection == null) throw new ArgumentNullException("oracleConnection", "The expected Oracle connection is not here.");
return new OracleCommand(commandText, oracleConnection);
}
public static OracleDataReader ToOracleDataReader(this OracleConnection oracleConnection, string sql, Dictionary<string, object> parameterCollection)
{
if (oracleConnection == null) throw new ArgumentNullException("oracleConnection", "The expected Oracle connection is not here.");
var oracleCommand = oracleConnection
.ToOracleCommand(sql)
.WithNamedParameters(parameterCollection);
var reader = oracleCommand.ExecuteReader(CommandBehavior.Default);
return reader;
}
public static OracleCommand WithNamedParameters(this OracleCommand oracleCommand, Dictionary<string, object> parameterCollection)
{
if (oracleCommand == null) throw new ArgumentNullException("oracleCommand", "The expected Oracle command is not here.");
parameterCollection.ForEachInEnumerable(i => oracleCommand.Parameters.Add(i.Key, i.Value));
oracleCommand.BindByName = true;
return oracleCommand;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment