Skip to content

Instantly share code, notes, and snippets.

@bariloce
Created February 19, 2015 10:23
Show Gist options
  • Save bariloce/eb346ff26a51a92e807a to your computer and use it in GitHub Desktop.
Save bariloce/eb346ff26a51a92e807a to your computer and use it in GitHub Desktop.
SQL insert from C# - not compiled, just written.
using(var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connSpionshopString"].ConnectionString))
{
connection.Open();
string sql = "INSERT INTO dbo.Table(userId, imagePath, userComments, dateCommented) VALUES (@userId, @imagePath, @userComments, @dateCommented)";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.Add("@userId", SqlDbType.Int).value = userId
cmd.Parameters.Add("@imagePath", SqlDbType.Varchar, 50).value = imagePath;
cmd.Parameters.Add("@userComments", SqlDbType.Varchar, 50).value = userComments;
cmd.Parameters.Add("@dateCommented", SqlDbType.DateTime).value = dateCommented;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment