Dapper找不到打开的连接MVC3

时间:2013-02-23 21:54:44

标签: mysql asp.net-mvc-3 asp.net-mvc-4 dapper

我最近在MVC3中创建了一个新项目,并将MySQL数据库连接到它没有问题。我能够创建用户和角色(使用NuGet的Universal Providers / SecurityGuard)。现在我正在尝试使用Dapper为我的编辑页面运行查询并获得SQL异常:

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

YSOD:

The system cannot find the file specified.
         var connection = new SqlConnection(connString);

         connection.Open();
         return connection;
     }

这是我正在尝试做的事情:

的web.config:

  <connectionStrings>
     <add name="DefaultConnection" connectionString="Database=[database];Data Source=localhost;Uid=[username];Pwd=[password];" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>

<membership defaultProvider="MySqlMembershipProvider">
  <providers>
    <clear />
    <add name="MySqlMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.6.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" autogenerateschema="true" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" />
  </providers>
</membership>
<profile defaultProvider="MySQLProfileProvider">
  <providers>
    <clear />
    <add name="MySQLProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider, MySql.Web, Version=6.6.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
  </providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
  <providers>
    <clear />
    <add connectionStringName="DefaultConnection" applicationName="/" autogenerateschema="True" name="MySQLRoleProvider" type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.6.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
  </providers>
</roleManager>

Db.cs:

public class Db
{
    public static IDbConnection GetOpenConnection()
    {
        var connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

        var connection = new SqlConnection(connString);

        //---- Hangs Here ----//
        connection.Open();
        return connection;
    }
}

MembershipController:

var connection = Db.GetOpenConnection();
    {
            const string conn = @"SELECT id
                               FROM my_aspnet_users
                               WHERE name = @UserName";

            var result = connection.Query<UserViewModel>(conn, new { userName }).Single();

            viewModel.Id = result;
        }

我从未进入控制器,因为它在尝试打开连接时挂起。我刚开始使用MySQL,所以它很可能是我忽略或忘记添加的东西。谢谢!

2 个答案:

答案 0 :(得分:4)

弄清楚我的问题。我试图使用

var connection = new SqlConnection(connString);
MySQL不支持

。相反,我发现Connecting an ASP.NET MVC application to MySQL使用了正确的连接器。

using (var connection = new MySqlConnection(ConnectionString))
using (var cmd = connection.CreateCommand())
{
connection.Open();
cmd.CommandText = "SELECT name FROM foo;";
using (var reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        string name = reader.GetString(reader.GetOrdinal("name"));
        // TODO: do something with the name ...
    }
}

}

答案 1 :(得分:0)

在它上面的代码看起来很好,我怀疑这是连接字符串的问题。基于最近的MySql项目,我在连接字符串上的工作略有不同。您可以尝试以下(通知数据源已更改为服务器)

<connectionStrings>
     <add name="DefaultConnection" connectionString="Server=localhost;Database=[database];Uid=[username];Pwd=[password];" providerName="MySql.Data.MySqlClient" />
</connectionStrings>

此处还有一些MySql ConnectionStrings:http://www.connectionstrings.com/mysql