mysql - 连接到数据库时未处理 SocketException

标签 mysql .net

using System;
using MySql.Data.MySqlClient;
namespace test2 {
    class Program {
        static void Main(string[] args) {
            string connectionString =  "Server=localhost;Database=login;Uid=root;Pwd=TheDarkest;";
            MySqlConnection connection = new MySqlConnection(connectionString);
            connection.Open();
        }
    }
}

enter image description here

异常发生在connection.Open()处。尽管我到处寻找,但似乎找不到解决方案。

顺便说一句,我可以使用 python 连接到数据库。

最佳答案

添加try/catch block ,直到数据库连接在代码中关闭。

喜欢,

     static void Main(string[] args) {
      try{
        ......
        string connectionString ="Server=localhost;Database=login;Uid=root;Pwd=TheDarkest;"; 
        MySqlConnection connection = new MySqlConnection(connectionString);
        connection.Open();
       .......
      } 
      catch(Exception e)
      {
        ....
      }

关于mysql - 连接到数据库时未处理 SocketException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40626448/

相关文章:

php - 将 XML 数据加载到 MYSQL(MYSQL 版本 5.1)

php - SQLSTATE[42S02] : Base table or view not found: 1146 Table 'app.articles' doesn't exist (SQL: select * from `articles` )

php - SQLSTATE[42S22] : Column not found: 1054 Unknown column laravel 5. 1

c# - new List<T>() 和 new List<T>(0) 有区别吗

c# - 在 WCF 客户端/服务器应用程序中计算 UTC/本地日期时间转换的最佳方法

.net - 数据驱动的状态机应用

c# - 用于小型 .NET 项目的 IoC/DI

mysql - 如何将这段sql翻译成没有子查询的左连接?

php - 如何在没有mysql_fetch_array的情况下打印mysql结果RESOURCE_ID(7)

c# - 使用 Windows 编解码器解码音频文件