gpt4 book ai didi

C# - 无法连接到远程 MySQL 服务器

转载 作者:行者123 更新时间:2023-11-29 07:35:26 24 4
gpt4 key购买 nike

我的问题是我无法连接到我的网站远程 MySQL 服务器。我已阅读 stackoverflow.com 中的所有答案,但找不到正确的答案。这是我的 C# 代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
SqlConnection con;

string connectionString = @"Server=[IP adress];Port=3306;Database=[database];Uid=[user];Pwd=[pass];";
con = new SqlConnection(connectionString);
try
{
con.Open();
Console.WriteLine ("Connection Open ! ");
}
catch (Exception ex)
{
Console.WriteLine("Can not open connection ! ");
Console.WriteLine(ex.Message); //shows what error actually occurs
}
Console.ReadLine();
}
}
}

错误:

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)

有什么想法吗?

最佳答案

当连接到 MySQL 数据库时,我总是使用 MySQL 连接器,您可以在此处获取:https://dev.mysql.com/downloads/connector/net/6.9.html

您必须将 MySQL 命名空间导入到您的项目中,然后您可以使用 MySQLConnection 而不是 SQLConnection,据我所知,SQLConnection 仅适用于 MSSQL 服务器。

http://www.codeproject.com/Tips/423233/How-to-Connect-to-MySQL-Using-Csharp

关于C# - 无法连接到远程 MySQL 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30977864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com