如何形成正确的MySQL连接字符串? [英] How to form a correct MySQL connection string?
本文介绍了如何形成正确的MySQL连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用C#,并且试图连接到由00webhost托管的MySQL数据库.
I am using C# and I am trying to connect to the MySQL database hosted by 00webhost.
我在connection.Open()
行上遇到错误:
没有使用此参数的mysql主机.
there is no mysql host with this parameters.
我检查了,一切似乎都很好.
I have checked and everything seems to be fine.
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();
此连接字符串有什么问题?
What is wrong with this connection string?
推荐答案
尝试以这种方式创建连接字符串:
try creating connection string this way:
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();
}
这篇关于如何形成正确的MySQL连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文