Let now discuss how we can use the Entity Client to insert Department data. To do this, follow these simple steps:
1. Create an instance of the
EntityConnection
class as shown below:EntityConnection conn = new EntityConnection ("Name=PayrollEntities");
2. Open the connection using the
EntityConnection
instance:conn.Open();
3. Create an
EntityCommand
instance and specify theCommandText
andCommandType
properties:EntityCommand cmd = conn.CreateCommand(); cmd.CommandText = "PayrollEntities.InsertDepartment"; cmd.CommandType = CommandType.StoredProcedure;
4. Add parameters using the
AddWithValue()
method of theParameters
collection of theEntityCommand
instance:cmd.Parameters.AddWithValue ("DepartmentName", "Finance");
5. Next, execute the procedure using the
ExecuteNonQuery()
method:cmd.ExecuteNonQuery();
Here is the complete code listing:
using (EntityConnection conn = new EntityConnection("Name= PayrollEntities")) { try { conn.Open(); EntityCommand cmd = conn.CreateCommand...