Ejemplo n.º 1
0
        internal static DbConnection GetConnection(string connectionString, DatabaseType dbType)
        {
            if (string.IsNullOrEmpty(connectionString))
            {
                throw new Exception("connectionString is null!");
            }
            DbConnection dbConnection = null;

            switch (dbType)
            {
            case DatabaseType.MsSQLServer:
                dbConnection = new System.Data.SqlClient.SqlConnection(connectionString);
                break;

            case DatabaseType.MySQL:
                dbConnection = new global::MySql.Data.MySqlClient.MySqlConnection(connectionString);
                break;

            case DatabaseType.SQLite:
                dbConnection = new System.Data.SQLite.SQLiteConnection(connectionString);
                break;

            default:
                throw new Exception("DatabaseType is error!");
            }

            return(dbConnection);
        }
Ejemplo n.º 2
0
        private bool connectionMode = true;    // true=> Main Database, false => Read only DB. ( Phục vụ cho việc tối ưu hệ thống ).

        /// <summary>
        /// Open Connection
        /// </summary>
        public void Open()
        {
            if (_cnn == null)
            {
                if (connectionMode)
                {
                    _cnn = new global::MySql.Data.MySqlClient.MySqlConnection(@"server=" + DbConnectionMySqlOffline.MainServer + ";Port=" + DbConnectionMySqlOffline.Port + ";user id=" + DbConnectionMySqlOffline.user + ";password="******";persistsecurityinfo=True;database=" + DbConnectionMySqlOffline.MainDB + ";allowuservariables=True;convert zero datetime=True;Charset='utf8';");    //Allow Zero Datetime=true
                }
                else
                {
                    _cnn = new global::MySql.Data.MySqlClient.MySqlConnection(@"server=" + DbConnectionMySqlOffline.MainServer + ";Port=" + DbConnectionMySqlOffline.Port + ";user id=" + DbConnectionMySqlOffline.user + ";password="******";persistsecurityinfo=True;database=" + DbConnectionMySqlOffline.MainDB + ";allowuservariables=True;convert zero datetime=True;Charset='utf8';");   // cái này dùng khi có 2 database tự đồng bộ với nhau mới sử dụng nhé.
                }
            }
            if (_cnn.State != ConnectionState.Open)
            {
                _cnn.Open();
            }
        }
 private void InitConnection() {
     this._connection = new global::MySql.Data.MySqlClient.MySqlConnection();
     this._connection.ConnectionString = global::KanjiDatabase.Properties.Settings.Default.kanjidatabaseConnectionString;
 }
 private void InitConnection() {
     this._connection = new global::MySql.Data.MySqlClient.MySqlConnection();
     this._connection.ConnectionString = global::SMS_Gateway.Properties.Settings.Default.cateringConnectionString1;
 }
Ejemplo n.º 5
0
        public bool ConnectionTest()
        {
            MySql.Data.MySqlClient.MySqlConnection connection = new global::MySql.Data.MySqlClient.MySqlConnection();

            connection.ConnectionString = _ConnectionString;
            try
            {
                MySql.Data.MySqlClient.MySqlCommand mycommand = new global::MySql.Data.MySqlClient.MySqlCommand();
                mycommand.Connection = connection;
                //  mycommand.CommandText = SQL_Statement;
                mycommand.CommandType = global::System.Data.CommandType.Text;
                mycommand.Connection.Open();

                if (mycommand.Connection.State != global::System.Data.ConnectionState.Open)
                {
                    return false;
                }
                else
                {
                    mycommand.Connection.Close();
                    if(connection!=null)
                    {
                        connection.Close();
                    }
                    return true;
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); return false; }
        }
Ejemplo n.º 6
0
        public MySqlDataAdapter GetAdapterFromSQLCommand(string SQL_Statement)
        {
            FileWriteLine(SQL_Statement);

            MySql.Data.MySqlClient.MySqlDataAdapter myAdapter = null;

            if (SQL_Statement.Length > 0)
            {
                myAdapter = new MySql.Data.MySqlClient.MySqlDataAdapter();
                MySql.Data.MySqlClient.MySqlConnection connection = new global::MySql.Data.MySqlClient.MySqlConnection();
                connection.ConnectionString = _ConnectionString;

                MySqlCommand myCommand = new MySqlCommand(SQL_Statement, connection);

                try
                {
                    commandReturnObect.CommandText = SQL_Statement;
                    myAdapter.SelectCommand = myCommand;

                }
                catch (Exception sqlEx)
                {
                    mySave.InsertRow((int)Definition.Message.D_ALARM, sqlEx.ToString());
                }
                finally
                {
                    if (myCommand != null)
                    {
                        myCommand.Dispose();
                    }

                    if (connection != null)
                    {
                        connection.Close();
                    }
                }
            }
            else
            {
                mySave.InsertRow((int)Definition.Message.D_ALARM, "MySQLHelper::GetAdapterFromSQLCommand: SQL statement too short");
            }
            return myAdapter;
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Initializes a new MySqlConnection class.
 /// </summary>
 /// <param name="connectionString">The connection string.</param>
 public MySqlConnection(string connectionString)
 {
     _connection = new global::MySql.Data.MySqlClient.MySqlConnection(connectionString);
 }
 private void InitConnection() {
     this._connection = new global::MySql.Data.MySqlClient.MySqlConnection();
     this._connection.ConnectionString = global::gselvacentral.Properties.Settings.Default.gselvacentraldbConnectionString;
 }