A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.

你说的曾经没有我的故事 提交于 2021-02-10 14:51:21

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/53689447/a-network-related-or-instance-specific-error-occurred-while-establishing-a-conne

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!