Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

1. Create the C# Windows Form Application.

2. Create Database.
A. Database Name: studentdb
B. Table Name: student
C. Fields or Column Names:

Column Name Data Type


id int
name varchar(100)
dob date
department varchar(100)

3. Right click on Project Name = > Add = > Class


Give the class name as StudentModel.cs and type the following code.

public class StudentModel


{
public int ID { get; set; }
public string Name { get; set; }
public string DOB { get; set; }
public string Department { get; set; }
}

3. Right click on Project Name = > Add = > Class


Give the class name as DbOperations.cs and type the following code.

using System;
using System.Collections.Generic;
using System.Data.SqlClient;

public class DbOperations


{
private SqlConnection sqlconn;

// Method for opening the connection to database.


public string OpenConnection()
{
try
{
string dbPath =
System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Locat
ion), "studentdb.mdf");
sqlconn = new SqlConnection();
sqlconn.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" +
dbPath + ";Integrated Security=True";
sqlconn.Open();
return null;
}
catch (Exception ex)
{
return ex.Message;
}
}

// Method for closing the connection


private bool CloseConnection()
{
try
{
sqlconn.Close();
return true;
}
catch { return false; }
}

// Code showing how to insert record to database.


public bool Insert(int id, string name, string dob, string department)
{
try
{
string query = "INSERT INTO student (id, name, dob, department) VALUES(" + id + ",'" + name +
"', '" + dob + "', '" + department + "')";
if (this.OpenConnection() == null)
{
SqlCommand cmd = new SqlCommand(query, sqlconn);
int res = cmd.ExecuteNonQuery();
if (res >= 1)
return true;
}
}
finally
{
this.CloseConnection();
}
return false;
}

// Code showing how to update record to database.


public bool Update(int id, string name, string dob, string department)
{
try
{
string query = "UPDATE student SET name='" + name + "', dob='" + dob + "', department='" +
department + "' WHERE id=" + id;
if (this.OpenConnection() == null)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = query;
cmd.Connection = sqlconn;
int res = cmd.ExecuteNonQuery();
if (res >= 1)
return true;
else
return false;
}
return false;
}
catch { return false; }
finally
{
this.CloseConnection();
}
}

// Code showing how to delete record to database.


public bool Delete(int id)
{
try
{
string query = "DELETE FROM student WHERE id=" + id + "";

if (this.OpenConnection() == null)
{
SqlCommand cmd = new SqlCommand(query, sqlconn);
int res = cmd.ExecuteNonQuery();
if (res >= 1)
return true;
}
}
finally
{
this.CloseConnection();
}
return false;
}

// Getting data from database and creating a collection of it using List Class.
public List<StudentModel> GetData()
{
try
{
List<StudentModel> list = new List<StudentModel>();
string query = "select *from student;";

if (this.OpenConnection() == null)
{
SqlCommand cmd = new SqlCommand(query, sqlconn);
SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
list.Add(new StudentModel()
{
ID = int.Parse(reader["id"].ToString()),
Name = reader["name"].ToString(),
Department = reader["department"].ToString(),
DOB = reader["dob"].ToString()
});
}
this.CloseConnection();
return list;
}
return null;
}
catch { return null; }
}
// Check if Record exists or not
public bool CheckRecord(int id)
{
try
{
if (this.OpenConnection() == null)
{
SqlCommand cmd = new SqlCommand("select count(*) from student where id = @id", sqlconn);
cmd.Parameters.AddWithValue("@id", id);
int count = (int)cmd.ExecuteScalar();
if (count > 0)
return true;
}
}
finally
{
this.CloseConnection();
}

return false;
}

// Check and return if database connection is successful or not


public string CheckConnection()
{
string message = OpenConnection();
if (message == null)
{
// Connection opened successfully
CloseConnection();
return null;
}
else
{
// Failed to open the Database Connection
return message;
}
}
}
4. Create the forms for Inserting, Updating and Deleting. These are the basic
Operations shown but apart from these you can customize however you want while creating the
Applications.

5. Events need to be written for the 5 buttons shown above.

(Please refer to the Sample provided)

You might also like