Freitag, 28. August 2009

Globale Methoden für Datenbankzugriffe

Es wäre doch nett wenn man sich ab und zu das Leben doch ein bisschen leichter machen könnte und nicht immer Zugriffscode neu schreiben müsste. Wie wäre es also mit einer globalen Klasse die genau das macht aber nicht unbedingt mit großen Performanceeinbüssen verbunden ist.

Also, zuerst dynamisch die Parameter der Stored Procedures auslesen.Dabei wird die ermittelte ParameterCollection im Applicationscache gespeichert und zuküftig auch von dort immer geholt.

   1:  private static void DiscoverParameters(SqlCommand command, string spName)
   2:  {
   3:       //CacheKey
   4:       string CacheKey = string.Concat("SP_", spName);
   5:   
   6:       //Check if SP is in Cache
   7:       if (System.Web.HttpContext.Current.Cache[CacheKey] == null)
   8:       {
   9:            SqlCommandBuilder.DeriveParameters(command);
  10:            SqlParameterCollection paramCollection = command.Parameters;
  11:   
  12:            //SqlParameter Array
  13:            SqlParameter[] sqlParam = new SqlParameter[paramCollection.Count];
  14:            paramCollection.CopyTo(sqlParam, 0);
  15:   
  16:            System.Web.HttpRuntime.Cache[CacheKey] = sqlParam;
  17:       }
  18:       else
  19:       {
  20:            //Read from Cache 
  21:            SqlParameter[] sqlParam = (SqlParameter[])
  22:                 CacheHelperWeb.GetApplicationCacheElement(CacheKey);
  23:   
  24:            //Add to Command 
  25:            for (int i = 0; i < sqlParam.Length; i++)
  26:            {
  27:                 SqlParameter item = (SqlParameter)
  28:                      ((ICloneable)sqlParam[i]).Clone();
  29:                 command.Parameters.Add(item);
  30:            }
  31:  }
  32:  }


Dann mit Werten befüllen. Es wird eine for Schleife verwendet da bei Collections sich etwas schneller als eine foreach Schleife verhält.

   1:  private static void FillParametersValues(SqlCommand command,
   2:    params object[] parameterValues)
   3:  {
   4:       int index = 0;
   5:       int count=command.Parameters.Count;
   6:   
   7:       for (int i=0;i<count;i++) 
   8:       {
   9:            if ((command.Parameters[i].Direction == ParameterDirection.Input)||
  10:              (command.Parameters[i].Direction == ParameterDirection.InputOutput))
  11:            {
  12:                 command.Parameters[i].Value = parameterValues[index];
  13:                 index++;
  14:            }
  15:       }
  16:  }

und die Zugriffsmethode:

   1:  public static void ExecuteNonQueryIntern(string storedProcedureName,
   2:  params object[] parameterValue)
   3:  {
   4:   
   5:       using (SqlConnection connection = new SqlConnection(CONNECTIONSTRING))
   6:       {
   7:   
   8:           using (SqlCommand command = new SqlCommand
   9:             (storedProcedureName,connection))
  10:           {
  11:                command.CommandType = CommandType.StoredProcedure;
  12:   
  13:                DiscoverParameters(command, storedProcedureName);
  14:                FillParametersValues(command, parameterValue);
  15:   
  16:   
  17:                command.ExecuteNonQuery();
  18:                command.Parameters.Clear();
  19:           }
  20:       }
  21:  }

Jetzt nur noch Errorhandling und Logging einbauen und fertig. Analog kann das ganze auch für ExecuteScalar oder ExecuteReader angewandt werden. War doch ganz einfach oder?

Hier noch ein Zugriffsbeispiel:
ExecuteNonQueryIntern("SP_UpdateData",1893,"Max","Mustermann");

Keine Kommentare:

Kommentar veröffentlichen