0

我正在处理来自 Magic Vision ( https://github.com/petesimard/Magic-Vision ) 的 C# 代码。当我使用 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);
    }
}
}
4

1 回答 1

1

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

于 2013-06-27T20:24:05.650 回答