gpt4 book ai didi

c# - KeyNotFoundException 与在线数据库

转载 作者:搜寻专家 更新时间:2023-10-30 23:39:09 25 4
gpt4 key购买 nike

我正在尝试连接到一个免费的在线数据库 (db4free.net),但我无法连接,因为当程序尝试打开连接时会抛出 KeyNotFoundException。

这是我的代码:

public static class BaseDeDatos { 
static string myConnectionString = "SERVER=db4free.net;" + "DATABASE=******;" + "UID=******;" + "PASSWORD=*****;";
static MySqlConnection cnx = new MySqlConnection(myConnectionString);

static String error;

public static int Conectar()
{
int resultado = -1;
try
{
cnx.Open(); //The exception is thrown HERE
if (cnx.State == ConnectionState.Open)
resultado = 1;
}
catch (MySqlException ex)
{
error = ex.Message;
}
return resultado;
}

.
.
.
.
public static void myMethod(){

Conectar();

}
}

当我调用 myMethod() 时,异常被抛出。我可以将此数据库与 MySQLWorkbench 和 PHP Android 连接,但为什么我不能在 Visual Studio 中使用 C# 进行此连接?我确定我做错了什么 请帮助我

PD:我读到我需要更改数据库的字符集,但它不起作用。我尝试了所有可用的字符集

最佳答案

查看其他示例,例如 Connecting to a Online MySQL Database using VB.Net

MySQLConnection.ConnectionString = "server=db4free.net;Port=3306; User ID=db4freeusername; password=db4freepassword; database=nameofyourdatabase"

他们正在指定端口号,这可能是您的问题吗?

关于c# - KeyNotFoundException 与在线数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37372394/

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