我最近在MVC3中創建了一個新項目,並且沒有任何問題地將MySQL數據庫連接到它。我能夠創建用戶和角色(使用NuGet中的Universal Providers/SecurityGuard)。現在,我試圖使用Dapper爲我的編輯頁面運行查詢並獲取SQL異常:Dapper無法找到打開連接MVC3
調試: 建立到SQL Server的連接時發生網絡相關或實例特定的錯誤。服務器未找到或無法訪問。驗證實例名稱是否正確,並將SQL Server配置爲允許遠程連接。 (provider:命名管道提供程序,error: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合作的新手,因此很可能是我忽視或忘記添加的東西。謝謝!
當你說它掛起,你得到任何錯誤呢?它在表面上看到連接字符串的問題。 – LiamB 2013-02-24 20:24:20