如何形成正确的 MySQL 连接字符串? [英] How to form a correct MySQL connection string?

查看:49
本文介绍了如何形成正确的 MySQL 连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using C# and I am trying to connect to the MySQL database hosted by 00webhost.

我在行 connection.Open() 上遇到错误:

I am getting error on the line 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆