Click here to Skip to main content
Licence CPOL
First Posted 2 Dec 2009
Views 13,146
Downloads 625
Bookmarked 5 times

WCF databinding using ADO.NET

By | 2 Dec 2009 | Article
This basic example illustrates invoking database tables using a WCF service.
 
Part of The SQL Zone sponsored by
See Also

Introduction

When I wanted to learn creating WCF Web Services, I thought it would not be a bad idea to do databinding using ADO.NET. Many articles I went through used LINQ to SQL. Although this was helpful, I decided to do a WCF app that retrieves data off of a database using plain ADO.NET. When I looked, there weren't any articles that would demonstrate that. This is a very basic example like a Hello World for retrieving data from a SQL Server database.

Background

As we all know, the DataContract attribute class is used to mark types you write as participating in the WCF serialization via the DataContractSerializer. Marking your classes with this attribute ensures that they can be sent to and from disparate clients in an efficient manner. This is automatically done when we use LINQ classes (.dbml) when you mark the property of the class for "uni-directional serialization".

We will try to retrieve all the records from a table in a database. I used my company database in this example. But you can change it to be the Northwind database by just changing the query.

The project involves creating a "WCF Service application" and a web client to test the service.

Using the code

We define the interface with two operation contracts as below:

//IMyService.cs
[OperationContract]
Shipper GetShipper(int shipperID);

[OperationContract]
Shipper saveShipper(Shipper shipper);

Now let's define the Data Contract with the two data members which need to be serialized.

//Define the data contract IMyService.cs
[DataContract]

public class Shipper
{
    [DataMember]
    public int ShipperID
    {
        get; set;
    }
    [DataMember]
    public DataSet dsTable
    {
       get;set;
    }

Web client

Create a regular ASP.NET website and add a web reference to the Web Service just created, and invoke the methods.

Points of interest

The entire thing took about 30 minutes to do, mainly because I did not know how I should mark serialization for the data. I hope this is useful for folks just starting with WCF services and databinding. I'm happy I learnt databinding, and this would be a good starting point to develop bigger applications. I tried LINQ, but did not succeed with invoking the service asynchronously. I plan to try again. This is my first article here, so any comments / suggestions are welcome.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

srilatkol



Unknown

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
General[My vote of 1] Is it just me... PinmemberAndre Luiz V Sanches7:09 2 Dec '09  
GeneralRe: [My vote of 1] Is it just me... Pinmembersrilatkol7:52 2 Dec '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 2 Dec 2009
Article Copyright 2009 by srilatkol
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid