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

前端之家收集整理的这篇文章主要介绍了c# – 如何形成一个正确的MySQL连接字符串?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用C#,我正在尝试连接到由00webhost托管的 MySQL数据库.

我在线路上遇到错误.打开():

there is no MysqL host with this 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#相关文章