c# - MySQL .NET Connector 连接失败

标签 c# mysql

我无法连接到 MySQL,它在线连接失败。打开(),我的代码有问题吗?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace MySQLConnection
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            string MyConString = "SERVER=localhost:3316;" +
                "DATABASE=mydb;" +
                "UID=user;" +
                "PASSWORD=password;";
            MySqlConnection connection = new MySqlConnection(MyConString);
            connection.Open();
            // ...
            connection.Close();

        }
    }
}

最佳答案

这是我用来通过 MySql.Data.dll 版本 6.1.2.0 连接的字符串格式

server={0};user id={1};password={2};database={3};port={4}

所以你的连接字符串应该是

server=localhost;user id=user;password=密码;database=mydb;port=3316

关于c# - MySQL .NET Connector 连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2686008/

相关文章:

c# - 使用C#正则表达式替换XML元素内容

c# - 将 XML 字符串转换为对象

c# - 从 C# 中的对象数组中获取最大值

mysql - 基于行值的不同选择语句

c# - 计算 DateTimes 集合之间的平均 TimeSpan

c# - 将解决方案从 Visual Studio 2005 转换为 2008

php - 在 Eloquent/Laravel 中使用子查询

php - 在php中的url中传递变量

Mysql 5.5 查询优化

mysql - 使用 mysql/nodejs 返回更新的行