简体   繁体   English

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

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

My problem is that I can't connect to my website remote MySQL server. 我的问题是我无法连接到我的网站远程MySQL服务器。 I have read all answers in stackoverflow.com, but I can't find right answer. 我已经在stackoverflow.com中阅读了所有答案,但是找不到正确答案。 Here's my C# code: 这是我的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();
    }
}
}

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)

Any ideas? 有任何想法吗?

When connecting to a MySQL-Database I always used the MySQL Connector you can get here: https://dev.mysql.com/downloads/connector/net/6.9.html 连接到MySQL数据库时,我一直使用MySQL连接器,您可以在这里获得: https : //dev.mysql.com/downloads/connector/net/6.9.html

You have to import the MySQL namespaces to your project and then you can use the MySQLConnection instead of the SQLConnection that is, as far as I know, only for MSSQL servers. 您必须将MySQL命名空间导入到您的项目中,然后可以使用MySQLConnection代替SQLConnection,据我所知,SQLConnection仅用于MSSQL服务器。

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

try the following 尝试以下

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

instead of 代替

 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