简体   繁体   中英

Cannot connect to mysql database in .NET MVC3

I've having some trouble in connecting mvc 3 application in .net. I know that mysql database instance has to be created before using web.config file to connect the it. My connectionStrings as below:

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

and I've got a database name call epmsDb created in Server explorer. Then I tried to get the connectionString in the code using this :

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

Every time I tried to access the database, I threw this exception:

   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)

I've been trying to overcome this for a week now. Does anyone have ideas why this occurres?

Your example tries to connect to an MS SQL database. To connect to a MySQL DB you might want to use the MySQL connector

Then change the ProviderName property:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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