コード例 #1
0
        public DbConnection CreateConnection()
        {
            DbProviderFactory factory = null;

            string lowerProviderName = this._dbProvider.ProviderName.ToLower();

            if (lowerProviderName.Contains("oracle"))
            {
                factory = new OracleClientFactory();
            }
            else if (lowerProviderName.Contains("mysql"))
            {
                factory = new MySqlClientFactory();
            }
            else
            {
                factory = DataUtils.GetDbProviderFactory(this._dbProvider.ProviderName);
            }

            DbConnection connection = factory.CreateConnection();

            if (connection != null)
            {
                connection.ConnectionString = this._connectionString;
                return(connection);
            }
            else
            {
                return(null);
            }
        }
コード例 #2
0
        public void ClientFactory()
        {
            DbProviderFactory         f  = new MySqlClientFactory();
            DbConnection              c  = f.CreateConnection();
            DbConnectionStringBuilder cb = f.CreateConnectionStringBuilder();

            cb.ConnectionString = GetConnectionString(true);
            c.ConnectionString  = cb.ConnectionString;
            c.Open();

            DbCommand cmd = f.CreateCommand();

            cmd.Connection  = c;
            cmd.CommandText = "SHOW TABLES FROM test";
            cmd.CommandType = CommandType.Text;
            DbDataReader reader = null;

            try
            {
                reader = cmd.ExecuteReader();
                reader.Read();
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
コード例 #3
0
        public void ClientFactory()
        {
            DbProviderFactory f = new MySqlClientFactory();

            using (DbConnection c = f.CreateConnection())
            {
                DbConnectionStringBuilder cb = f.CreateConnectionStringBuilder();
                cb.ConnectionString = st.GetConnectionString(true);
                c.ConnectionString  = cb.ConnectionString;
                c.Open();

                DbCommand cmd = f.CreateCommand();
                cmd.Connection  = c;
                cmd.CommandText = "SELECT 1";
                cmd.CommandType = CommandType.Text;
                using (DbDataReader reader = cmd.ExecuteReader())
                {
                    reader.Read();
                }
            }
        }
コード例 #4
0
        public void DbProviderFactories()
        {
#if NETCOREAPP2_1
            MySqlClientFactory.Register();
#endif
#if BASELINE
            var providerInvariantName = "MySql.Data.MySqlClient";
#else
            var providerInvariantName = MySqlClientFactory.InvariantName;
#endif
            var factory = System.Data.Common.DbProviderFactories.GetFactory(providerInvariantName);
            Assert.NotNull(factory);
            Assert.Same(MySqlClientFactory.Instance, factory);

            using (var connection = new MySqlConnection())
            {
                factory = System.Data.Common.DbProviderFactories.GetFactory(connection);
                Assert.NotNull(factory);
                Assert.Same(MySqlClientFactory.Instance, factory);
            }
        }
コード例 #5
0
        /// <summary>
        /// 通过配置文件中的ProviderName来自动生成Connection对象
        /// </summary>
        /// <param name="databaseName">在appsetting中配置的数据库连接对象名称</param>
        /// <returns></returns>
        public IDbConnection CreateConnection(string databaseName)
        {
            bool encrypt = TwinkleContext.Config.GetValue <bool>($"ConnectionStrings:{databaseName}:Encrypt");

            string connectString = TwinkleContext.Config.GetValue <string>($"ConnectionStrings:{databaseName}:ConnectString");

            //如果连接字符串被加密了 需要解密
            if (encrypt)
            {
                connectString = DataCipher.RSADecrypt(connectString);
            }

            string providerName = TwinkleContext.Config.GetValue <string>($"ConnectionStrings:{databaseName}:ProviderName");

            IDbConnection connection = null;

            switch (providerName)
            {
            case "System.Data.SqlClient":
                connection = SqlClientFactory.Instance.CreateConnection();
                break;

            case "Oracle.ManagedDataAccess.Client":
                connection = new OracleClientFactory().CreateConnection();
                break;

            case "MySql.Data.MySqlClient":
                connection = new MySqlClientFactory().CreateConnection();
                break;

            default:
                throw new DataException("There is no suitable Provider for creating IDbConnection");
            }

            connection.ConnectionString = connectString;

            this.Connection = connection;
            return(connection);
        }
コード例 #6
0
        public static DbProviderFactory GetFactory(string providerName)
        {
            if (providerName == null)
            {
                throw new ArgumentNullException("providerName");
            }
            DbProviderFactory dbFactory;

            switch (providerName)
            {
            case DbProviderNames.MySQL:
            case "MYSQL":
            case "MySQL":
            case "mysql":
                dbFactory = new MySqlClientFactory();
                break;

            case DbProviderNames.Oracle_Managed_ODP:
            case "ORACLE":
            case "Oracle":
            case "oracle":
                dbFactory = new Oracle.ManagedDataAccess.Client.OracleClientFactory();
                break;

            case DbProviderNames.SqlServer:
            case "SQLSERVER":
            case "SqlServer":
            case "sqlserver":
                dbFactory = SqlClientFactory.Instance;
                break;

            default:
                dbFactory = null;
                break;
            }
            return(dbFactory);
        }
コード例 #7
0
ファイル: ConnManager.cs プロジェクト: wcss2010/ProjectLoader
 public static void Close()
 {
     Context.getConnection().Dispose();
     factory = null;
     Context = null;
 }
コード例 #8
0
ファイル: ConnManager.cs プロジェクト: wcss2010/ProjectLoader
 /// <summary>
 /// 初始化数据库连接
 /// </summary>
 public static void Open(string ip, string user, string pwd, string dbName)
 {
     factory = new MySqlClientFactory();
     Context = new DbContext(dbName, "server=" + ip + ";port=3306;user="******";password="******";database=" + dbName + ";Charset=utf8", factory);
     //Context.IsSupportInsertAfterSelectIdentity = false;
 }