开发者

Connection failed with MySQL .NET Connector

开发者 https://www.devze.com 2022-12-28 05:01 出处:网络
I cannot connect to MySQL it fails on line connection.Open(), is there something wrong with my code ?

I cannot connect to MySQL it fails on line connection.Open(), is there something wrong with my code ?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using Syste开发者_如何学Cm.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();

        }
    }
}


this is the string format I use to connect via the MySql.Data.dll version 6.1.2.0

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

so your connection string should be

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


You need to specify the Port as a separate argument in the connection string and it looks like the password key is "Pwd" instead of "Password".

See connectionstrings.com for help on the exact syntax.

0

精彩评论

暂无评论...
验证码 换一张
取 消