A generic data access component using factory pattern

xiaoxiao2021-03-06  89

using System; using System.Reflection; using System.Data; using System.Data.OleDb; using System.Data.SqlClient; namespace CSharpCorner.ProviderFactory {///

/// The collection of ADO.NET data providers that Are supported by . /// The OLE DB ( ) .NET DATA Provider.// OLEDB = 0, /// /// The SQL Server () .NET DATA provider./ // summary> //// providerfactory Class Abstracts ADO.NET RELATIONAL DATA Providers Through Creat Methods Which Return /// THE Underning interface./// /// /// this code was inspired by" Design An Effective Data-Access Architecture "by Dan Fox (.Netmagazine, Vol. 2, NO . Connection)}; private static Type [] _commandTypes = new Type [] {typeof (OleDbCommand), typeof (SqlCommand)}; private static Type [] _dataAdapterTypes = new Type [] {typeof (OleDbDataAdapter), typeof (SqlDataAdapter)}; private static Type [] _dataParameterTypes = new Type [] {typeof (OleDbParameter), typeof (SqlParameter)}; private ProviderType _provider; # endregion # region ctorsprivate ProviderFactory () {} // force user to specify providerpublic ProviderFactory (ProviderType provider) { _Provider = provider;

} # Endregion # region Provider propertypublic ProviderType Provider {get {return _provider;} set {_provider = value;}} # endregion # region IDbConnection methodspublic IDbConnection CreateConnection () {IDbConnection conn = null; try {conn = (IDbConnection) Activator.CreateInstance (_connectionTypes [(int) _provider]);} catch (TargetInvocationException e) {throw new SystemException (e.InnerException.Message, e.InnerException);} return conn;} public IDbConnection CreateConnection (string connectionString) {IDbConnection conn = null; object [] args = {connectionString}; try {conn = (IDbConnection) Activator.CreateInstance (_connectionTypes [(int) _provider], args);} catch (TargetInvocationException e) {throw new SystemException (e.InnerException.Message, e. InnerException);} return conn;} # endregion # region IDbCommand methodspublic IDbCommand CreateCommand () {IDbCommand cmd = null; try {cmd = (IDbCommand) Activator.CreateInstance (_commandTypes [(int) _provider]);} catch (TargetInvocationException e) {throw new systemException (EI nnerException.Message, e.InnerException);} return cmd;} public IDbCommand CreateCommand (string cmdText) {IDbCommand cmd = null; object [] args = {cmdText}; try {cmd = (IDbCommand) Activator.CreateInstance (_commandTypes [( int) _provider], args);} catch (TargetInvocationException e) {throw new SystemException (e.InnerException.Message, e.InnerException);} return cmd;} public IDbCommand CreateCommand (string cmdText, IDbConnection connection) {IDbCommand cmd = null ; object [] args = {cmdText, connection}; try {cmd = (IDbCommand) Activator.CreateInstance (_commandTypes [(int) _provider], args);} catch (TargetInvocationException e) {throw new SystemException (e.InnerException.Message , e.innerexception;} Return CMD;

} Public IDbCommand CreateCommand (string cmdText, IDbConnection connection, IDbTransaction transaction) {IDbCommand cmd = null; object [] args = {cmdText, connection, transaction}; try {cmd = (IDbCommand) Activator.CreateInstance (_commandTypes [(int) _provider ], args);} catch (TargetInvocationException e) {throw new SystemException (e.InnerException.Message, e.InnerException);} return cmd;} # endregion # region IDbDataAdapter methodspublic IDbDataAdapter CreateDataAdapter () {IDbDataAdapter da = null; try { da = (IDbDataAdapter) Activator.CreateInstance (_dataAdapterTypes [(int) _provider]);} catch (TargetInvocationException e) {throw new SystemException (e.InnerException.Message, e.InnerException);} return da;} public IDbDataAdapter CreateDataAdapter (IDbCommand selectCommand) {IDbDataAdapter da = null; object [] args = {selectCommand}; try {da = (IDbDataAdapter) Activator.CreateInstance (_dataAdapterTypes [(int) _provider], args);} catch (TargetInvocationException e) {throw new SystemException ( E.Nnerexception.Message, e.InnerException);} return da;} public IDbDataAdapter CreateDataAdapter (string selectCommandText, IDbConnection selectConnection) {IDbDataAdapter da = null; object [] args = {selectCommandText, selectConnection}; try {da = (IDbDataAdapter) Activator.CreateInstance (_dataAdapterTypes [ (int) _provider], args);} catch (TargetInvocationException e) {throw new SystemException (e.InnerException.Message, e.InnerException);} return da;} public IDbDataAdapter CreateDataAdapter (string selectCommandText, string selectConnectionString) {IDbDataAdapter da = Null; object [] args = {selectcommandtext, selectconnectionstring}; try {da = (idbdataadapter) activator.createInstance (_Dataadaptertypes [(int) _provider], args;

转载请注明原文地址:https://www.9cbs.com/read-102229.html

New Post(0)