WCF Service to Insert and Retrieve Data From SQL Server 2012 Express

Insertion and retrieval of data becomes easier with WCF services. Today we will discuss how to create a WCF service for this purpose.

Note: Before the Visual Studio application, first of all Install and create Database and Table in SQL Server Express 2012.

The following is the procedure to use.

1. Open Visual Studio 2013 and select WCF Service application project and name it.

WCF Service

2. Now right-click on the project's name and "Add" -> "New item".

add new item

3. Create a LINQ to SQL classes project and add it.

LINQ to SQL

4. Now open the Server Explorer in Visual Studio and right-click on "Data Connections" and "Add New Connection".

Add New Connection

5. Now enter your Server Name (Instance Name) and select your database that is to be used.

Server Name

6. You can "Test your Connection" and then click OK.

7. Now your SQL DB is connected to Visual Studio and it is added to the project, visible in the Server Explorer. Expand DB, then tables and drag your table to the middle screen and drop it there.

SQL DB

8. Now open IService.cs from the Solution Explorer and add this code.

IService

cs code

9. Then open Service1.svc.cs and add this code.

open Service1

10. Now you have created a WCF service. Now test whether or not the service works. Right-click on Service1.svc from the Solution Explorer and select "View In Browser". Your expected result will look like this.

local host

WCF service status

11. Now copy the link of service from the browser and save it in Notepad for further use.

Calling WCF Web Service from Windows Phone 8 Application

12. Open Visual Studio 2013 and create a Windows Phone Blank app.

13. In Solution Explorer, right-click on references and "Add New Service Reference".

add reference

14. Then enter that copied URL and click Go. Then enter your Service Name and click OK.

COPIED

15. In MainPage.xaml add this code.

MainPage

16. In MainPage.xaml.cs, add this namespace for service reference.

  1. using PhoneApp.ServiceReference2;  
17. Now add a new service object like this.

add a new service

18. Then add this code behind the Insert Button's click event.

Insert Button
19. Add this code behind the Find Button's click event.

code behind

20. Then add a new page to the project and add a listbox in Page1.xaml.

21. Now add this code to Page1.xaml.cs.

Page1

22. Don't forget to add a namespace in Page1.xaml.cs as in the following:

using PhoneApp.ServiceReference2;

23. Now rebuild the project and press F5 and you are good to go :).

user name

delete insert

page name

my application

Note: To run WCF services perfectly with Windows Phone Emulator, See this article carefully.

Happy Coding :).

 


Similar Articles