Search code examples
c#sqlconnection

What to fill into SqlConnectionString


I'm trying to connect my C#-Program to a MySQL DB which is running on my OSX. I'm getting this errormessage:

System.Data.SqlClient.SqlException
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)"

So it can't find the server, what means something in the Connectionstring must be wrong. Here's it:

using(SqlConnection conn = new SqlConnection()) {
            conn.ConnectionString = "Server=localhost;DataBase=Studentenverwaltung;Trusted_Connection=true";
            conn.Open();
        }

I tried to use the Port of the Server instead of localhost, but it did not work, too. Studentenverwaltung is the name of my DB.

Can somebody help me out to spot the mistake in my code? Thank you?


Solution

  • I think you should be using MySqlConnnection and not SqlConnection

    Your connection string will need to contain a username and password, as Trusted Authentication will not work.

    try:

    using(MySqlConnection conn = new MySqlConnection()) {
        conn.ConnectionString = "Server=localhost:3306;DataBase=Studentenverwaltung;uid=username;pwd=password";
        conn.Open();
    }
    

    I don't have a MAC, but that's how I'd do it using Windows.