Last active
March 14, 2026 16:50
-
-
Save abonello/9ebe85482c0392af17fae986aa95c76f to your computer and use it in GitHub Desktop.
Fundamentals of Crud in .net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.Data.SqlClient; | |
| class Program | |
| { | |
| private static string connectionString = "Server=(localdb)\\mssqllocaldb;Database=LinqPracticeDB;Trusted_Connection=True;"; | |
| static void Main() | |
| { | |
| Console.WriteLine("Current Tasks:"); | |
| ShowTasks(); | |
| Console.WriteLine("\nAdding new task..."); | |
| AddTask("Review pull request"); | |
| Console.WriteLine("\nTasks after insert:"); | |
| ShowTasks(); | |
| Console.WriteLine("\nMarking task 1 as completed..."); | |
| MarkCompleted(1); | |
| Console.WriteLine("\nTasks after update:"); | |
| ShowTasks(); | |
| Console.WriteLine("\nDeleting task 7..."); | |
| DeleteTask(7); | |
| Console.WriteLine("\nTasks after delete:"); | |
| ShowTasks(); | |
| } | |
| // CREATE (INSERT) | |
| static void AddTask(string title) | |
| { | |
| string query = "INSERT INTO Tasks (Title, Completed) VALUES (@title, 0)"; | |
| using SqlConnection connection = new SqlConnection(connectionString); | |
| connection.Open(); | |
| using SqlCommand command = new SqlCommand(query, connection); | |
| command.Parameters.AddWithValue("@title", title); | |
| command.ExecuteNonQuery(); | |
| } | |
| // READ | |
| static void ShowTasks() | |
| { | |
| string query = "SELECT Id, Title, Completed FROM Tasks"; | |
| using SqlConnection connection = new SqlConnection(connectionString); | |
| connection.Open(); | |
| using (SqlCommand command = new SqlCommand(query, connection)) | |
| using (SqlDataReader reader = command.ExecuteReader()) | |
| while (reader.Read()) | |
| { | |
| int id = reader.GetInt32(0); | |
| string title = reader.GetString(1); | |
| bool completed = reader.GetBoolean(2); | |
| Console.WriteLine($"{id} = {title} - {completed}"); | |
| } | |
| } | |
| //UPDATE | |
| static void MarkCompleted(int id) | |
| { | |
| string query = "UPDATE Tasks SET Completed = 1 WHERE ID = @id"; | |
| using SqlConnection connection = new SqlConnection(connectionString); | |
| connection.Open(); | |
| using SqlCommand command = new SqlCommand(query, connection); | |
| command.Parameters.AddWithValue("@id", id); | |
| command.ExecuteNonQuery(); | |
| } | |
| //DELETE | |
| static void DeleteTask(int id) | |
| { | |
| string query = "DELETE FROM Tasks WHERE ID = @id"; | |
| using SqlConnection connection = new SqlConnection(connectionString); | |
| connection.Open(); | |
| using SqlCommand command = new SqlCommand (query, connection); | |
| command.Parameters.AddWithValue("@id", id); | |
| command.ExecuteNonQuery(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment