/// <summary> /// 构造函数 /// </summary> /// <param name="dbProvider"></param> public Database(DbProvider dbProvider) { this.dbProvider = dbProvider; }
/// <summary> /// 创建数据库事件提供程序 /// </summary> /// <param name="assemblyName">Name of the assembly.</param> /// <param name="className">Name of the class.</param> /// <param name="connectionString">The conn STR.</param> /// <returns>The db provider.</returns> public static DbProvider CreateDbProvider(string assemblyName, string className, string connectionString, DatabaseType?databaseType) { Check.Require(connectionString, "connectionString", Check.NotNullOrEmpty); if (connectionString.IndexOf("microsoft.jet.oledb", StringComparison.OrdinalIgnoreCase) > -1 || connectionString.IndexOf(".db3", StringComparison.OrdinalIgnoreCase) > -1) { Check.Require(connectionString.IndexOf("data source", StringComparison.OrdinalIgnoreCase) > -1, "ConnectionString的格式有错误,请查证!"); string mdbPath = connectionString.Substring(connectionString.IndexOf("data source", StringComparison.OrdinalIgnoreCase) + "data source".Length + 1).TrimStart(' ', '='); if (mdbPath.ToLower().StartsWith("|datadirectory|")) { mdbPath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\App_Data" + mdbPath.Substring("|datadirectory|".Length); } else if (connectionString.StartsWith("./") || connectionString.EndsWith(".\\")) { connectionString = connectionString.Replace("/", "\\").Replace(".\\", AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\"); } connectionString = connectionString.Substring(0, connectionString.ToLower().IndexOf("data source")) + "Data Source=" + mdbPath; } //如果是~则表示当前目录 if (connectionString.Contains("~/") || connectionString.Contains("~\\")) { connectionString = connectionString.Replace("/", "\\").Replace("~\\", AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\"); } //by default, using sqlserver db provider if (string.IsNullOrEmpty(className)) { className = typeof(SqlServer.SqlServerProvider).ToString(); if (databaseType == null) { databaseType = DatabaseType.SqlServer; } } else if (string.Compare(className, "System.Data.SqlClient", true) == 0 || string.Compare(className, "Dos.ORM.SqlServer", true) == 0) { className = typeof(SqlServer.SqlServerProvider).ToString(); if (databaseType == null) { databaseType = DatabaseType.SqlServer; } } else if (string.Compare(className, "Dos.ORM.SqlServer9", true) == 0 || className.IndexOf("SqlServer9", StringComparison.OrdinalIgnoreCase) >= 0 || className.IndexOf("sqlserver2005", StringComparison.OrdinalIgnoreCase) >= 0 || className.IndexOf("sql2005", StringComparison.OrdinalIgnoreCase) >= 0) { className = typeof(SqlServer9.SqlServer9Provider).ToString(); if (databaseType == null) { databaseType = DatabaseType.SqlServer9; } } else if (className.IndexOf("oracle", StringComparison.OrdinalIgnoreCase) >= 0) { className = typeof(Oracle.OracleProvider).ToString(); if (databaseType == null) { databaseType = DatabaseType.Oracle; } } else if (className.IndexOf("access", StringComparison.OrdinalIgnoreCase) >= 0) { className = typeof(MsAccess.MsAccessProvider).ToString(); if (databaseType == null) { databaseType = DatabaseType.MsAccess; } } else if (className.IndexOf("mysql", StringComparison.OrdinalIgnoreCase) >= 0) { className = "Dos.ORM.MySql.MySqlProvider"; assemblyName = "Dos.ORM.MySql"; if (databaseType == null) { databaseType = DatabaseType.MySql; } } else if (className.IndexOf("sqlite", StringComparison.OrdinalIgnoreCase) >= 0) { className = "Dos.ORM.Sqlite.SqliteProvider"; assemblyName = "Dos.ORM.Sqlite"; if (databaseType == null) { databaseType = DatabaseType.Sqlite3; } } string cacheKey = string.Concat(assemblyName, className, connectionString); if (providerCache.ContainsKey(cacheKey)) { return(providerCache[cacheKey]); } else { System.Reflection.Assembly ass; if (assemblyName == null) { ass = typeof(DbProvider).Assembly; } else { ass = System.Reflection.Assembly.Load(assemblyName); } DbProvider retProvider = ass.CreateInstance(className, false, System.Reflection.BindingFlags.Default, null, new object[] { connectionString }, null, null) as DbProvider; if (retProvider != null && databaseType != null) { retProvider.DatabaseType = databaseType.Value; } providerCache.Add(cacheKey, retProvider); return(retProvider); } }