我最近在 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;
}
这是我正在尝试做的事情:
网络配置:
<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>
数据库:
public class Db
{
public static IDbConnection GetOpenConnection()
{
var connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
var connection = new SqlConnection(connString);
//---- Hangs Here ----//
connection.Open();
return connection;
}
}
会员控制器:
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 的新手,所以它很可能是我忽略或忘记添加的东西。谢谢!