using System.Data.SqlClient; using System.Data.Common; // create a SqlConnection and open it SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=myDb;Integrated Security=True"); conn.Open(); // create a DbCommand with a SQL INSERT statement string insertSql = "INSERT INTO myTable (col1, col2) VALUES (@param1, @param2)"; DbCommand cmd = new SqlCommand(insertSql, conn); // set the parameters cmd.Parameters.AddWithValue("@param1", "value1"); cmd.Parameters.AddWithValue("@param2", "value2"); // execute the command int rowsAffected = cmd.ExecuteNonQuery(); // close the connection conn.Close();
using System.Data.OleDb; using System.Data.Common; // create an OleDbConnection and open it OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=myDb.mdb"); conn.Open(); // create a DbCommand with a SQL DELETE statement string deleteSql = "DELETE FROM myTable WHERE col1 = ?"; DbCommand cmd = new OleDbCommand(deleteSql, conn); // set the parameter cmd.Parameters.AddWithValue("?", "value1"); // execute the command int rowsAffected = cmd.ExecuteNonQuery(); // close the connection conn.Close();In this example, we use an OleDbConnection to connect to a Microsoft Access database. We create a DbCommand object with a SQL DELETE statement and set the parameter. Then we execute the command using ExecuteNonQuery and store the number of rows affected. Finally, we close the connection. Package library: System.Data.OleDb