using System.Data.SqlClient; string connectionString = "Server=myServerAddress;Database=myDatabase;User Id=myUsername;Password=myPassword;"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); // database operations }
using System.Data.SqlClient; string connectionString = "Server=myServerAddress;Database=myDatabase;User Id=myUsername;Password=myPassword;"; string sqlQuery = "SELECT * FROM Customers"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand(sqlQuery, connection)) { SqlDataReader dataReader = command.ExecuteReader(); while (dataReader.Read()) { Console.WriteLine($"Customer ID: {dataReader["CustomerID"]}, Company Name: {dataReader["CompanyName"]}"); } dataReader.Close(); } }In this example, we execute the SQL query to select all customers from the Customers table and print their IDs and company names. The package library used in these examples is System.Data.SqlClient, which is a part of .NET Framework and .NET Core platforms.