string connectionString = "Data Source=myServer;Initial Catalog=myDatabase;Integrated Security=True"; string sql = "INSERT INTO Customers (FirstName, LastName) VALUES ('John', 'Doe')"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand(sql, connection)) { command.ExecuteNonQuery(); } }
string connectionString = "Data Source=myServer;Initial Catalog=myDatabase;Integrated Security=True"; string sql = "UPDATE Customers SET FirstName='Jane' WHERE LastName='Doe'"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand(sql, connection)) { command.ExecuteNonQuery(); } }Here, we are updating an existing customer in the "Customers" table by changing their first name to "Jane". The ExecuteNonQuery method is used to execute the SQL statement and make the update. Overall, ExecuteNonQuery is a useful method that can help you execute SQL statements that do not return any result data. It is part of the SQLHandler package library for C#.