I just read Mahesh's article Writing a Generic Data Access Component.

Another way to solve this problem is to utilize the System.Activator class and a factory pattern to create the concrete provider classes as was pointed-out in Dan Fox's article "Design an Effective Data-Access Architecture" (.netmagazine, vol. 2, no. 7). I took this idea and refined it a bit so that the only steps necessary to add a new provider to the factory is to add a new enum and the associated type values to the factory's static type arrays (a total of 5 lines of code).

Here is the sample code. See the attached source code for a test application.

  1. using System;
  2. using System.Reflection;
  3. using System.Data;
  4. using System.Data.OleDb;
  5. using System.Data.SqlClient;
  6. namespace CSharpCorner.ProviderFactory
  7. {
  8. /// <summary>
  9. /// The collection of ADO.NET data providers that are supported by <see cref="ProviderFactory"/>.
  10. /// </summary>
  11. public enum ProviderType
  12. {
  13. /// <summary>
  14. /// The OLE DB (<see cref="System.Data.OleDb"/>) .NET data provider.
  15. /// </summary>
  16. OleDb = 0,
  17. /// <summary>
  18. /// The SQL Server (<see cref="System.Data.SqlClient"/>) .NET data provider.
  19. /// </summary>
  20. SqlClient
  21. };
  22. /// <summary>
  23. /// The <b>ProviderFactory</b> class abstracts ADO.NET relational data providers through creator methods which return
  24. /// the underlying <see cref="System.Data"/> interface.
  25. /// </summary>
  26. /// <remarks>
  27. /// This code was inspired by "Design an Effective Data-Access Architecture" by Dan Fox (.netmagazine, vol. 2, no. 7)
  28. /// </remarks>
  29. public class ProviderFactory
  30. {
  31. #region private variables
  32. private static Type[] _connectionTypes = new Type[] { typeof(OleDbConnection), typeof(SqlConnection) };
  33. private static Type[] _commandTypes = new Type[] { typeof(OleDbCommand), typeof(SqlCommand) };
  34. private static Type[] _dataAdapterTypes = new Type[] { typeof(OleDbDataAdapter), typeof(SqlDataAdapter) };
  35. private static Type[] _dataParameterTypes = new Type[] { typeof(OleDbParameter), typeof(SqlParameter) };
  36. private ProviderType _provider;
  37. #endregion
  38. #region ctors
  39. private ProviderFactory() { } // force user to specify provider
  40. public ProviderFactory(ProviderType provider)
  41. {
  42. _provider = provider;
  43. }
  44. #endregion
  45. #region Provider property
  46. public ProviderType Provider
  47. {
  48. get
  49. {
  50. return _provider;
  51. }
  52. set
  53. {
  54. _provider = value;
  55. }
  56. }
  57. #endregion
  58. #region IDbConnection methods
  59. public IDbConnection CreateConnection()
  60. {
  61. IDbConnection conn = null;
  62. try
  63. {
  64. conn = (IDbConnection)Activator.CreateInstance(_connectionTypes[(int)_provider]);
  65. }
  66. catch (TargetInvocationException e)
  67. {
  68. throw new SystemException(e.InnerException.Message, e.InnerException);
  69. }
  70. return conn;
  71. }
  72. public IDbConnection CreateConnection(string connectionString)
  73. {
  74. IDbConnection conn = null;
  75. object[] args = { connectionString };
  76. try
  77. {
  78. conn = (IDbConnection)Activator.CreateInstance(_connectionTypes[(int)_provider], args);
  79. }
  80. catch (TargetInvocationException e)
  81. {
  82. throw new SystemException(e.InnerException.Message, e.InnerException);
  83. }
  84. return conn;
  85. }
  86. #endregion
  87. #region IDbCommand methods
  88. public IDbCommand CreateCommand()
  89. {
  90. IDbCommand cmd = null;
  91. try
  92. {
  93. cmd = (IDbCommand)Activator.CreateInstance(_commandTypes[(int)_provider]);
  94. }
  95. catch (TargetInvocationException e)
  96. {
  97. throw new SystemException(e.InnerException.Message, e.InnerException);
  98. }
  99. return cmd;
  100. }
  101. public IDbCommand CreateCommand(string cmdText)
  102. {
  103. IDbCommand cmd = null;
  104. object[] args = { cmdText };
  105. try
  106. {
  107. cmd = (IDbCommand)Activator.CreateInstance(_commandTypes[(int)_provider], args);
  108. }
  109. catch (TargetInvocationException e)
  110. {
  111. throw new SystemException(e.InnerException.Message, e.InnerException);
  112. }
  113. return cmd;
  114. }
  115. public IDbCommand CreateCommand(string cmdText, IDbConnection connection)
  116. {
  117. IDbCommand cmd = null;
  118. object[] args = { cmdText, connection };
  119. try
  120. {
  121. cmd = (IDbCommand)Activator.CreateInstance(_commandTypes[(int)_provider], args);
  122. }
  123. catch (TargetInvocationException e)
  124. {
  125. throw new SystemException(e.InnerException.Message, e.InnerException);
  126. }
  127. return cmd;
  128. }
  129. public IDbCommand CreateCommand(string cmdText, IDbConnection connection, IDbTransaction transaction)
  130. {
  131. IDbCommand cmd = null;
  132. object[] args = { cmdText, connection, transaction };
  133. try
  134. {
  135. cmd = (IDbCommand)Activator.CreateInstance(_commandTypes[(int)_provider], args);
  136. }
  137. catch (TargetInvocationException e)
  138. {
  139. throw new SystemException(e.InnerException.Message, e.InnerException);
  140. }
  141. return cmd;
  142. }
  143. #endregion
  144. #region IDbDataAdapter methods
  145. public IDbDataAdapter CreateDataAdapter()
  146. {
  147. IDbDataAdapter da = null;
  148. try
  149. {
  150. da = (IDbDataAdapter)Activator.CreateInstance(_dataAdapterTypes[(int)_provider]);
  151. }
  152. catch (TargetInvocationException e)
  153. {
  154. throw new SystemException(e.InnerException.Message, e.InnerException);
  155. }
  156. return da;
  157. }
  158. public IDbDataAdapter CreateDataAdapter(IDbCommand selectCommand)
  159. {
  160. IDbDataAdapter da = null;
  161. object[] args = { selectCommand };
  162. try
  163. {
  164. da = (IDbDataAdapter)Activator.CreateInstance(_dataAdapterTypes[(int)_provider], args);
  165. }
  166. catch (TargetInvocationException e)
  167. {
  168. throw new SystemException(e.InnerException.Message, e.InnerException);
  169. }
  170. return da;
  171. }
  172. public IDbDataAdapter CreateDataAdapter(string selectCommandText, IDbConnection selectConnection)
  173. {
  174. IDbDataAdapter da = null;
  175. object[] args = { selectCommandText, selectConnection };
  176. try
  177. {
  178. da = (IDbDataAdapter)Activator.CreateInstance(_dataAdapterTypes[(int)_provider], args);
  179. }
  180. catch (TargetInvocationException e)
  181. {
  182. throw new SystemException(e.InnerException.Message, e.InnerException);
  183. }
  184. return da;
  185. }
  186. public IDbDataAdapter CreateDataAdapter(string selectCommandText, string selectConnectionString)
  187. {
  188. IDbDataAdapter da = null;
  189. object[] args = { selectCommandText, selectConnectionString };
  190. try
  191. {
  192. da = (IDbDataAdapter)Activator.CreateInstance(_dataAdapterTypes[(int)_provider], args);
  193. }
  194. catch (TargetInvocationException e)
  195. {
  196. throw new SystemException(e.InnerException.Message, e.InnerException);
  197. }
  198. return da;
  199. }
  200. #endregion
  201. #region IDbDataParameter methods
  202. public IDbDataParameter CreateDataParameter()
  203. {
  204. IDbDataParameter param = null;
  205. try
  206. {
  207. param = (IDbDataParameter)Activator.CreateInstance(_dataParameterTypes[(int)_provider]);
  208. }
  209. catch (TargetInvocationException e)
  210. {
  211. throw new SystemException(e.InnerException.Message, e.InnerException);
  212. }
  213. return param;
  214. }
  215. public IDbDataParameter CreateDataParameter(string parameterName, object value)
  216. {
  217. IDbDataParameter param = null;
  218. object[] args = { parameterName, value };
  219. try
  220. {
  221. param = (IDbDataParameter)Activator.CreateInstance(_dataParameterTypes[(int)_provider], args);
  222. }
  223. catch (TargetInvocationException e)
  224. {
  225. throw new SystemException(e.InnerException.Message, e.InnerException);
  226. }
  227. return param;
  228. }
  229. public IDbDataParameter CreateDataParameter(string parameterName, DbType dataType)
  230. {
  231. IDbDataParameter param = CreateDataParameter();
  232. if (param != null)
  233. {
  234. param.ParameterName = parameterName;
  235. param.DbType = dataType;
  236. }
  237. return param;
  238. }
  239. public IDbDataParameter CreateDataParameter(string parameterName, DbType dataType, int size)
  240. {
  241. IDbDataParameter param = CreateDataParameter();
  242. if (param != null)
  243. {
  244. param.ParameterName = parameterName;
  245. param.DbType = dataType;
  246. param.Size = size;
  247. }
  248. return param;
  249. }
  250. public IDbDataParameter CreateDataParameter(string parameterName, DbType dataType, int size, string sourceColumn)
  251. {
  252. IDbDataParameter param = CreateDataParameter();
  253. if (param != null)
  254. {
  255. param.ParameterName = parameterName;
  256. param.DbType = dataType;
  257. param.Size = size;
  258. param.SourceColumn = sourceColumn;
  259. }
  260. return param;
  261. }
  262. #endregion
  263. }
  264. }