无法连接到.NET MVC3中的mysql数据库

时间:2012-10-13 11:34:58

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

我在连接.net中的mvc 3应用程序时遇到了一些麻烦。我知道在使用web.config文件连接它之前必须创建mysql数据库实例。我的 connectionStrings如下:

<connectionStrings>
<add name="epmsDb" 
  connectionString="Server=localhost;Database=database1;
                     Uid=root;Pwd=mypassword"
  providerName="System.Data.SqlClient" />
</connectionStrings>

我在Server explorer中创建了一个数据库名称调用epmsDb。然后我尝试使用以下代码获取代码中的connectionString:

  string connectionString =
          ConfigurationManager.ConnectionStrings["epmsDb"].ConnectionString;
        context = new DataContext(connectionString);
        usersTable = context.GetTable<UserObj>();

每当我尝试访问数据库时,我都抛出了这个异常:

   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)

我一直试图克服这个问题一个星期了。有没有人知道为什么会这样?

1 个答案:

答案 0 :(得分:1)

您的示例尝试连接到MS SQL数据库。要连接到MySQL数据库,您可能需要使用MySQL connector

然后更改ProviderName属性:

<connectionStrings>
<add name="epmsDb" 
  connectionString="Server=localhost;Database=database1;
                     Uid=root;Pwd=mypassword"
  providerName="MySql.Data.MySqlClient" />
</connectionStrings>