Video tutorial on Consuming WCF Data Service

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

}

}

}

3 responses to “Video tutorial on Consuming WCF Data Service”

  1. Good one, you are rocking.

  2. YES. I’ve been searching for the solution as to why I couldn’t save my data back to the WCF service. This code works (although I’ve got an error on the last SaveChanges but I think that’s to do with related entities). Many thanks for this!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com