——————Insert Operation————————

insert

insert2

//Step 1:Connect to DB and Open the connection
string connString = “Data Source=.;Initial Catalog=EmployeeDB;Integrated Security=True”;
SqlConnection sqlConn = new SqlConnection(connString);
sqlConn.Open();
//Step 2:Execute insertCommand by passing insertQuery
string insertQuery = “INSERT INTO tblEmployee values (1,’Kumar’,’Nagendra’,’Nagendra.Kumar@crudsys.com’)”;
SqlCommand insertCommand = new SqlCommand(insertQuery, sqlConn);
insertCommand.ExecuteNonQuery();
//Step 3: Dispose the SqlCommand and close the SqlConnection
insertCommand.Dispose();
sqlConn.Close();
Console.WriteLine(“Inserted successfully”);
Console.ReadLine();

——————Read Operation————————

selectselect3

//Step 1:Connect to DB and Open the connection
string connString = “Data Source=.;Initial Catalog=EmployeeDB;Integrated Security=True”;
SqlConnection sqlConn = new SqlConnection(connString);
sqlConn.Open();
//Step 2:Executing the SelectQuery
string selectQuery = “select * from tblEmployee”;
SqlCommand selectCommand = new SqlCommand(selectQuery, sqlConn);
SqlDataReader sqlReader = selectCommand.ExecuteReader();
//Step 3: printing the Result
Console.WriteLine(“Id” + ” “ + “FirstName” + ” “ + “EmailId”);
while (sqlReader.Read())
{
Console.WriteLine(sqlReader[0] + ” “ + sqlReader[2] + ” “ + sqlReader[“EmailId”]);
}
//Step 4: Close SqlDataReader & SqlConnection
selectCommand.Dispose();
sqlReader.Close();
sqlConn.Close();
Console.ReadLine();

——————Update Operation————————

Here am updating Last Name and Email ID

update

update2

//Step 1:Connect to DB and Open the connection
string connString = “Data Source=.;Initial Catalog=EmployeeDB;Integrated Security=True”;
SqlConnection sqlConn = new SqlConnection(connString);
sqlConn.Open();
//Step 2:Execute updateCommand by passing UpdateQuery
string UpdateQuery = “Update tblEmployee set LastName = ‘Ponnaganti’,EmailId = ‘Nagendra.Ponnagnti@crudsys.com’ where Empid = 1”;
SqlCommand updateCommand = new SqlCommand(UpdateQuery, sqlConn);
updateCommand.ExecuteNonQuery();
//Step 3:Dispose SqlCommand and close the connection
Console.WriteLine(“Updated successfully”);
updateCommand.Dispose();
sqlConn.Close();
Console.ReadLine();

——————Delete Operation————————

delete

delete2

//Step 1:Connect to DB and Open the connection
string connString = “Data Source=.;Initial Catalog=EmployeeDB;Integrated Security=True”;
SqlConnection sqlConn = new SqlConnection(connString);
sqlConn.Open();
//Step 2:Execute deleteCommand by passing deleteQuery
string deleteQuery = “Delete tblEmployee where Empid= 1”;
SqlCommand deleteCommand = new SqlCommand(deleteQuery, sqlConn);
deleteCommand.ExecuteNonQuery();
//Step 3:Dispose SqlCommand and close the SqlConnection
Console.WriteLine(“Deleted successfully”);
deleteCommand.Dispose();
sqlConn.Close();
Console.ReadLine();

———————————————————-

happ