AppHarbor:通过ASP.NET MVC 5 + Dapper连接到MySQL数据库

时间:2021-05-20 03:36:09

I'm trying to get an application up and running on AppHarbor. It's been smooth sailing for the most part, but today I'm having trouble connecting to my MySQL database.

我正在尝试在AppHarbor上运行并运行应用程序。大部分时间一帆风顺,但今天我无法连接到我的MySQL数据库。

Using the credentials AppHarbor provided me with, I successfully connected to the mySQL database instance using dbForge Studio Express, created the necessary SQL schema, and populated the tables with data. Now I'm trying to use the following C# code to do a simple query using Dapper:

使用AppHarbor提供的凭据,我使用dbForge Studio Express成功连接到mySQL数据库实例,创建了必要的SQL模式,并使用数据填充了表。现在我尝试使用以下C#代码使用Dapper进行简单查询:

var sqlConnStringBuilder = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["AppHarborMySql"].ConnectionString);

using (var db = new SqlConnection(sqlConnStringBuilder.ConnectionString))
{
    db.Open();
    // Do database query stuff

    return result;
}

But this is the error that I'm running into, when I try to open the database connection:

但是当我尝试打开数据库连接时,这是我遇到的错误:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

System.Data.dll中出现“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理

Additional information: 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)

附加信息:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供程序:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接)

This is how I've declared the connection string in Web.config:

这就是我在Web.config中声明连接字符串的方式:

<connectionStrings>
    <add name="AppHarborMySql" connectionString="server=MYDOMAIN.mysql.sequelizer.com;database=MYDBNAME;uid=MYUSERNAME;pwd=MYPASSWORD" providerName="System.Data.SqlClient" />
</connectionStrings>

Am I missing something obvious? I'm using ASP.NET MVC 5.x and the latest version of Dapper... thanks in advance!

我错过了一些明显的东西吗我正在使用ASP.NET MVC 5.x和最新版本的Dapper ...提前感谢!

1 个解决方案

#1


0  

And of course, despite having spent the better part of an hour investigating, I discover the answer immediately after posting the question.

当然,尽管我花了大约一小时的时间进行调查,但在发布问题后我立即发现答案。

For anyone else who may run across the same problem: I added the MySql.Web NuGet package to my MVC project, added using MySql.Data.MySqlClient; to the top of my page, and then changed my code to point from SqlConnection to MySqlConnection. Here's what my working code block now looks like:

对于任何可能遇到同样问题的人:我将MySql.Web NuGet包添加到我的MVC项目中,使用MySql.Data.MySqlClient添加;到我的页面顶部,然后将我的代码更改为从SqlConnection指向MySqlConnection。这是我的工作代码块现在的样子:

using (var db = new MySqlConnection(ConfigurationManager.ConnectionStrings["AppHarborMySql"].ConnectionString))
{
    db.Open();
    // Do database query stuff

    return result;
}

#1


0  

And of course, despite having spent the better part of an hour investigating, I discover the answer immediately after posting the question.

当然,尽管我花了大约一小时的时间进行调查,但在发布问题后我立即发现答案。

For anyone else who may run across the same problem: I added the MySql.Web NuGet package to my MVC project, added using MySql.Data.MySqlClient; to the top of my page, and then changed my code to point from SqlConnection to MySqlConnection. Here's what my working code block now looks like:

对于任何可能遇到同样问题的人:我将MySql.Web NuGet包添加到我的MVC项目中,使用MySql.Data.MySqlClient添加;到我的页面顶部,然后将我的代码更改为从SqlConnection指向MySqlConnection。这是我的工作代码块现在的样子:

using (var db = new MySqlConnection(ConfigurationManager.ConnectionStrings["AppHarborMySql"].ConnectionString))
{
    db.Open();
    // Do database query stuff

    return result;
}