我正在开发与免费托管网站上托管的MySQL数据库交互的C#应用程序:
private void Initialize()
{
server = "ex.exexexex.com:3306";
database = "1_dotnet";
uid = "1_dotnet";
password = "********";
connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message.ToString());
return false;
}
}
但是有一个错误:
无法连接到任何指定的MySQL主机。
答案 0 :(得分:0)
我会在你的web.config中使用连接字符串设置
<connectionStrings>
<add name="strConnection" connectionString="Data Source=DATABASE_IP_OR_DOMAIN;userid=MYSQL_LOGIN;password=MYSQL_PASSWORD;database=DATABASE_NAME;Connection Lifetime=0;Connect Timeout=300;pooling='true';Max Pool Size=200" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
然后在您的C#代码中,您可以像这样引用您的数据库连接
MySqlConnection objConnection = new MySqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
MySqlCommand cmd = default(MySqlCommand);
objConnection.Open();