无法连接到指定的MySQL主机?

时间:2013-06-27 20:22:45

标签: c# mysql

我正在搞乱Magic Vision的C#代码(https://github.com/petesimard/Magic-Vision)。 当我使用Visual C#2010 Express调试它时,我在语句sql.Open()上收到错误

“MySQL异常未处理:无法连接到任何指定的MySQL主机。”

如何解决此错误?

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);
    }
}
}

1 个答案:

答案 0 :(得分:1)

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