英文:
I get a C# SQL Connection error even though everything is correct
问题
我一直在尝试将我的C# Windows窗体连接到SQL服务器,但它总是给我这个错误:
错误:在建立与SQL Server的连接时发生了与网络或特定实例相关的错误。无法找到服务器或无法访问服务器。请验证实例名称是否正确,并且SQL Server已配置为允许远程连接。 (提供程序:命名管道提供程序,错误:40 - 无法打开到SQL Server的连接)
这是我的代码:(我用“Test”替换了用户名和密码。我使用remotemysql.com)
private void button1_Click(object sender, EventArgs e)
{
string connetionString;
SqlConnection cnn;
connetionString = @"Data Source=test;Initial Catalog=test;User ID=test;Password=test";
cnn = new SqlConnection(connetionString);
cnn.Open();
MessageBox.Show("连接已打开!");
cnn.Close();
}
英文:
I have been trying to connect my C# windows form to a SQL server and it always give me this error:
> Error: 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)
Here is my code: (I replaced the username and password with "Test". I use remotemysql.com)
private void button1_Click(object sender, EventArgs e)
{
string connetionString;
SqlConnection cnn;
connetionString = @"Data Source=test;Initial Catalog=test;User ID=test;Password=test";
cnn = new SqlConnection(connetionString);
cnn.Open();
MessageBox.Show("Connection Open !");
cnn.Close();
}
答案1
得分: 1
The server is not found, because there is no sql server.
服务器未找到,因为没有SQL服务器。
remotemysql.com is a mysql server.
remotemysql.com是一个MySQL服务器。
In order to connect to a mysql server you have to install Oracle's MySql.Data NuGet package. Then you can use the MySqlConnection instead of the SqlConnection class.
要连接到MySQL服务器,您必须安装Oracle的MySql.Data NuGet包。然后,您可以使用MySqlConnection而不是SqlConnection类。
英文:
The server is not found, because there is no sql server.
remotemysql.com is a mysql server.
In order to connect to a mysql server you have to install Oracle's MySql.Data NuGet package. Then you can use the MySqlConnection instead of the SqlConnection class.
答案2
得分: 0
"DbConnection": "Server=test.database.windows.net,1433;Initial Catalog=test_catalog;Persist Security Info=False;User ID=user;Password=password;MultipleActiveResultSets=False;TrustServerCertificate=False;Connection Timeout=30;"
如果连接到MySQL服务器,您应该添加正确的MySQL服务器连接并安装正确的.NET MySQL NuGet包。
英文:
You should add server as well into connetionString.
Smth like this(SQL server example):
"DbConnection": "Server=test.database.windows.net,1433;Initial Catalog=test_catalog;Persist Security Info=False;User ID=user;Password=password;MultipleActiveResultSets=False;TrustServerCertificate=False;Connection Timeout=30;"
If you connected to MySQL server, you should add correct connection for mySQL server and install correct nuget for mySQL for .net
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论