0

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

<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)

一个星期以来,我一直在努力克服这个问题。有谁知道为什么会发生这种情况?

4

1 回答 1

1

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

然后更改 ProviderName 属性:

<connectionStrings>
<add name="epmsDb" 
  connectionString="Server=localhost;Database=database1;
                     Uid=root;Pwd=mypassword"
  providerName="MySql.Data.MySqlClient" />
</connectionStrings>
于 2012-10-13T12:28:00.953 回答