using System.Data.SqlClient; string connectionString = "Data Source=myServerAddress;" + "Initial Catalog=myDataBase;" + "Integrated Security=SSPI;"; SqlConnection connection = new SqlConnection(connectionString); connection.Open();
using MySql.Data.MySqlClient; string connectionString = "server=localhost;user id=root;password=root;database=mydatabase"; MySqlConnection connection = new MySqlConnection(connectionString); connection.Open();This example demonstrates how to create a MySqlConnection object and open a connection to a MySQL database using a connection string. The MySqlConnection class is from the MySql.Data.MySqlClient package. In both examples, once the connection is open, you can execute SQL queries or procedures using a SqlCommand or MySqlCommand object, respectively. Before closing the connection, it is important to use a try-catch block to handle any exceptions that may occur.