【问题标题】:Connect to External SQL Database in C#在 C# 中连接到外部 SQL 数据库
【发布时间】:2012-10-11 12:17:11
【问题描述】:

我在外部服务器上设置了 SQL 服务器和数据库(为此,我们将域名称为“hello.com”),我想通过 C# 程序连接到该服务器。到目前为止,我有这个(所有服务器/数据库细节都与真实的不同):

private static void SetupSQL()
{
    string connectionString = "server=hello.com; database=db1; uid=user1; pwd=xxxxx;";
    connection = new SqlConnection();
    connection.ConnectionString = connectionString;
    try
    {
        connection.Open();
        Console.WriteLine("Connected");
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message.ToString());
    }
}

这给了我一个错误信息:

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)

我已经检查了所有的连接字符串,并且我被允许远程访问,因为我现在在同一台计算机上打开 SQLWorkbench 查询数据库。

有什么想法吗?

【问题讨论】:

  • 如果数据库是sql server,为什么这个标签为mysql?
  • 使用 connectionString 中的名称检查您的 sqlInstance
  • 这是一个 MySQL 服务器。抱歉,如果我没有说清楚。
  • 你读过这个:blog.sqlauthority.com/2009/05/21/… 吗? 编辑:不,不清楚,因为SqlConnection 显然是一个SQL-Server 连接。
  • SQL SERVER 状态是否正在运行?阅读这篇文章Here,如果你使用 MySQL,你应该安装MySQL Connector

标签: c# mysql sql .net


【解决方案1】:

您需要MySQL 驱动程序:

http://dev.mysql.com/downloads/connector/net/

然后您可以使用MySqlConnection 连接类进行连接。

MySqlConnection connection = new MySqlConnection(connectionString);

http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL

【讨论】:

  • @AllFallD0wn - 很高兴为您提供帮助 :)
【解决方案2】:

你不能使用SqlConnection对象连接MySQL数据库,你应该在导入它的dll后使用MySqlConnection来代替

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多