gpt4 book ai didi

c# - 如何形成正确的MySQL连接字符串?

转载 作者:行者123 更新时间:2023-11-29 17:01:10 25 4
gpt4 key购买 nike

我正在使用 C#,并且正在尝试连接到 00webhost 托管的 MySQL 数据库。 .

我在 connection.Open() 线上收到错误:

there is no MySQL host with these parameters.

我已经检查过了,一切似乎都没问题。

string MyConString = "SERVER=mysql7.000webhost.com;" +
"DATABASE=a455555_test;" +
"UID=a455555_me;" +
"PASSWORD=something;";

MySqlConnection connection = new MySqlConnection(MyConString);

MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;

command.CommandText = "INSERT Test SET lat=" +
OSGconv.deciLat + ",long=" + OSGconv.deciLon;

connection.Open();
Reader = command.ExecuteReader();

connection.Close();

此连接字符串有什么问题?

最佳答案

尝试以这种方式创建连接字符串:

MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
conn_string.Server = "mysql7.000webhost.com";
conn_string.UserID = "a455555_test";
conn_string.Password = "a455555_me";
conn_string.Database = "xxxxxxxx";

using (MySqlConnection conn = new MySqlConnection(conn_string.ToString()))
using (MySqlCommand cmd = conn.CreateCommand())
{ //watch out for this SQL injection vulnerability below
cmd.CommandText = string.Format("INSERT Test (lat, long) VALUES ({0},{1})",
OSGconv.deciLat, OSGconv.deciLon);
conn.Open();
cmd.ExecuteNonQuery();
}

关于c# - 如何形成正确的MySQL连接字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52241769/

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