gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:46:23 24 4
gpt4 key购买 nike

我正在摆弄来自 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);
}
}
}

最佳答案

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

关于c# - 无法连接到指定的MySQL主机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17352498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com