MySQL .NET Connector连接失败

时间:2010-04-21 19:35:51

标签: 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();

        }
    }
}

2 个答案:

答案 0 :(得分:2)

您需要在连接字符串中将Port指定为单独的参数,并且密码密钥看起来像是“Pwd”而不是“Password”。

有关确切语法的帮助,请参阅connectionstrings.com

答案 1 :(得分:2)

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

server = {0};用户ID = {1};密码= {2};数据库= {3}; port = {4}

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

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