Saltar al contenido principal
Mantén tu empresa abierta durante el COVID-19Obtener más información
Llámanos
Números telefónicos y horarios
Solicitar una llamada

Evita la espera. Haz una solicitud y te devolveremos la llamada

Solicitar una llamada
Centro de Ayuda

Explora nuestros recursos de ayuda en línea

BlogAyuda

Windows Hosting (Plesk) Ayuda

Connecting to a MySQL Database Using ASP.NET

This example describes using ASP.NET/MySql.Data to connect to a MySQL Database. A few important things you need before you get started:
  • Knowledge of computer programming.
  • Microsoft® Visual Studio .NET.
  • MySql Connector/NET on your development computer. For more information, click here.
  • Knowledge of MySql and specifically the MySql.Data Namespace.
  • A setup MySql Database.

To Connect to a MySQL Database Using ASP.NET

  1. Find your database's connection strings (Plesk).

    Note: Change the your password value to your real database password value.

  2. Using Microsoft Visual Studio .NET create an ASP.NET Project.
  3. Add a reference to MySql.Data.dll.
  4. Replace the value in the following code with your_ConnectionString with your database information.
  5. Insert the following code into your project including your modified your_ConnectionString value:

    Note: If your MySql database was created with the Allow Direct Database Access enabled, you can connect to the database from your development computer. If you did not enable Allow Direct Database Access, your MySql is in a secure environment and you cannot connect to the database from your development computer. A connection can only be successful when your code is deployed to the hosting site.

  6. 
    

    MySql.Data.MySqlClient.MySqlConnection mySqlConnection = new
    MySql.Data.MySqlClient.MySqlConnection();
    mySqlConnection.ConnectionString = “your_ConnectionString”;

    try
    {
    mySqlConnection.Open();

    switch (mySqlConnection.State)
    {
    case System.Data.ConnectionState.Open:
    // Connection has been made
    break;
    case System.Data.ConnectionState.Closed:
    // Connection could not be made, throw an error
    throw new Exception("The database connection state is Closed");
    break;
    default:
    // Connection is actively doing something else
    break;
    }

    // Place Your Code Here to Process Data //
    }
    catch (MySql.Data.MySqlClient.MySqlException mySqlException)
    {
    // Use the mySqlException object to handle specific MySql errors
    }
    catch (Exception exception)
    {
    // Use the exception object to handle all other non-MySql specific errors
    }
    finally
    {
    // Make sure to only close connections that are not in a closed state
    if (mySqlConnection.State != System.Data.ConnectionState.Closed)
    {
    // Close the connection as a good Garbage Collecting practice
    mySqlConnection.Close();
    }
    }

Relacionado con la comunidad

mikeb1's Avatar
Connecting to mySQL database

5 Respuestas

Last posted about 1 year ago.

overkill452's Avatar
Connecting to a Remote MySQL Database

21 Respuestas

Last posted about 3 years ago.

codyz's Avatar
Visual Studio - Publish Database without Web Deploy

1 Respuestas

Last posted over 3 years ago.

heta's Avatar
Trouble connecting MySql Database in my asp.net form

6 Respuestas

Last posted almost 3 years ago.

SamHobbs's Avatar
SQL Server Connection Strings in web.config

5 Respuestas

Last posted about 4 years ago.

¿No encuentras lo que estás buscando? Busca en la comunidad