Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
BRH 58
#ASP.NET 55
ASP.NET 50
#DOTNET 49
.NET 40
WCF 21
DOTNET 12
c# 8
windows azure 7
SILVERLIGHT 7

Archive · View All
April 2011 9
March 2011 9
February 2011 8
December 2010 7
November 2010 5
September 2010 5
August 2010 5
May 2011 4
October 2010 4
January 2011 2

Stored Procedure in WCF Data Service

Apr 1 2011 5:16AM by Dhananjay Kumar   

In this article we will explore, how could we use Stored Procedure with WCF Data Service?

You can read Introduction to WCF Data service and ODATA here

To use Stored Procedure, at time of creation of Data Model, select Stored Procedure as part of Data Model.

Now open EDMX file and right click on that. Select Add and then select Function Import.

Popup window will be open.

  1. Give Function import name
  2. Choose Stored procedure from drop down
  3. Choose the Entity type Stored Procedure is returning.

Stored Procedure we are selecting is GetStudentGrades and it is returning one or more entities of StudentGrade.

After clicking OK you can see columns are mapped

You can see in model browser that GetStudentGrades has been listed in Function Imports section and it is having one input parameter StudentD.

We can see now that Stored Procedure has been mapped to Entity model and can be exposed as WCF Data Service.

Next step we need to create a function in DataService class. This function will return list of entities.Client will call this function to execute Stored Procedure.

[WebGet]
public List<StudentGrade> GetStudentGrade(string studentId)
{
    SchoolEntities entities = new SchoolEntities();
    return entities.GetStudentGrades(Convert.ToInt32(studentId)).ToList();
}

In above function,

  1. Creating instance of Entity class.
  2. Calling the Function on this object. We need to search function import name to call on object of entity class.
  3. Function is having one input parameter to pass as parameter to execute stored Procedure.

For Reference full source code of service class is as below,

WcfDataService.svc.cs

using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
 
namespace WebApplication6
{
    public class WcfDataService1 : DataService<SchoolEntities>
    {
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }
 
        [WebGet]
        public List<StudentGrade> GetStudentGrade(string studentId)
        {
            SchoolEntities entities = new SchoolEntities();
            return entities.GetStudentGrades(Convert.ToInt32(studentId)).ToList();
        }
    }
}

To call the Stored Procedure in browser

http://localhost:14469/WcfDataService1.svc/GetStudentGrade?studentId='2'

Now to call Stored Procedure at Client side

List<StudentGrade> lstStudentsGrade = context.Execute<StudentGrade>
    (new Uri("http://localhost:14469/WcfDataService1.svc/GetStudentGrade?studentId='2'")).ToList();

We need to pass the exact URL of the function in service class executing Stored Procedure.

For Reference full source code at client is as below,

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services;
using System.Data.Services.Client;
using ConsoleApplication1.ServiceReference1; 
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           DataServiceContext context = new DataServiceContext(new Uri("http://localhost:14469/WcfDataService1.svc/"));
            List<StudentGrade> lstStudentsGrade = context.Execute<StudentGrade>
                (new Uri("http://localhost:14469/WcfDataService1.svc/GetStudentGrade?studentId='2'")).ToList();
           
            foreach (var r in lstStudentsGrade)
            {
                Console.WriteLine("Student :" + r.StudentID.ToString() +" Grade : " +  r.Grade);
            }
            Console.ReadKey(true);
        }
    }
}

Output we are expecting is as below,

We saw in this article, how we could call a Stored Procedure using WCF Data Service.

Tags: WCF, #ASP.NET, ASP.NET, BRH, .NET,


Dhananjay Kumar
49 · 4% · 1198
1
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"Stored Procedure in WCF Data Service" rated 5 out of 5 by 1 readers
Stored Procedure in WCF Data Service , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]