コード例 #1
0
ファイル: AdoCommandBase.cs プロジェクト: modulexcite/pash-1
        private void CreateConnectionFromParameters()
        {
            DbConnectionStringBuilder builder = _factory.CreateConnectionStringBuilder();

            try
            {
                // most builders should translate these generic keywords
                // to provider-specific native keywords.

                if (!String.IsNullOrEmpty(this.UserName))
                {
                    builder.Add("User", this.UserName);
                }
                if (!String.IsNullOrEmpty(this.Password))
                {
                    builder.Add("Password", this.Password);
                }
                if (!String.IsNullOrEmpty(this.Server))
                {
                    builder.Add("Server", this.Server);
                }
                if (!String.IsNullOrEmpty(this.Database))
                {
                    builder.Add("Database", this.Database);
                }
            }
            catch (ArgumentException ex)
            {
                // invalid keyword(s)
                throw new ArgumentException(GetValidKeywords(builder), ex);
            }

            _connection = _factory.CreateConnection();
            _connection.ConnectionString = builder.ConnectionString;
        }
        /// <summary>
        /// 判断数据库连接 是否一致
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="connectionString"></param>
        /// <returns>true:一致</returns>
        private bool ConnectionStringCompare(DbConnection conn, string connectionString)
        {
            DbProviderFactory factory = DbProviderFactories.GetFactory(conn);

            DbConnectionStringBuilder a = factory.CreateConnectionStringBuilder();

            a.ConnectionString = conn.ConnectionString;

            DbConnectionStringBuilder b = factory.CreateConnectionStringBuilder();

            b.ConnectionString = connectionString;

            return(a.EquivalentTo(b));
        }
コード例 #3
0
        public override DbConnectionStringBuilder CreateConnectionStringBuilder(string connectionString)
        {
            var builder = dbFactory.CreateConnectionStringBuilder();

            builder.ConnectionString = connectionString;
            return(builder);
        }
コード例 #4
0
        public static DbConnectionStringBuilder CreateConnectionStringBuilder(this DbProviderFactory @this, string connectionString)
        {
            var dbConnectionStringBuilder = @this.CreateConnectionStringBuilder() !;

            dbConnectionStringBuilder.ConnectionString = connectionString;
            return(dbConnectionStringBuilder);
        }
コード例 #5
0
ファイル: DbContext.cs プロジェクト: zyg0717/LJTH_SJSJ
        /// <summary>
        /// 获取一个DbContext对象
        /// </summary>
        /// <param name="name">连接名称</param>
        /// <param name="autoClose">是否自动关闭</param>
        /// <returns>DbContext对象</returns>
        public static DbContext GetContext(string name, bool autoClose)
        {
            //得到映射后连接名称
            name = DbConnectionMappingContext.GetMappedConnectionName(name);

            DbProviderFactory factory = DbConnectionManager.GetDbProviderFactory(name);

            DbConnectionStringBuilder csb = factory.CreateConnectionStringBuilder();

            csb.ConnectionString = DbConnectionManager.GetConnectionString(name);

            bool enlist = true;

            if (csb.ContainsKey("enlist"))
            {
                enlist = (bool)csb["enlist"];
            }

            DbContext result = null;

            if (enlist)
            {
                result = new AutoEnlistDbContext(name, autoClose);
            }
            else
            {
                result = new NotEnlistDbContext(name, autoClose);
            }

            result.autoClose = autoClose;

            return(result);
        }
コード例 #6
0
        private static void PopulateSqlServer2000(DbConnection conn, DbProviderFactory factory, DbConfiguration cfg)
        {
            if (factory is System.Data.SqlClient.SqlClientFactory)
            {
                var connectionStringBuilder = factory.CreateConnectionStringBuilder();
                connectionStringBuilder.ConnectionString = cfg.ConnectionString;
                connectionStringBuilder["Database"]      = "master";

                var state = conn.State;
                if (state != ConnectionState.Open)
                {
                    conn.Open();
                }

                var serverVersion = conn.ServerVersion;
                if (state != ConnectionState.Open)
                {
                    conn.Close();
                }

                var version = int.Parse(serverVersion.Substring(0, 2));
                if (version < 9)
                {
                    InitMsSql2000(cfg);
                }
            }
        }
コード例 #7
0
        private static DbConnection DbConnection()
        {
            //get the proper factory
            DbProviderFactory         factory           = DbProviderFactories.GetFactory(Global.Core.DatabaseProvider);
            DbConnectionStringBuilder dbProviderBuilder = factory.CreateConnectionStringBuilder();
            var connString = Global.Core.ConnectionString;

            //make sure it got created
            if (dbProviderBuilder != null)
            {
                dbProviderBuilder.ConnectionString = connString;
                //if this provider supports this setting, set it.
                if (dbProviderBuilder.ContainsKey("MultipleActiveResultSets"))
                {
                    dbProviderBuilder["MultipleActiveResultSets"] = true;
                }
                //use the new modified connection string
                connString = dbProviderBuilder.ConnectionString;
            }
            //create a command of the proper type.
            DbConnection dbCon = factory.CreateConnection();

            //set the connection string
            if (dbCon != null)
            {
                dbCon.ConnectionString = connString;
            }
            return(dbCon);
        }
コード例 #8
0
        /// <summary>
        /// Constructor
        /// </summary>
        public SyncQueue()
        {
            messageQueue = new Queue();
            synchQ       = Queue.Synchronized(messageQueue);

            using (DataTable tbl = DbProviderFactories.GetFactoryClasses())
            {
                foreach (DataRow row in tbl.Rows)
                {
                    string prov = row[2].ToString();

                    /*if (prov.IndexOf("SQLite", 0, StringComparison.OrdinalIgnoreCase) != -1
                    || prov.IndexOf("SqlClient", 0, StringComparison.OrdinalIgnoreCase) != -1
                    || )
                    ||  _provider.Items.Add(prov);*/
                    if (prov == "CSLibrary.Data.SQLite")
                    {
                        _fact = DbProviderFactories.GetFactory(prov);
                        break;
                    }
                }
            }

            _cnn = _fact.CreateConnection();
            _cnn.ConnectionString       = databaseName;
            _cnnstring                  = _fact.CreateConnectionStringBuilder();
            _cnnstring.ConnectionString = databaseName;
        }
コード例 #9
0
        /// <summary>
        /// Creates query context instance.
        /// </summary>
        internal QueryContext(string connectionContextName)
        {
            // Get entity works context.
            var entityWorksContext = EntityWorksContext.GetEntityWorksContext();

            // Get connection context.
            this.ConnectionContext = EntityWorksContext.ConnectionContexts[connectionContextName];

            // Set db provider factory.
            this.DbProviderFactory = this.ConnectionContext.GetSpecificDbProvider();

            // Set db string builder.
            this.DbConnectionStringBuilder = DbProviderFactory.CreateConnectionStringBuilder();

            // Parse connection string to builder.
            this.DbConnectionStringBuilder.ConnectionString = ConnectionContext.ConnectionString;

            // Set connection segments.
            Name     = connectionContextName;
            Database = DbConnectionStringBuilder["Initial Catalog"] as string;
            Username = DbConnectionStringBuilder["User ID"] as string;
            Password = DbConnectionStringBuilder["Password"] as string;
            Hostname = DbConnectionStringBuilder["Data Source"] as string;

            // Set connection string.
            ConnectionString = DbConnectionStringBuilder.ConnectionString;

            // Set default language code.
            LanguageCode = entityWorksContext.LanguageCode;
        }
コード例 #10
0
        private static string GetDatabaseName()
        {
            DbConnectionStringBuilder builder = factory.CreateConnectionStringBuilder();

            builder.ConnectionString = connectionStringSettings.ConnectionString;

            return(builder["Initial Catalog"].ToString());
        }
コード例 #11
0
        public override DbConnectionStringBuilder CreateConnectionStringBuilder()
        {
            var connectionBuilder = DbProviderFactory.CreateConnectionStringBuilder();

            AssertNullReference(connectionBuilder);
            connectionBuilder.ConnectionString = ConnectionString;
            return(connectionBuilder);
        }
コード例 #12
0
 public TunnelConnection(ITunnelDriver driver, string conns)
 {
     m_driver  = driver;
     m_factory = driver.ProviderFactory;
     m_params  = (TunnelConnectionStringBuilder)m_factory.CreateConnectionStringBuilder();
     m_params.ConnectionString = conns;
     m_driver.LoadConnectionParams(m_params);
 }
コード例 #13
0
        public static bool ConnectionStringCompare(DbConnection connection, string connectionString)
        {
            DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient");

            var first = factory.CreateConnectionStringBuilder();

            first.ConnectionString = connection.ConnectionString;

            var second = factory.CreateConnectionStringBuilder();

            second.ConnectionString = connectionString;

            var result = first["Data Source"].Equals(second["Data Source"]);

            Console.WriteLine("EquivalentTo " + result.ToString());

            return(result);
        }
コード例 #14
0
        public ConnectionStringEditorDlg()
        {
            InitializeComponent();

            DbProviderFactory factory = DbProviderFactories.GetFactory("MySql.Data.MySqlClient");

            builder = factory.CreateConnectionStringBuilder();
            connStrProps.SelectedObject = builder;
        }
コード例 #15
0
        public DataTable Execute(string dataSource, object arg)
        {
            DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");

            using (DbConnection conn = factory.CreateConnection())
            {
                #region
                //
                // Excel用の接続文字列を構築.
                //
                // Providerは、Microsoft.ACE.OLEDB.12.0を使用する事。
                // (JETドライバを利用するとxlsxを読み込む事が出来ない。)
                //
                // Extended Propertiesには、ISAMのバージョン(Excel 12.0)とHDRを指定する。
                // (2003までのxlsの場合はExcel 8.0でISAMバージョンを指定する。)
                // HDRは先頭行をヘッダ情報としてみなすか否かを指定する。
                // 先頭行をヘッダ情報としてみなす場合はYESを、そうでない場合はNOを設定。
                //
                // HDR=NOと指定した場合、カラム名はシステム側で自動的に割り振られる。
                // (F1, F2, F3.....となる)
                //
                #endregion
                DbConnectionStringBuilder builder = factory.CreateConnectionStringBuilder();

                builder["Provider"]            = "Microsoft.ACE.OLEDB.12.0";
                builder["Data Source"]         = dataSource;
                builder["Extended Properties"] = "Excel 12.0;HDR=YES";

                conn.ConnectionString = builder.ToString();
                conn.Open();

                #region
                //
                // SELECT.
                //
                // 通常のSQLのように発行できる。その際シート指定は
                // [Sheet1$]のように行う。範囲を指定することも出来る。[Sheet1$A1:C7]
                // -------------------------------------------------------------------
                // INSERT
                //
                // こちらも普通のSQLと同じように発行できる。
                // 尚、トランザクションは設定できるが効果は無い。
                // (ロールバックを行ってもデータは戻らない。)
                //
                // また、INSERT,UPDATEはエクセルを開いた状態でも
                // 行う事ができる。
                //
                // データの削除は行う事ができない。(制限)
                //
                #endregion
                using (DbCommand command = conn.CreateCommand())
                {
                    return(RunSql(command, arg));
                }
            }
            throw new Exception();
        }
コード例 #16
0
        public static void ExecuteReader(string connectionString, bool singleRow, string commandText, CommandType commandType, DbExecutionParameters executionParameters, Action <DbDataReader> handleOneRow, params DbParameter[] parameters)
        {
            using (var connection = _factory.CreateConnection())
            {
                if (executionParameters != null && executionParameters.ConnectionTimeout.HasValue)
                {
                    var connectionStringBuilder = _factory.CreateConnectionStringBuilder();
                    connectionStringBuilder.ConnectionString = connectionString;
                    if (connectionStringBuilder is SqlConnectionStringBuilder)
                    {
                        ((SqlConnectionStringBuilder)connectionStringBuilder).ConnectTimeout = executionParameters.ConnectionTimeout.Value;
                    }
                    else
                    {
                        throw new Exception("Do not support setting ConnectionTimeout on " + connectionStringBuilder.GetType().ToString());
                    }
                    connectionString = connectionStringBuilder.ToString();
                }
                connection.ConnectionString = connectionString;
                connection.Open();

                var logContext = LogBegin("ExecuteReader", connection, commandText, parameters);

                try
                {
                    using (var cmd = _factory.CreateCommand())
                    {
                        cmd.Connection  = connection;
                        cmd.CommandText = commandText;
                        cmd.CommandType = commandType;
                        if (executionParameters != null && executionParameters.CommandTimeout.HasValue)
                        {
                            cmd.CommandTimeout = executionParameters.CommandTimeout.Value;
                        }

                        cmd.Parameters.AddRange(parameters);

                        using (var reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                handleOneRow(reader);

                                if (singleRow)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
                finally
                {
                    LogEnd(logContext);
                }
            }
        }
コード例 #17
0
        public ConnectionStringEditorDlg()
        {
            InitializeComponent();

            DbProviderFactory factory = MySqlClientFactory.Instance;

            builder = factory.CreateConnectionStringBuilder();
            connStrProps.SelectedObject = builder;
        }
コード例 #18
0
        public static string GetPGSQLConnectionString(string server, string database, string login, string password, string connectionString = "")
        {
            var builder = dbFactory.CreateConnectionStringBuilder();

            builder.ConnectionString = connectionString;
            var srv = server.Split(':');

            builder["Server"]             = srv[0];
            builder["Port"]               = srv.Length > 1 ? srv[1] : "5432";
            builder["UserID"]             = login;
            builder["Password"]           = password;
            builder["IntegratedSecurity"] = login.IsEmpty() && password.IsEmpty();
            if (!database.IsEmpty())
            {
                builder["Database"] = database;
            }

            return(builder.ConnectionString);
        }
コード例 #19
0
        //[ConnectionStringORA]Oracle接続用ConnectionString(ODP/ADO共通)
        private static string ConnectionStringORA(DbProviderFactory factory)
        {
            DbConnectionStringBuilder csb = factory.CreateConnectionStringBuilder();

            csb.Add("Data Source", "xxx.xxx.xxx.xxx:9999/SID");
            csb.Add("User ID", "USERID");
            csb.Add("Password", "PASSWORD");

            return(csb.ConnectionString);
        }
コード例 #20
0
        protected DbRepository(string connectionStringName = "natrail", int?defaultCommandTimeout = null)
        {
            _connString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
            _dbFactory  = DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings[connectionStringName].ProviderName);

            DbConnectionStringBuilder connBuilder = _dbFactory.CreateConnectionStringBuilder();

            connBuilder.ConnectionString = _connString;
            _commandTimeout = defaultCommandTimeout ?? _defaultCommandTimeout;
        }
コード例 #21
0
        private DbConnectionStringBuilder GetConnectionStringBuilder()
        {
            DbProviderFactory         factory = CreateDbProviderFactory();
            DbConnectionStringBuilder builder = factory.CreateConnectionStringBuilder();

            builder[Keys.DataSource] = DataSourceTextBox != null ? DataSourceTextBox.Text : string.Empty;
            builder[Keys.Pooling]    = true;

            return(builder);
        }
コード例 #22
0
        /// <summary>
        /// 接続文字列の設定を行います。
        /// </summary>
        /// <param name="connection">DbConnection オブジェクト。</param>
        /// <param name="providerFactory">DbProviderFactory オブジェクト。</param>
        /// <param name="connectionValues">接続文字列。</param>
        public static void SetConnectionString(this DbConnection connection, DbProviderFactory providerFactory, Dictionary <string, string> connectionValues)
        {
            var builder = providerFactory.CreateConnectionStringBuilder();

            foreach (KeyValuePair <string, string> config in connectionValues)
            {
                builder[config.Key] = config.Value;
            }
            connection.ConnectionString = builder.ConnectionString;
        }
コード例 #23
0
 private void db_connect(string dbfilename)
 {
     //if (System.IO.File.Exists(dbfilename)) System.IO.File.Delete(dbfilename);
     makeProvider();
     _dbfact = DbProviderFactories.GetFactory("System.Data.SQLite");
     _dbcon  = _dbfact.CreateConnection();
     _dbcon.ConnectionString       = "Data Source=" + dbfilename + ";Pooling=true;FailIfMissing=false";
     _dbconstring                  = _dbfact.CreateConnectionStringBuilder();
     _dbconstring.ConnectionString = "Data Source=" + dbfilename + ";Pooling=true;FailIfMissing=false";
     _dbcon.Open();
 }
コード例 #24
0
        public DbConnectionStringBuilder GetConnectionStringBuilder(string connectionString = null)
        {
            var builder = _dbProviderFactory.CreateConnectionStringBuilder();

            if (builder != null && !string.IsNullOrWhiteSpace(connectionString))
            {
                builder.ConnectionString = connectionString;
            }

            return(builder);
        }
コード例 #25
0
ファイル: ConnectDialog.cs プロジェクト: Meetkp001/mysql
 public ConnectDialog()
 {
     InitializeComponent();
     factory = DbProviderFactories.GetFactory("MySql.Data.MySqlClient");
     if (factory == null)
     {
         throw new Exception("MySql Data Provider is not correctly registered");
     }
     connectionStringBuilder             = factory.CreateConnectionStringBuilder();
     connectionProperties.SelectedObject = connectionStringBuilder;
 }
コード例 #26
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bufferSize">BufferSize can't excess <c>MaxBufferSize = 2000</c></param>
        public SyncQueue(uint bufferSize)
        {
            messageQueue = new Queue();
            synchQ       = Queue.Synchronized(messageQueue);

            _fact = DbProviderFactories.GetFactory("CSLibray.Data.SQLite");
            _cnn  = _fact.CreateConnection();
            _cnn.ConnectionString       = databaseName;
            _cnnstring                  = _fact.CreateConnectionStringBuilder();
            _cnnstring.ConnectionString = databaseName;
            this.BufferSize             = bufferSize;
        }
コード例 #27
0
 private void wpconnprops_ShowFromNext(object sender, EventArgs e)
 {
     try
     {
         m_builder = m_factory.CreateConnectionStringBuilder();
         ctlProperties.SelectedObject = m_builder;
     }
     catch (Exception err)
     {
         Errors.Report(err);
     }
 }
コード例 #28
0
        public GenericConnEditor(GenericSqlStoredConnection conn)
        {
            InitializeComponent();

            m_conn = conn;
            DbProviderFactory factory = conn.GetFactory();

            m_builder = factory.CreateConnectionStringBuilder();
            m_builder.ConnectionString = conn.ConnectionString;

            SelectedObject = m_builder;
        }
コード例 #29
0
        public AdoDotNetConnectionProperties(string providerName)
        {
            Debug.Assert(providerName != null);
            _providerName = providerName;

            // Create an underlying connection string builder object
            DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);

            Debug.Assert(factory != null);
            _connectionStringBuilder = factory.CreateConnectionStringBuilder();
            Debug.Assert(_connectionStringBuilder != null);
            _connectionStringBuilder.BrowsableConnectionString = false;
        }
コード例 #30
0
        /// <summary>
        /// Returns a new instance of the provider's class that implements the
        /// System.Data.Common.DbConnectionStringBuilder class.
        /// </summary>
        /// <param name="factory">
        /// The System.Data.Common.DbProviderFactory used to create the
        /// System.Data.Common.DbConnectionStringBuilder.
        /// </param>
        /// <param name="connectionString">
        /// Connection string associated with the System.Data.Common.DbConnectionStringBuilder.
        /// </param>
        /// <returns>
        /// A new instance of System.Data.Common.DbConnectionStringBuilder.
        /// </returns>
        public static DbConnectionStringBuilder CreateConnectionStringBuilder(
            this DbProviderFactory factory,
            string connectionString)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory", "factory should not be null.");
            }

            DbConnectionStringBuilder builder = factory.CreateConnectionStringBuilder();

            builder.ConnectionString = connectionString;
            return(builder);
        }