2013-02-23 118 views
2

我最近在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合作的新手,因此很可能是我忽視或忘記添加的東西。謝謝!

+0

當你說它掛起,你得到任何錯誤呢?它在表面上看到連接字符串的問題。 – LiamB 2013-02-24 20:24:20

回答

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 ... 
    } 
} 

}

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