Brian Warshaw Brian Warshaw - 1 month ago 24
SQL Question

C# - SQLClient - Simplest INSERT

I'm basically trying to figure out the simplest way to perform your basic insert operation in C#.NET using the SqlClient namespace.

I'm using

SqlConnection
for my db link, I've already had success executing some reads, and I want to know the simplest way to insert data. I'm finding what seem to be pretty verbose methods when I google.

Answer
using (var conn = new SqlConnection(yourConnectionString))
{
    var cmd = new SqlCommand("insert into Foo values (@bar)", conn);
    cmd.Parameters.AddWithValue("@bar", 17);
    conn.Open();
    cmd.ExecuteNonQuery();
}