简体   繁体   中英

Unable to connect to specified MySQL host?

I am messing around with C# code from Magic Vision ( https://github.com/petesimard/Magic-Vision ). When I debug it using Visual C# 2010 Express, I get an error on the statement sql.Open() that says

"MySQL exception was unhandled: unable to connect to any of the specified MySQL hosts."

How do I resolve this error?

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;


namespace PoolVision
{
public class MySqlClient
{
    private MySqlConnection sql;

    public DataRow dbRow(String query)
    {
        MySqlCommand command = sql.CreateCommand();
        command.CommandText = query;

        DataTable selectDT = new DataTable();
        MySqlDataAdapter dataAd = new MySqlDataAdapter(command);

        dataAd.Fill(selectDT);

        if (selectDT.Rows.Count > 0)
            return selectDT.Rows[0];
        else
            return null;
    }

    public int lastInsertId()
    {
        DataRow r = dbRow("SELECT last_insert_id() as lid");

        Int64 id = (Int64)r[0];

        return (int)id;
    }

    public int affectedRows()
    {
        DataRow r = dbRow("SELECT ROW_COUNT()");
        int id = (int)r[0];

        return id;
    }

    public DataTable dbResult(String query)
    {
        MySqlCommand command = sql.CreateCommand();
        command.CommandText = query;

        DataTable selectDT = new DataTable();
        MySqlDataAdapter dataAd = new MySqlDataAdapter(command);

        dataAd.Fill(selectDT);

        return selectDT;

    }

    internal int dbNone(string query)
    {
        MySqlCommand command = sql.CreateCommand();
        //MySqlDataReader Reader;
        command.CommandText = query;
        return command.ExecuteNonQuery();
    }

    public MySqlClient(String SqlConString)
    {
        sql = new MySqlConnection(SqlConString);
        sql.Open();
    }

    public DateTime ConvertFromUnixTimestamp(double timestamp)
    {
        DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        return origin.AddSeconds(timestamp);
    }

    public double ConvertToUnixTimestamp(DateTime date)
    {
        DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        TimeSpan diff = date - origin;
        return Math.Floor(diff.TotalSeconds);
    }
}
}

检查您的连接字符串,并确保该字符串对您要连接的服务器和数据库有效。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM