0% found this document useful (0 votes)
38 views3 pages

Model

The document defines a C# class called "emp" with properties for an employee ID and name. It includes static methods to insert, update, delete, get all, and get a single employee record from a SQL database table called "emp" using ADO.NET and SQL commands. Connections and queries are performed against a local SQL Server database with the provided connection string.

Uploaded by

Sanket
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views3 pages

Model

The document defines a C# class called "emp" with properties for an employee ID and name. It includes static methods to insert, update, delete, get all, and get a single employee record from a SQL database table called "emp" using ADO.NET and SQL commands. Connections and queries are performed against a local SQL Server database with the provided connection string.

Uploaded by

Sanket
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

using Microsoft.Data.

SqlClient;

namespace WebApplication17.Models
{
public class emp
{
public int Id { get; set; }
public string Name { get; set; }

public static void insert(emp emp)


{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial
Catalog=Lord;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server
Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False";
try
{
con.Open();
SqlCommand insert = new SqlCommand();
insert.Connection = con;
insert.CommandType = System.Data.CommandType.Text;
insert.CommandText = "insert into emp values (@Id,@Name)";
insert.Parameters.AddWithValue("@Id", emp.Id);
insert.Parameters.AddWithValue("@Name", emp.Name);
insert.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally { con.Close(); }
}

public static void update(emp emp)


{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial
Catalog=Lord;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server
Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False";
try
{
con.Open();
SqlCommand insert = new SqlCommand();
insert.Connection = con;
insert.CommandType = System.Data.CommandType.Text;
insert.CommandText = "update emp set Name = @Name where Id =
@Id"; ;
insert.Parameters.AddWithValue("@Id", emp.Id);
insert.Parameters.AddWithValue("@Name", emp.Name);
insert.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally { con.Close(); }
}
public static void delete(emp e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial
Catalog=Lord;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server
Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False";
try
{
con.Open();
SqlCommand insert = new SqlCommand();
insert.Connection = con;
insert.CommandType = System.Data.CommandType.Text;
insert.CommandText = "delete from emp where Id=@Id";
insert.Parameters.AddWithValue("@Id",e.Id);
insert.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally { con.Close(); }
}
public static List<emp> getAll()
{
List<emp> list = new List<emp>();
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial
Catalog=Lord;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server
Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False";
try
{
con.Open();
SqlCommand insert = new SqlCommand();
insert.Connection = con;
insert.CommandType = System.Data.CommandType.Text;
insert.CommandText = "select * from emp";
SqlDataReader dr = insert.ExecuteReader();
while (dr.Read())
{
list.Add(new emp { Id = dr.GetInt32(0), Name =
dr.GetString(1) });
}
dr.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally { con.Close(); }
return list;
}

public static emp? getSingle(int id)


{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial
Catalog=Lord;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server
Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False";
try
{
con.Open();
SqlCommand insert = new SqlCommand();
insert.Connection = con;
insert.CommandType = System.Data.CommandType.Text;
insert.CommandText = $"select * from emp where Id ={id}";
SqlDataReader dr = insert.ExecuteReader();
if (dr.Read())
{
return (new emp { Id = dr.GetInt32(0), Name =
dr.GetString(1) });
}
dr.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally { con.Close(); }
return null;
}
}
}

You might also like