简体   繁体   中英

C# WebSite connection to a mySQL database

I want to connect my WebSite to a local database. My connection String is the following:

     <connectionStrings>

<add name ="localhost"

 connectionString="server=localhost;user id=root;persistsecurityinfo=True;database=doctorappointments" 
     providerName="MySql.Data.MySqlClient"
 />

I can connect from MySql Workbench and also from Visual Studio if I add a new connection in Server Explorer. The connection String from above is the same as the one from server Explorer. I checked that my server is running, I also put a firewall rule for the port that it connects to.

This how I am using the connectionString:

      using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["localhost"].ConnectionString))
        {

        con.Open();
        Response.Redirect("MainPage.aspx");

        }

And on the con.Open() it gives me this error:

An exception of type 'System.Data.SqlClient.SqlException' occurred in >System.Data.dll but was not handled in user code

Additional information: 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)

and also:

  • ServerVersion 'con.ServerVersion' threw an exception of type 'System.InvalidOperationException' string {System.InvalidOperationException}

I am new to this and I have tried everything I found online. I do not know what am I doing wrong...

SqlConnection is for Sql Server.

Make sure you have installed MySql.Data via nuget package and use MySqlConnection instead.

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