Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DataManager : IDataManager
- {
- public DbDataAdapter DataAdapter { get; set; }
- public DbConnection Connection { get; set; }
- public DbProviderFactory Factory { get; set; }
- public DataManager(string providerName)
- {
- var connectionString = GetConnectionStringByProvider(providerName);
- CreateDbConnection(providerName, connectionString);
- }
- public string GetConnectionStringByProvider(string providerName)
- {
- // Return null on failure.
- string returnValue = null;
- // Get the collection of connection strings.
- ConnectionStringSettingsCollection settings = ConfigurationManager.ConnectionStrings;
- // Walk through the collection and return the first
- // connection string matching the providerName.
- if (settings != null)
- {
- foreach (ConnectionStringSettings cs in settings)
- {
- if (Equals(cs.ProviderName.ToLower(), providerName.ToLower()))
- {
- returnValue = cs.ConnectionString;
- break;
- }
- }
- }
- return returnValue;
- }
- public void CreateDbConnection(string providerName, string connectionString)
- {
- // Create the DbProviderFactory and DbConnection.
- if (connectionString != null)
- {
- try
- {
- var dbpf = DbProviderFactories.GetFactory(providerName);
- this.Connection = dbpf.CreateConnection();
- this.Connection.ConnectionString = connectionString;
- this.DataAdapter = dbpf.CreateDataAdapter(); // returns 0
- this.Factory = dbpf;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement