/// <summary> /// 创建IDBDatabase /// </summary> /// <param name="strConnection">Connection的全称</param> /// <returns>创建的IDBDatabase</returns> public static IDBDatabase CreateDatabase(String strConnection) { List <DBProvider> list = Database.DBProvider.ProviderList; DBProvider dbProvider = list.Find(delegate(DBProvider x) { return(x.Connection.FullName == strConnection); }); if (dbProvider == null) { return(null); } IDBDatabase db = CreateDatabase(dbProvider); return(db); }
/// <summary> /// 创建IDBDatabase /// </summary> /// <param name="providerFactory">表示一组方法,这些方法用于创建提供程序对数据源类的实现的实例</param> /// <returns>创建的IDBDatabase</returns> public static IDBDatabase CreateDatabase(DbProviderFactory providerFactory) { DBProvider dbProvider = new DBProvider(); using (DbConnection cnn = providerFactory.CreateConnection()) using (DbCommand cmd = providerFactory.CreateCommand()) using (DbCommandBuilder cmdBuilder = providerFactory.CreateCommandBuilder()) using (DbDataAdapter adapter = providerFactory.CreateDataAdapter()) { DbParameter param = providerFactory.CreateParameter(); dbProvider.Connection = cnn.GetType(); dbProvider.Command = cmd.GetType(); dbProvider.Parameter = param.GetType(); dbProvider.CommandBuilder = cmdBuilder.GetType(); dbProvider.DataAdapter = adapter.GetType(); } IDBDatabase db = CreateDatabase(dbProvider); return(db); }
/// <summary> /// 创建IDBDatabase /// </summary> /// <param name="dbProvider">数据库驱动提供者</param> /// <returns>创建的IDBDatabase</returns> public static IDBDatabase CreateDatabase(DBProvider dbProvider) { Type type = DbDatabase.MakeGenericType(dbProvider.Connection, dbProvider.Command, dbProvider.Parameter, dbProvider.DataAdapter, dbProvider.CommandBuilder); IDBDatabase db = Activator.CreateInstance(type) as IDBDatabase; String strCnnName = dbProvider.Connection.FullName.ToUpper(); if (strCnnName.Contains(".OLEDB.") || strCnnName.Contains(".ODBC.")) { db.ParameterDerive = DBUnnamedParameterDerive.Instance; } else if (strCnnName.Contains("ORACLE")) { db.ParameterDerive = DBOracleParameterDerive.Instance; } else { db.ParameterDerive = DBNamedParameterDerive.Instance; } return(db); }
private static void GetProvider(Assembly assembly, List <DBProvider> lProvider) { List <Type> lConnection = new List <Type>(); List <Type> lCommand = new List <Type>(); List <Type> lParameter = new List <Type>(); List <Type> lDataAdapter = new List <Type>(); List <Type> lCommandBuilder = new List <Type>(); Type[] types = assembly.GetTypes(); foreach (Type t in types) { if (!t.IsClass) { continue; } if (!t.IsPublic) { continue; } if (t.BaseType == typeof(DbConnection)) { lConnection.Add(t); } else if (t.BaseType == typeof(DbCommand)) { lCommand.Add(t); } else if (t.BaseType == typeof(DbParameter)) { lParameter.Add(t); } else if (t.BaseType == typeof(DbDataAdapter)) { lDataAdapter.Add(t); } else if (t.BaseType == typeof(DbCommandBuilder)) { lCommandBuilder.Add(t); } } foreach (Type cnn in lConnection) { String ns = cnn.Namespace; DBProvider provider = new DBProvider(); provider.Connection = cnn; Predicate <Type> func = delegate(Type x) { return(x.Namespace == ns); }; provider.Command = lCommand.Find(func); provider.Parameter = lParameter.Find(func); provider.DataAdapter = lDataAdapter.Find(func); provider.CommandBuilder = lCommandBuilder.Find(func); if (provider.Command != null && provider.Parameter != null && provider.DataAdapter != null && provider.CommandBuilder != null) { lProvider.Add(provider); } } }