Create,Read,Update,Delete only functionality with Model class for CRUD Practice | C# .Net

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;


namespace CRUDApplication.Models

{

    public class Client

    {

        public int id;

        public string firstname = "";

        public string lastname = "";

        public string email = "";

        public string phone = "";

        public string address = "";

        public string createdAt = "";

    }

}

Above File is CRUDApplication\Models\Client.cs








using System;

using System.Collections.Generic;

using System.Data.SqlClient;

using System.Linq;

using System.Web;

using CRUDApplication.Models;

using System.Configuration;

using System.Data;


namespace CRUDApplication.Repositories

{

    public class ClientRepository

    {

        private readonly string connectionString = "Data Source=HEER\\SQLEXPRESS;Initial Catalog=practiceDB;Integrated Security=True;Encrypt=False";


        public List<Client> GetClients()

        {

            var clients = new List<Client>();


            try

            {

                using(SqlConnection connection = new SqlConnection(connectionString))

                {

                    connection.Open();


                    string sql = "SELECT * FROM clients ORDER BY id DESC";

                    using(SqlCommand command = new SqlCommand(sql,connection))

                    {

                        using(SqlDataReader reader = command.ExecuteReader())

                        {

                            while(reader.Read())

                            {

                                Client client = new Client();

                                client.id = reader.GetInt32(0);

                                client.firstname = reader.GetString(1);

                                client.lastname = reader.GetString(2);

                                client.email = reader.GetString(3);

                                client.phone = reader.GetString(4);

                                client.address = reader.GetString(5);

                                client.createdAt = reader.GetDateTime(6).ToString();


                                clients.Add(client);

                            }

                        }

                    }


                }

            }

            catch (Exception ex)

            {

                Console.WriteLine("Exception: " + ex.ToString());

            }


            return clients;

        }


        public Client GetClient(int id)

        {

            try

            {

                using(SqlConnection connection = new SqlConnection(connectionString))

                {

                    connection.Open();

                    string sql = "SELECT * FROM clients WHERE id=@id";

                    using(SqlCommand command = new SqlCommand(sql,connection))

                    {

                        command.Parameters.AddWithValue("@id",id);

                        using(SqlDataReader reader = command.ExecuteReader())

                        {

                            if (reader.Read())

                            {

                                Client client = new Client();

                                client.id = reader.GetInt32(0);

                                client.firstname = reader.GetString(1);

                                client.lastname = reader.GetString(2);

                                client.email = reader.GetString(3);

                                client.phone = reader.GetString(4);

                                client.address = reader.GetString(5);

                                client.createdAt = reader.GetDateTime(6).ToString();


                                return client;

                            }

                        }

                    }

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine("Exception: " + ex.ToString());

            }

            return null;

        }


        public void CreateClient(Client client)

        {

            try

            {

                using(SqlConnection connection = new SqlConnection(connectionString))

                {

                    connection.Open();

                    string sql = "INSERT INTO clients " +

                        "(firstname,lastname,email,phone,address) VALUES" +

                        "(@firstname,@lastname,@email,@phone,@address);";


                    using(SqlCommand command = new SqlCommand(sql,connection))

                    {

                        command.Parameters.AddWithValue("@firstname",client.firstname);

                        command.Parameters.AddWithValue("@lastname",client.lastname);

                        command.Parameters.AddWithValue("@email",client.email);

                        command.Parameters.AddWithValue("@phone",client.phone);

                        command.Parameters.AddWithValue("@address",client.address);


                        command.ExecuteNonQuery();

                    }

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine("Exception: " + ex.ToString());

            }

        }


        public void UpdateClient(Client client)

        {

            try

            {

                using(SqlConnection connection = new SqlConnection(connectionString))

                {

                    connection.Open();

                    string sql = "UPDATE clients" +

                        "SET firstname=@firstname, lastname=@lastname, " +

                        "email=@email, phone=@phone, address=@address " +

                        "WHERE id=@id";


                    using(SqlCommand command = new SqlCommand(sql,connection))

                    {

                        command.Parameters.AddWithValue("@firstname",client.firstname);

                        command.Parameters.AddWithValue("@lastname",client.lastname);

                        command.Parameters.AddWithValue("@email",client.email);

                        command.Parameters.AddWithValue("@phone",client.phone);

                        command.Parameters.AddWithValue("@address",client.address);

                        command.Parameters.AddWithValue("@id",client.id);


                        command.ExecuteNonQuery();

                    }

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine("Exception: " + ex.ToString());

            }

        }


        public void DeleteClient(int id)

        {

            try

            {

                using(SqlConnection connection = new SqlConnection(connectionString))

                {

                    connection.Open();

                    string sql = "DELETE FROM clients WHERE id=@id";


                    using(SqlCommand command = new SqlCommand(sql,connection))

                    {

                        command.Parameters.AddWithValue("@id",id);


                        command.ExecuteNonQuery();

                    }

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine("Exception: " + ex.ToString());

            }

        }

    }

}

Above File is CRUDApplication\Repositories\ClientRepository.cs


Comments

Popular posts from this blog

Create a User Registration Form in ASP.NET using SQL Server, Visual Studio 2022 & Bootstrap

Create a Simple Login Form in ASP.NET using Visual Studio 2022

SqlCommand Class ADO.Net | ExecuteNonQuery | ExecuteReader | ExecuteScalar