繁体   English   中英

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

[英]C# - Can't connect to remote MySQL server

我的问题是我无法连接到我的网站远程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

尝试以下

string connectionString = @"Server=[IP adress]:3306;Database=[database];Uid=[user];Pwd=[pass];";

代替

 string connectionString = @"Server=[IP adress];Port=3306;Database=[database];Uid=[user];Pwd=[pass];";

暂无
暂无

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

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