网络安全 频道

教你如何来进行编写通用的数据访问

但事实上,如果将检索、插入、更新和删除不同数据源(使用不同的数据提供程序)中的信息所需的各种操作封装在数据访问层中,并且只公开基本接口的成员,则可以实现第一级抽象-至少从数据提供程序的角度来看是这样。让我们看一看以下演示该设计思想的代码:

using System;

using System.Data;

using System.Data.Common;

using System.Data.SqlClient;

using System.Data.OleDb;

using System.Data.OracleClient;





namespace DAL

{

   public enum DatabaseType

   {

      Access,

      SQLServer,

      Oracle

      // 任何其他数据源类型

   }



   public enum ParameterType

   {

      Integer,

      Char,

      VarChar

      // 定义公用参数类型集

   }



   public class DataFactory

   {

      private DataFactory(){}



      public static IDbConnection CreateConnection

         (string ConnectionString, 

         DatabaseType dbtype)

      {

         IDbConnection cnn;



         switch(dbtype)

         {

            case DatabaseType.Access:

               cnn = new OleDbConnection

                  (ConnectionString); 

               break;

            case DatabaseType.SQLServer:

               cnn = new SqlConnection

                  (ConnectionString); 

               break;

            case DatabaseType.Oracle:

               cnn = new OracleConnection

                  (ConnectionString);

               break;

            default:

               cnn = new SqlConnection

                  (ConnectionString); 

               break;               

         }



         return cnn;

      }





      public static IDbCommand CreateCommand

         (string CommandText, DatabaseType dbtype,

         IDbConnection cnn)

      {

         IDbCommand cmd;

         switch(dbtype)

         {

            case DatabaseType.Access:

               cmd = new OleDbCommand

                  (CommandText,

                  (OleDbConnection)cnn);

               break;



            case DatabaseType.SQLServer:

               cmd = new SqlCommand

                  (CommandText,

                  (SqlConnection)cnn); 

               break;



            case DatabaseType.Oracle:

               cmd = new OracleCommand

                  (CommandText,

                  (OracleConnection)cnn);

               break;

            default:

               cmd = new SqlCommand

                  (CommandText,

                  (SqlConnection)cnn); 

               break;

         }



         return cmd;

      }





      public static DbDataAdapter CreateAdapter

         (IDbCommand cmd, DatabaseType dbtype)

      {

         DbDataAdapter da;

         switch(dbtype)

         {

            case DatabaseType.Access:

               da = new OleDbDataAdapter

                  ((OleDbCommand)cmd); 

               break;



            case DatabaseType.SQLServer:

               da = new SqlDataAdapter

                  ((SqlCommand)cmd); 

               break;



            case DatabaseType.Oracle:

               da = new OracleDataAdapter

                  ((OracleCommand)cmd); 

               break;



            default:

               da = new SqlDataAdapter

                  ((SqlCommand)cmd); 

               break;

         }



         return da;

      }

   }

}

该类的作用是向应用程序的较高级别隐藏与创建特定类型(来自特定的数据提供程序)的实例有关的细节,应用程序现在可以使用通过基本接口公开的一般行为与数据源进行交互。

让我们了解一下如何从应用程序的其他部分使用该类:








using System;

using System.Data;

using System.Data.Common;

using System.Configuration;    



namespace DAL

{

   public class CustomersData

   {

      public DataTable GetCustomers()

      {

         string ConnectionString = 

            ConfigurationSettings.AppSettings

            ["ConnectionString"];

         DatabaseType dbtype = 

            (DatabaseType)Enum.Parse

            (typeof(DatabaseType),

            ConfigurationSettings.AppSettings

            ["DatabaseType"]);



         IDbConnection cnn = 

            DataFactory.CreateConnection

            (ConnectionString,dbtype);



         string cmdString = "SELECT CustomerID" +

            ",CompanyName,ContactName FROM Customers";



         IDbCommand cmd = 

            DataFactory.CreateCommand(

            cmdString, dbtype,cnn);



            DbDataAdapter da = 

               DataFactory.CreateAdapter(cmd,dbtype); 



         DataTable dt = new DataTable("Customers");



         da.Fill(dt);



         return dt;

      }

        

      public CustomersDS GetCustomerOrders(string CustomerID)

      {

         // 待定

         return null;

      }

      public CustomersList GetCustomersByCountry

         (string CountryCode)

      {

         // 待定

         return null;

      }

      public bool InsertCustomer()

      {

         // 待定

         return false;

      }

   }

}
0
相关文章