Source code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using secondClient.ServiceReference1;
using System.Data.Services.Client;
namespace secondClient
{
class Program
{
static void Main(string[] args)
{
DataServiceContext context = new DataServiceContext
(new Uri(“http://localhost:3803/WcfDataService1.svc/”));
StudentDBEntities studentDbEnt = new StudentDBEntities
(new Uri(“http://localhost:3803/WcfDataService1.svc/”));
#region Get all the Records
var res = from r in studentDbEnt.Students select r;
foreach (var r in res)
{
Console.WriteLine(r.Name);
}
Console.Read();
#endregion
#region add student
Student std = new Student() { Name = “Mahesh”, RollNumber = “457”, Subject = “WCF data “ };
context.AddObject(“Students”, std);
context.SaveChanges();
//studentDbEnt.AddToStudents(std);
//studentDbEnt.SaveChanges();
#endregion
#region Update Record
Student std = new Student() { Name = “Mahesh”, RollNumber = “457”, Subject = “SilverLight “ };
Student stdUpdate = (from r in studentDbEnt.Students where r.RollNumber == std.RollNumber select r).First();
stdUpdate.Name = std.Name;
stdUpdate.Subject = std.Subject;
context.AttachTo(“Students”, stdUpdate);
context.UpdateObject(stdUpdate);
context.SaveChanges();
//studentDbEnt.UpdateObject(stdUpdate);
//studentDbEnt.SaveChanges();
#endregion
#region Delete a Record
Student stdDelete = (from r in studentDbEnt.Students where r.RollNumber ==“457” select r).First();
context.AttachTo(“Students”, stdDelete);
context.DeleteObject(stdDelete);
context.SaveChanges();
//studentDbEnt.DeleteObject(stdDelete);
//studentDbEnt.SaveChanges();
#endregion
}
}
}
Leave a Reply