Advertisement
Guest User

datamanager

a guest
Mar 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 KB | None | 0 0
  1.     public class DataManager : IDataManager
  2.     {
  3.         public DbDataAdapter DataAdapter { get; set; }
  4.         public DbConnection Connection { get; set; }
  5.         public DbProviderFactory Factory { get; set; }
  6.  
  7.         public DataManager(string providerName)
  8.         {
  9.             var connectionString = GetConnectionStringByProvider(providerName);
  10.             CreateDbConnection(providerName, connectionString);
  11.         }
  12.  
  13.         public string GetConnectionStringByProvider(string providerName)
  14.         {
  15.             // Return null on failure.
  16.             string returnValue = null;
  17.  
  18.             // Get the collection of connection strings.
  19.             ConnectionStringSettingsCollection settings = ConfigurationManager.ConnectionStrings;
  20.  
  21.             // Walk through the collection and return the first
  22.             // connection string matching the providerName.
  23.             if (settings != null)
  24.             {
  25.                 foreach (ConnectionStringSettings cs in settings)
  26.                 {
  27.                     if (Equals(cs.ProviderName.ToLower(), providerName.ToLower()))
  28.                     {
  29.                         returnValue = cs.ConnectionString;
  30.                         break;
  31.                     }
  32.                 }
  33.             }
  34.  
  35.             return returnValue;
  36.         }
  37.  
  38.         public void CreateDbConnection(string providerName, string connectionString)
  39.         {
  40.             // Create the DbProviderFactory and DbConnection.
  41.             if (connectionString != null)
  42.             {
  43.                 try
  44.                 {
  45.                     var dbpf = DbProviderFactories.GetFactory(providerName);
  46.  
  47.                     this.Connection = dbpf.CreateConnection();
  48.                     this.Connection.ConnectionString = connectionString;
  49.                     this.DataAdapter = dbpf.CreateDataAdapter(); // returns 0
  50.                     this.Factory = dbpf;
  51.                 }
  52.                 catch (Exception ex)
  53.                 {
  54.                     Console.WriteLine(ex.Message);
  55.                 }
  56.             }
  57.         }
  58.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement