Пример #1
0
        /// <summary>
        /// Opens a database connection with the settings specified by the ConnectionString property of the provider-specific Connection object.
        /// </summary>
        public void Open()
        {
            string database     = this.FindValueInCS("Data Source=");
            string location     = this.FindValueInCS("Location=");
            string userid       = this.FindValueInCS("User ID=");
            string password     = this.FindValueInCS("Password="******"Port=");
            string characterset = this.FindValueInCS("Character Set=");
            string clientPath   = this.FindValueInCS("Client Path=");

            // BEGIN ADDITION 2004-07-01 by Alex Seewald
            string strtimeout          = this.FindValueInCS("Timeout=");
            uint   timeout             = strtimeout != "" ? uint.Parse(strtimeout) : 0;
            bool   activateCompression = (this.FindValueInCS("Compression=") != ""); // will default to false if omitted

            if (port == "")
            {
                port = "3306";
            }

            if (NativeConnection == null)
            {
                NativeConnection = new NativeConnection(clientPath, NativeTracer);
            }
            else
            {
                throw new MySqlException("Connection already open");
            }


            // mysql_options must be called after mysql_init and before mysql_real_connect

            // Timeout option
            if (timeout != 0)
            {
                NativeConnection.mysql_options(mysql_option.MYSQL_OPT_CONNECT_TIMEOUT, ref timeout);
            }

            // Compression option
            uint _null = 0;

            if (activateCompression)
            {
                NativeConnection.mysql_options(mysql_option.MYSQL_OPT_COMPRESS, ref _null);
            }

            // Change autentication method
            //var rv= NativeConnection.mysql_options(mysql_option.MYSQL_DEFAULT_AUTH, "mysql_native_password");

            // END ADDITION 2004-07-01

            var retval = NativeConnection.mysql_real_connect(location, userid, password, database, Convert.ToUInt32(port), null, 0);

            /* Explicit error conection:
             * "Christophe Ravier" <*****@*****.**> 2003-11-27*/
            if (retval == IntPtr.Zero)
            {
                var diagnosticInfo = new Dictionary <string, string>();

                diagnosticInfo["Hostname"] = location;
                diagnosticInfo["Port"]     = port;
                diagnosticInfo["User"]     = userid;
                diagnosticInfo["Database"] = database;

                IPHostEntry entry = null;
                try
                {
                    entry = Dns.GetHostEntry(location);
                }
                catch (Exception ex)
                {
                    diagnosticInfo["Server IPs"] = ex.Message;
                }
                if (entry != null)
                {
                    if (entry.AddressList.Any())
                    {
                        var ips = string.Join(", ", entry.AddressList.Select(x => x.ToString()));
                        diagnosticInfo["Server IPs"] = ips;

                        try
                        {
                            var c = new TcpClient(location, Convert.ToInt32(port));
                            c.Close();
                        }
                        catch (Exception ex)
                        {
                            diagnosticInfo["Tcp connection test"] = "Error: " + ex.Message;
                        }
                    }
                    else
                    {
                        diagnosticInfo["Server IPs"] = "name resolution failed";
                    }
                }

                throw new MySqlException(NativeConnection, string.Join("\n", diagnosticInfo.Select(x => x.Key + ": " + x.Value)));
            }

            try
            {
                if (!string.IsNullOrEmpty(characterset))
                {
                    if (NativeConnection.mysql_set_character_set(characterset) == 0)
                    {
                        Encoding = Encoding.GetEncoding(characterset);
                    }
                }
            }
            catch (EntryPointNotFoundException)
            {
                string version = NativeConnection.ClientVersion;
                throw new MySqlException(NativeConnection, "'Character Set' keyword not supported in client version " + version);
            }

#if DEBUG
            NativeConnection.mysql_ping();
#endif
            Dbname = database;
        }