/// <summary>
        /// 创建一个数据提供程序实例
        /// </summary>
        /// <param name="connectionStringName"></param>
        /// <returns></returns>
        public static Database Create(string connectionStringName)
        {
            CheckUtil.ArgumentNotNullOrEmpty(connectionStringName, "connectionStringName");

            ConnectionStringSettings css = ConfigurationManager.ConnectionStrings[connectionStringName];
            if (css == null) throw new Exception(string.Format(Resources.Data.ConnectionStringNameNotFound, connectionStringName));

            string connectionString = css.ConnectionString;
            string providerName = css.ProviderName;
            Database db = new SqlServerDatabase(connectionString);
            DbProviderFactory providerFactory = null;

            if(string.IsNullOrEmpty(providerName))return db;

            providerFactory = DbProviderFactories.GetFactory(css.ProviderName);
            if (providerFactory == null) throw new Exception(string.Format(Resources.Data.DataProviderNotFound, css.ProviderName));

            switch (providerName)
            {
                //case "System.Data.SqlClient":
                //    break;
                case "System.Data.Odbc":
                    db = new OdbcDatabase(connectionString);
                    break;
                case "System.Data.OleDb":
                    db = new OleDbDatabase(connectionString);
                    break;
                case "System.Data.OracleClient":
                    db = new OracleDatabase(connectionString);
                    break;
                case "System.Data.SQLite":
                    db = new SQLiteDatabase(connectionString, providerFactory);
                    break;
                case "MySql.Data.MySqlClient":
                    db = new MySqlDatabase(connectionString, providerFactory);
                    break;
                case "IBM.Data.DB2":
                    db = new DB2Database(connectionString, providerFactory);
                    break;
                case "FirebirdSql.Data.FirebirdClient":
                    db = new FirebirdDatabase(connectionString, providerFactory);
                    break;
                default:
                    break;
            }

            return db;
        }
Example #2
0
        /// <summary>
        /// 创建一个数据提供程序实例
        /// </summary>
        /// <param name="connectionStringName"></param>
        /// <returns></returns>
        public static Database Create(string connectionStringName)
        {
            CheckUtil.ArgumentNotNullOrEmpty(connectionStringName, "connectionStringName");

            ConnectionStringSettings css = ConfigurationManager.ConnectionStrings[connectionStringName];
            if (css == null) throw new Exception(string.Format(Resources.Data.ConnectionStringNameNotFound, connectionStringName));

            string connectionString = css.ConnectionString;
            string providerName = css.ProviderName;
            Database db = new SqlServerDatabase(connectionString);
            DbProviderFactory providerFactory = null;

            if(string.IsNullOrEmpty(providerName))return db;

            //if (css.ProviderName == "System.Data.OleDb")
            //{
            //    providerFactory = OleDbFactory.Instance;
            //}
            //else
            //{
            //    providerFactory = DbProviderFactories.GetFactory(css.ProviderName);
            //}
            //if (providerFactory == null) throw new Exception(string.Format(Resources.Data.DataProviderNotFound, css.ProviderName));

            switch (providerName)
            {
                //case "System.Data.SqlClient":
                //    break;
                case "System.Data.Odbc":
                    db = new OdbcDatabase(connectionString);
                    break;
                case "System.Data.OleDb":
                    db = new OleDbDatabase(connectionString);
                    break;
                case "System.Data.OracleClient":
                    db = new OracleDatabase(connectionString);
                    break;
                case "Devart.Data.Oracle": //http://evget.com/zh-CN/product/954/feature.aspx  http://www.devart.com/ 
                case "DDTek.Oracle": //http://www.datadirect.com/index.html
                    providerFactory = DbProviderFactories.GetFactory(providerName);
                    db = new OracleDatabase(connectionString, providerFactory);
                    break;
                case "System.Data.SQLite":
                    providerFactory = DbProviderFactories.GetFactory(providerName);
                    db = new SQLiteDatabase(connectionString, providerFactory);
                    break;
                case "MySql.Data.MySqlClient":
                    providerFactory = DbProviderFactories.GetFactory(providerName);
                    db = new MySqlDatabase(connectionString, providerFactory);
                    break;
                case "IBM.Data.DB2":
                    providerFactory = DbProviderFactories.GetFactory(providerName);
                    db = new DB2Database(connectionString, providerFactory);
                    break;
                case "FirebirdSql.Data.FirebirdClient":
                    providerFactory = DbProviderFactories.GetFactory(providerName);
                    db = new FirebirdDatabase(connectionString, providerFactory);
                    break;
                default:
                    break;
            }

            return db;
        }