简体   繁体   English

如何将ASP.NET Web Api项目与托管的mySQL数据库连接?

[英]How to connect ASP.NET Web Api project with hosted mySQL database?

I am working with an api web application that is connected to mysql database using Visual Studio for Mac, my problem is that when I try to connect with hosted mysql db in the server I got the following exception "Unable to connect to any of the specified MySQL hosts." 我正在使用使用Visual Studio for Mac连接到mysql数据库的api Web应用程序,我的问题是当我尝试与服务器中的托管mysql db连接时,出现以下异常“无法连接到任何指定的MySQL主机。” However, when you connect to the local db mysql by using XAMPP, the connection works well and successfully. 但是,当您使用XAMPP连接到本地db mysql时,连接工作良好且成功。

here is my code. 这是我的代码。

in Program.cs 在Program.cs中

  public static MySqlConnection Conn(){

         var connection_string = "Data Source=MYSQL5015.site4now.net;Initial Catalog=db_a42078_holzqp;User ID=xxxxxx;Password=xxxxxxx";

        var conn = new MySqlConnection(connection_string);

        return conn;
    }

in ValuesController.cs 在ValuesController.cs中

public string Get(int id){ 
        conn = Program.Conn();
        try
        {
            conn.Open();
        }
        catch (MySqlException ex)
        {
            return "Failure: " + ex.Message.ToString();
        }
        if(conn.State == System.Data.ConnectionState.Open){
            return "opened";
        }else{
            return "closed";
        }}

Note : i also tried the standard mysql connection string like "SERVER:XXX;DATABASE=XXX;UID=XXXX;PWD=XXXX" and i got the same exception. 注意 :我还尝试了标准的mysql连接字符串,例如“ SERVER:XXX; DATABASE = XXX; UID = XXXX; PWD = XXXX”,并且遇到了相同的异常。

please help and thanks for all 请帮助并感谢大家

My first guess, is that the error you are getting indicates it couldn't find the host name (a DNS issue). 我的第一个猜测是,您收到的错误表明它找不到主机名( DNS问题)。 So, if the issue is around the server name in your connection string you can try 1 of 2 things: 因此,如果问题出在连接字符串中的服务器名称附近,则可以尝试以下两种方法之一:

  1. Point to a localhost or server, something you know is resolvable from your computer. 指向本地主机或服务器,您可以从计算机上解决您知道的问题。
  2. Use an IP address instead of server name 使用IP地址代替服务器名称

You can use your " Network Utility ", since you are using MAC, to discard if this is a DNS issue by ping ing the server name and it will probably give you the same error if it can't resolve the name. 您可以使用“ 网络实用程序 ”(因为您使用的是MAC)通过ping服务器名称来丢弃这是否是DNS问题,如果无法解析该名称,则可能会出现相同的错误。 This will also give you the IP Address if you want to use this to connect. 如果要使用此IP地址进行连接,还将为您提供IP地址。

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

相关问题 无法从IIS 7.5上托管的ASP.NET MVC项目连接到mysql数据库 - Unable to connect to mysql database from ASP.NET MVC project hosted on IIS 7.5 如何连接到ASP.NET Web应用程序中的MySql数据库? - How to connect to a MySql database in ASP.NET web application? asp.net web api与Mysql数据库 - asp.net web api with Mysql database ASP.NET Web API 连接MySQL数据库 - ASP.NET Web API Connecting to MySQL Database 如何在不使用EF的情况下连接ASP.NET Core Web API中的数据库? - How do I connect to database in ASP.NET Core Web API without using EF? 如何为引用Amazon AWS上托管的托管ASP.NET Web API的本地文件启用CORS - How to enable CORS for a local file that references a hosted ASP.NET Web API that is hosted on Amazon AWS ASP.NET Web API服务器代码在本地运行时连接到数据库,但在Azure上托管时不连接到数据库 - ASP.NET Web API server code connects to database when run locally, but not when hosted on Azure 如何使用mysql在asp.net中连接远程数据库? - How to connect remote database in asp.net using mysql? 如何在 ASP.NET Core Web API 项目中设置 cookie - How to set a cookie in a ASP.NET Core Web API project 在ASP.NET自托管Web API上配置SSL - Configuring SSL on ASP.NET Self-Hosted Web API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM