1

I am running a local mysql database on my computer using wampserver. I have made a c# console application that I want to use to update values in a table. However I am having issues connecting to the database. My password is an empty string.

string user = "John Doe";

string queryString = "UPDATE users SET is_awesome=1 WHERE user=@a1";
string connectionString = "Server=localhost;Database=mydatabase;User Id=root;Password=;";

SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand cmd = new SqlCommand(queryString, connection);
cmd.Parameters.Add("a1", user);
cmd.ExecuteNonQuery();

However when I attempt to run this code I get the following error:

System.Data.SqlClient.SqlException 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)' Inner Exception Win32Exception: The system cannot find the file specified

I believe my database allows remote connections because I am able to connect to it using a different php application I am running. How can I connect to a sql database from c# and run my query?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • I recommend testing connecting from Workbench on the machine the above code is running on. – ProgrammingLlama Dec 09 '18 at 04:54
  • 1
    You have a provider problem. You are going to connect to MySql with SQL Server Connection provider – Yashar Aliabbasi Dec 09 '18 at 04:55
  • SqlConnection and SqlCommand are for Microsoft SQL Server, not MySql. Put "My" in front of each, when you have the correct references in place. Also note that the connection and command are both IDisposable so each should be in a `using` block. – Richardissimo Dec 09 '18 at 05:07

1 Answers1

0

You must connect to MySql witch is specific for MySql Like MySqlConnection for more look this tutorial or this

Yashar Aliabbasi
  • 2,663
  • 1
  • 23
  • 35