繁体   English   中英

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

[英]Cannot connect to mysql database in .NET MVC3

我在连接.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>

我在服务器资源管理器中创建了一个名为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)

我已经尝试克服这个问题已有一个星期了。 有谁知道为什么会这样?

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

然后更改ProviderName属性:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM