开发者社区> 问答> 正文

如何形成正确的MySQL连接字符串??mysql

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

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

没有使用此参数的mysql主机。

我检查了,一切似乎都很好。

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(); 此连接字符串有什么问题?

展开
收起
保持可爱mmm 2020-05-17 10:43:13 432 0
1 条回答
写回答
取消 提交回答
  • 尝试以这种方式创建连接字符串:

    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(); }来源:stack overflow

    2020-05-17 10:49:34
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
搭建电商项目架构连接MySQL 立即下载
搭建4层电商项目架构,实战连接MySQL 立即下载
PolarDB MySQL引擎重磅功能及产品能力盛大发布 立即下载

相关镜像