0

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 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();

        }
    }
}
3
  • 1
    The exception will give you hints about what's wrong. What does the exception say ? Commented Apr 21, 2010 at 19:37
  • Can you maybe show us the exception message? Commented Apr 21, 2010 at 19:37
  • And what version of the Connector are you using? Recent versions use connections strings like SQL Server uses... (User ID instead of UID) Commented Apr 21, 2010 at 19:58

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks will try. I can't mark two answers as good one but I selected you as you have less points :)
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.