-5

I'm trying to get a connection to my local MySQL database, I'm not sure what all the parameters are. Hopefully someone can help.

My code:

string connetionString = "Server = 127.0.0.1; Database = name of database; User Id = root, Password = password of database";

SqlConnection cnn = new SqlConnection(connetionString);

try
{
    cnn.Open();
    MessageBox.Show("Connection Open ! ");
    cnn.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString(), "titel", MessageBoxButtons.OK);
    MessageBox.Show("Can not open connection ! ");
}

Thanks

Shadow
  • 33,525
  • 10
  • 51
  • 64
NielsSchutte
  • 55
  • 1
  • 10
  • What error message are you getting? – Peter Abolins Sep 28 '17 at 07:47
  • [This page](https://stackoverflow.com/a/15632342/6741868) has some example and help on connection strings. – Keyur PATEL Sep 28 '17 at 07:48
  • Is this SQL Server or MySQL database? From the connection string seems you're trying to use MySQL root user, check https://www.connectionstrings.com/mysql/ for details. – Tetsuya Yamamoto Sep 28 '17 at 07:49
  • Mysql database using xampp – NielsSchutte Sep 28 '17 at 07:50
  • 6
    It takes 12 seconds to do a Google Search, and 4 minutes to create a SO question. The former only wastes your time, the latter wastes a lot of people's time. https://www.dotnetperls.com/sqlconnection – Peter Abolins Sep 28 '17 at 07:50
  • 1
    You're using wrong connection string & wrong instance. You should use `MySql.Data.MySqlConnection` with connection string example given on the link. – Tetsuya Yamamoto Sep 28 '17 at 07:50
  • Error: Server is not open or you dont have acces. – NielsSchutte Sep 28 '17 at 07:52
  • You have tagged the question with `sql-server` and you are saying that the RDBMS you are using is `mysql`. These two are not the same thing. – Peter Abolins Sep 28 '17 at 07:54
  • The connection string should be like this: `Server=localhost;Database=[DB name];Uid=root;Pwd=[password];`. MySQL & SQL Server use different way to connect, `SqlConnection` should only used for SQL Server. – Tetsuya Yamamoto Sep 28 '17 at 07:55
  • I cant figure it out, if database name = test and uid = root(right?) and password is test aswell, the connection string is as follows: string connectionString = "Data Source=127.0.0.1;Initial Catalog=test;User ID=root;Password=test";? The last password, is this the password of the database or from the UID? – NielsSchutte Sep 28 '17 at 07:57
  • I've tried this: Server = 127.0.0.1; Database = name of database; User Id = root, Password = password of database. It's not working.. – NielsSchutte Sep 28 '17 at 08:00
  • @NielsSchutte That's SQL Server connection string, it's not to be confused with MySQL connection string I presented before (you can't use `Initial Catalog` parameter in MySQL). Install `MySql.Data` package & use `MySql.Data.MySqlConnection` to connect. – Tetsuya Yamamoto Sep 28 '17 at 08:01
  • @TetsuyaYamamoto check topic again, i've edit it. Still not working tho.. – NielsSchutte Sep 28 '17 at 08:02
  • 1
    If you're using **MySQL**, then you **must use** the `MySqlConnection` and `MySqlCommand` classes - ***NOT*** the `SqlConnection` and `SqlCommand` - those are **SQL Server ONLY** – marc_s Sep 28 '17 at 08:03
  • 2
    @NielsSchutte As @marc_s & I mentioned before, I suggest you to distinguish between SQL Server & MySQL connection string, don't mix them both. MySQL uses `MySql.Data.MySqlClient` namespace where SQL Server uses `System.Data.SqlClient` namespace, they're *not same thing*. – Tetsuya Yamamoto Sep 28 '17 at 08:06
  • I've got it, had to use Mysql.Data.MysqlClient. Question is answered. – NielsSchutte Sep 28 '17 at 08:17

1 Answers1

-2

Can you try setting up your connection string as

server=127.0.0.1;database={dbname};uid={};password=password@1;Max Pool Size=800;
  • Welcome to Stack Overflow! Please read [How do I write a good answer?](http://stackoverflow.com/help/how-to-answer) before attempting to answer more questions. –  Sep 28 '17 at 13:43