Step by step CRUD Operations using Console Application
Through-out the article we will learn step-by-step: How to perform crud operations in Console Application . We will discuss crud operations with usage of pure Sql server. I have mention the code for Save,Edit,delete given below.
Abstract
Through-out the article we will learn step-by-step: How to perform crud operations in Console Application. We will discuss crud operations with usage of pure Sql server. Pre-requisite
To implement the solution and to feel the taste of code, you should:Step1: Create Console Application Project
Launch your visual studio and perform following action (Refer to snapshot)
File->New->Select the new project ->Console Application->
Step3:List the Records using
In your view, write or copy/paste code, mentioned in following code-snippet:
//Select Part
MyConn.Open();
command = new SqlCommand("SELECT * FROM TblEmp", MyConn);
using (SqlDataReader reader = command.ExecuteReader())
{
Console.WriteLine("Emp Id\t\t\t\t\t Name \t\t\t\t\t\t EmpNo\t");
while (reader.Read())
{
Console.WriteLine(String.Format("{0} \t\t\t | {1} \t | {2}",
reader[0], reader[1], reader[2]));
}
}
Console.WriteLine("Data displayed! Now press enter to move to the next section!");
Console.ReadLine();
Console.Clear();
MyConn.Close();
Following code inserts records to database.
//Insert Part
MyConn.Open();
SqlCommand insertCommand = new SqlCommand("INSERT INTO TblEmp (EmpNo, EmpName) VALUES (@EmpNo, @EmpName)", MyConn);
insertCommand.Parameters.Add(new SqlParameter("EmpNo", 100));
insertCommand.Parameters.Add(new SqlParameter("EmpName", "Mr.Gaurav Arora"));
Console.WriteLine("Commands executed! Total rows affected are " + insertCommand.ExecuteNonQuery());
Console.WriteLine("Done! Press enter to move to the next step");
Console.ReadLine();
Console.Clear();
MyConn.Close();
Following code-snippet defines How to select corresponding record and then to update them.
//Update
MyConn.Open();
SqlCommand updateCommand = new SqlCommand("Update TblEmp set EmpNo=@EmpNo,EmpName=@EmpName where Id=@Id", MyConn);
updateCommand.Parameters.Add(new SqlParameter("Id", 8));
updateCommand.Parameters.Add(new SqlParameter("EmpNo", 1980));
updateCommand.Parameters.Add(new SqlParameter("EmpName", "Mr.Tony John"));
Console.WriteLine("Commands executed! Total rows affected are " + updateCommand.ExecuteNonQuery());
Console.WriteLine("Done! Press enter to move to the next step");
Console.ReadLine();
Console.Clear();
MyConn.Close();
Following code-snippet describing how to delete the corresponding records:
//Delete
MyConn.Open();
SqlCommand deleteCommand = new SqlCommand("Delete from TblEmp where Id=@Id", MyConn);
deleteCommand.Parameters.Add(new SqlParameter("Id", 8));
Console.WriteLine("Commands executed! Total rows affected are " + deleteCommand.ExecuteNonQuery());
Console.WriteLine("Done! Press enter to move to the next step");
Console.ReadLine();
Console.Clear();
MyConn.Close();
Write or copy/paste code to your Console Application window, mentioned in the following code-snippet:
Following is the final view of our Console Application Operations:Conclusion
In this whole article, we discussed about Console Application operations using c#.
1. Create a new project
2. Choose correct Project Selection
3. Add Class
4. Add various operations
Hope, you find this article helpful.