In the previous episode, we have successfully created Business Logic Layer (BLL) which contains Getters and Setters for Category Module.
In this episode, we will create Data Access Layer (DAL) which will have methods to Display all Categories, Insert New Category and Update & Delete Existing Category.
Code used to create these methods in Category DAL are as follows
WATCH THE VIDEO FIRST
1. Code for Select Method
public DataTable Select()
{
//Creating Database Connection
SqlConnection conn = new SqlConnection(myconnstrng);
DataTable dt = new DataTable();
try
{
//Wrting SQL Query to get all the data from DAtabase
string sql = "SELECT * FROM tbl_categories";
SqlCommand cmd = new SqlCommand();
SqlDataAdapter adapter = new SqlDataAdapter();
//Open DAtabase Connection
conn.Open();
//Adding the value from adapter to Data TAble dt
adapter.Fill(dt);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return dt;
}
2. Code for Insert Method
public bool Insert(categoriesBLL c)
{
//Creating A Boolean VAriable and set its default value to false
bool isSucces = false;
//Connecting to Database
SqlConnection conn = new SqlConnection(myconnstrng);
try
{
//Writing Query to Add New Category
string sql = "INSERT INTO tbl_categories (title, description, added_date, added_by) VALUES (@title, @description, @added_date, @added_by)";
//Creating SQL Command to pass values in our query
SqlCommand cmd = new SqlCommand(sql, conn);
//Passing Values through parameter
cmd.Parameters.AddWithValue("@title", c.title);
cmd.Parameters.AddWithValue("@description", c.description);
cmd.Parameters.AddWithValue("@added_date", c.added_date);
cmd.Parameters.AddWithValue("@added_by", c.added_by);
//Open Database Connection
conn.Open();
//Creating the int variable to execute query
int rows = cmd.ExecuteNonQuery();
//If the query is executed successfully then its value will be greater than 0 else it will be less than 0
if(rows>0)
{
//Query Executed Succesfully
isSucces = true;
}
else
{
//Failed to Execute Query
isSucces = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//Closing Database Connection
conn.Close();
}
return isSucces;
}
3. Code for Update Method
public bool Update(categoriesBLL c)
{
//Creating Boolean variable and set its default value to false
bool isSuccess = false;
//Creating SQL Connection
SqlConnection conn = new SqlConnection(myconnstrng);
try
{
//Query to Update Category
string sql = "UPDATE tbl_categories SET title=@title, description=@description, added_date=@added_date, added_by=@added_by WHERE id=@id";
//SQl Command to Pass the Value on Sql Query
SqlCommand cmd = new SqlCommand(sql, conn);
//Passing Value using cmd
cmd.Parameters.AddWithValue("@title", c.title);
cmd.Parameters.AddWithValue("@description", c.description);
cmd.Parameters.AddWithValue("@added_date", c.added_date);
cmd.Parameters.AddWithValue("@added_by", c.added_by);
cmd.Parameters.AddWithValue("@id", c.id);
//Open DAtabase Connection
conn.Open();
//Create Int Variable to execute query
int rows = cmd.ExecuteNonQuery();
//if the query is successfully executed then the value will be grater than zero
if(rows>0)
{
//Query Executed Successfully
isSuccess = true;
}
else
{
//Failed to Execute Query
isSuccess = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return isSuccess;
}
4. Code for Delete Method
public bool Delete(categoriesBLL c)
{
//Create a Boolean variable and set its value to false
bool isSuccess = false;
SqlConnection conn = new SqlConnection(myconnstrng);
try
{
//SQL Query to Delete from Database
string sql = "DELETE FROM tbl_categories WHERE id=@id";
SqlCommand cmd = new SqlCommand(sql, conn);
//Passing the value using cmd
cmd.Parameters.AddWithValue("@id", c.id);
//Open SqlConnection
conn.Open();
int rows = cmd.ExecuteNonQuery();
//If the query is executd successfully then the value of rows will be greater than zero else it will be less than 0
if(rows>0)
{
//Query Executed Successfully
isSuccess = true;
}
else
{
//Faied to Execute Query
isSuccess = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return isSuccess;
}
All of these methods will be used to add actual functionality to add, update and delete in our Category Module which will be done in next episode.
Thank you so much for reading and watching my tutorial. If you want to see more posts like this, then don't forget to SUBSCRIBE.
If you want to START from the beginning of this course, then CLICK HERE.
Comments