Skip to content

Instantly share code, notes, and snippets.

@luisdeol
Last active July 8, 2020 00:04
Show Gist options
  • Save luisdeol/05e03a94624cf6299bd7b8b50effff61 to your computer and use it in GitHub Desktop.
Save luisdeol/05e03a94624cf6299bd7b8b50effff61 to your computer and use it in GitHub Desktop.
4.2: Inserting, Updating and Deleting data.
using System;
using System.Data.SqlClient;
namespace _42_ConsumeData
{
class Program
{
static void Main(string[] args)
{
const string connectionString = "Server=LAPTOP-201QPF8A\\SQLEXPRESS;Database=EcommerceDb;Trusted_Connection=true";
const string productDescription = "Product 4";
const int productQuantity = 400;
const decimal productPrice = 400.0m;
using (var sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
const string insertionQuery = "INSERT INTO Product VALUES (@description, @quantity, @price)";
const string updateQuery = "UPDATE Product SET Price = 4 WHERE Description = @description";
const string deletionQuery = "DELETE FROM Product WHERE Description = @description";
// Setting and running INSERT command
var sqlCommand = new SqlCommand(insertionQuery, sqlConnection);
sqlCommand.Parameters.Clear();
sqlCommand.Parameters.AddWithValue("description", productDescription);
sqlCommand.Parameters.AddWithValue("quantity", productQuantity);
sqlCommand.Parameters.AddWithValue("price", productPrice);
sqlCommand.ExecuteNonQuery();
// Setting and running UPDATE command
sqlCommand.CommandText = updateQuery;
sqlCommand.ExecuteNonQuery();
// Setting and running DELETE command
sqlCommand.CommandText = deletionQuery;
sqlCommand.ExecuteNonQuery();
}
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment