ProviderFactory : indipendenza dal database.

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Text;
   4:  
   5: using System.Configuration;
   6: using System.Data;
   7: using System.Data.Common;
   8: using System.Windows.Forms;
   9:  
  10: namespace GenericDbProvider
  11: {
  12:     public class DbProvider
  13:     {
  14:         private string connectionString,providerName;
  15:         private DbProviderFactory dbProviderFactory;
  16:  
  17:         public DbProvider()
  18:         {
  19:             try
  20:             {
  21:                 providerName = Convert.ToString(ConfigurationManager.ConnectionStrings["DBCONN"].ProviderName);
  22:                 connectionString = ConfigurationManager.ConnectionStrings["DBCONN"].ConnectionString;
  23:             }
  24:             catch(Exception ex)
  25:             {
  26:                 MessageBox.Show(ex.Message);
  27:             }            
  28:         }
  29:  
  30:         public DataTable ExecuteQuery()
  31:         {
  32:             dbProviderFactory = DbProviderFactories.GetFactory(providerName);
  33:             DataTable dt = new DataTable();
  34:  
  35:             using (DbConnection dbConnection = dbProviderFactory.CreateConnection())
  36:             {
  37:                 dbConnection.ConnectionString = connectionString;
  38:                 try
  39:                 {
  40:                     dbConnection.Open();
  41:  
  42:                     using (DbDataAdapter adatpter = dbProviderFactory.CreateDataAdapter())
  43:                     {
  44:                         DbCommand select = dbProviderFactory.CreateCommand();
  45:                         select.CommandText = "SELECT * FROM USERTABLE";
  46:                         select.Connection = dbConnection;
  47:                         adatpter.SelectCommand = select;
  48:                         adatpter.Fill(dt);
  49:                     }
  50:                 }
  51:                 catch (Exception ex)
  52:                 {
  53:                     MessageBox.Show(ex.Message);
  54:                     if(ex.InnerException != null)
  55:                         MessageBox.Show(ex.InnerException.Message);                      
  56:                     
  57:                 }
  58:             }
  59:             return dt; 
  60:         }
  61:     }
  62: }




No Comments


You can leave the first : )