繁体   English   中英

在 C# 中连接到外部 SQL 数据库

[英]Connect to External SQL Database in C#

我在外部服务器上安装了 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 查询数据库。

有任何想法吗?

您将需要MySQL驱动程序:

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

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

MySqlConnection connection = new MySqlConnection(connectionString);

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

您不能使用SqlConnection对象连接到 MySQL 数据库,您应该在导入其 dll 后使用MySqlConnection

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM