using System.Data.SqlClient; string queryString = "SELECT COUNT(*) FROM Employees"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(queryString, connection); connection.Open(); int count = await command.ExecuteScalarAsync(); Console.WriteLine("Total number of employees: {0}", count); }
using System.Data.SqlClient; string queryString = "SELECT FirstName, LastName FROM Employees WHERE EmployeeID = @ID;"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(queryString, connection); command.Parameters.AddWithValue("@ID", 1001); connection.Open(); SqlDataReader reader = await command.ExecuteReaderAsync(); while (reader.Read()) { Console.WriteLine(reader["FirstName"] + " " + reader["LastName"]); } reader.Close(); }This code example connects to a SQL Server database and executes a query to return the first and last name of an employee with the ID of 1001. The query parameters are set using the AddWithValue method. The data is retrieved using a SqlDataReader object and printed to the console. The System.Data.SqlClient package library is used in both examples to provide the necessary classes to connect to and interact with a SQL Server database.