0

I have to connect my Database to an existing code in .NET. My Database is a MySql DB in PhpMyAdmin.

I am using System.Data.SqlClient in .NET and it seems like I cant get the connection String right for the connection function

private string _connetionString = @"Data Source=localhost;Initial Catalog=Testdb;"
SqlConnection _connection;

public DatabaseService()
{
  _connection = new SqlConnection(_connetionString);
}

Everytime I start my .Net-Service it needs a long time to and eventually the connection fails.

2
  • Try using that connection information and see if you can log-in to the db from another tool. That will help you out. Commented Jun 22, 2019 at 16:22
  • Ah damn, this is not the right information. This is the information from the other server. The problem is, that I am trying to connect some code that a colleague used on his machine, now im trying to use it on my machine with my own setup. Commented Jun 22, 2019 at 16:24

2 Answers 2

2

SqlConnection class (the whole namespace System.Data.SqlClient) is for connection to Microsoft SQL Server. You are using MySQL, so you need to use System.Data.OleDb namespace (OleDbConnection).

How connestion string for MySql look you can check here: https://www.connectionstrings.com/mysql-connector-net-mysqlconnection/

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

Comments

1

That's not the right connection string syntax for MySQL. The sample you've posted looks to be for MSSQL. For MySQL you would need something like:

"server=localhost;uid=username;pwd=password;database=Testdb"

MySQL API Reference for .NET

1 Comment

Finally. This worked for me. I guess Stano Pet'kos would have worked somehow as well, but this works for me. Thanks a lot. I didn't think of checking the manual of MySQL as well. Smh...

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.