using System.Data.SqlClient; using System.Threading.Tasks; public async Task> GetNamesAsync() { List
names = new List (); using (SqlConnection connection = new SqlConnection("connectionString")) { await connection.OpenAsync().ConfigureAwait(false); using (SqlCommand command = new SqlCommand("SELECT Name FROM Customers", connection)) { using (SqlDataReader reader = await command.ExecuteReaderAsync().ConfigureAwait(false)) { while (await reader.ReadAsync().ConfigureAwait(false)) { names.Add(reader.GetString(0)); } } } } return names; }
using System.Data.SqlClient; public void UpdateCustomer(string customerId, string newContactName) { using (SqlConnection connection = new SqlConnection("connectionString")) { connection.Open(); using (SqlCommand command = new SqlCommand("UPDATE Customers SET ContactName = @newContactName WHERE CustomerID = @customerId", connection)) { command.Parameters.AddWithValue("@newContactName", newContactName); command.Parameters.AddWithValue("@customerId", customerId); command.ExecuteNonQuery(); } } }In this example, we create a method that updates the contact name of a customer in the database. We open a connection to the database using the Open method, and then create a SqlCommand object with a parameterized UPDATE statement. We use the AddWithValue method to set the values for the parameters in the query, and then execute the query using the ExecuteNonQuery method. Package/library: System.Data.SqlClient