다음을 통해 공유


WCF Service to Insert and Retrieve Data from SQL server 2012 Express and Call it in Windows Phone 8 App

Introduction:

Insertion and Retrieval of data becomes more easy with WCF service. Today we are gonna discuss how to create a WCF service for this purpose.

NOTE: Before Visual Studio application, first of all INSTALL* ***and Create ***Database and Table in SQL Server Express 2012 


1) Open VS 2013 and select WCF Service application project and Name it.

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F3.bp.blogspot.com%2F-OCacbot0T6s%2FVCbiMf5AI4I%2FAAAAAAAAAe0%2FaRuW26ar1ok%2Fs1600%2F7.png&container=blogger&gadget=a&rewriteMime=image%2F*

2) Now right click on the project's name and add new item.

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F2.bp.blogspot.com%2F-o1GIoz8_8-o%2FVCbiM6TENNI%2FAAAAAAAAAe8%2FfNHYjdalrhA%2Fs1600%2F8.png&container=blogger&gadget=a&rewriteMime=image%2F*

3) Create LINQ to SQL classes project and Add it.

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F4.bp.blogspot.com%2F-5dtWr8kUa1M%2FVCbiNuLsyGI%2FAAAAAAAAAfE%2Fu2UNtN5XDhs%2Fs1600%2F9.png&container=blogger&gadget=a&rewriteMime=image%2F*

4) Now open server explorer in VS and right click on Data Connections and Add New Connection.

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F3.bp.blogspot.com%2F-RbYpV6sjENU%2FVCbiKB9R3LI%2FAAAAAAAAAfU%2FWhYmXQXgBNw%2Fs1600%2F10.png&container=blogger&gadget=a&rewriteMime=image%2F*

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

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F2.bp.blogspot.com%2F-SxEx38se114%2FVCbiKMdnMAI%2FAAAAAAAAAeQ%2FV44eLuQr6Oo%2Fs1600%2F11.png&container=blogger&gadget=a&rewriteMime=image%2F*

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

7) Now your SQL DB is connected to VS and is added in Server Explorer. Expand DB, then Tables and DRAG your Table to the middle screen and DROP it there.

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F2.bp.blogspot.com%2F-UgQEYIBaXR4%2FVCbiKWOyQFI%2FAAAAAAAAAfQ%2FtYOJj5KjarE%2Fs1600%2F12.png&container=blogger&gadget=a&rewriteMime=image%2F*

8) Now open IService.cs form Solution Explorer and add this code.

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-_ETzF2aowfI%2FVCbiK66Y-BI%2FAAAAAAAAAfI%2FHYFBHBvlgZY%2Fs1600%2F13.png&container=blogger&gadget=a&rewriteMime=image%2F*

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F4.bp.blogspot.com%2F-yxBW5-2uAN4%2FVCbiLI8YPZI%2FAAAAAAAAAek%2FYbEqdhFEM4Y%2Fs1600%2F14.png&container=blogger&gadget=a&rewriteMime=image%2F*\

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

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F4.bp.blogspot.com%2F-VFHt4wwoL9c%2FVCbiLkCtOqI%2FAAAAAAAAAfA%2Fm2wUX-cJnhM%2Fs1600%2F15.png&container=blogger&gadget=a&rewriteMime=image%2F*

10) Now you have created WCF service. Now Test the service whether it works fine or not. Right click on the Service1.svc from sloution explorer and select View In Browser. Your expected result will look like this.

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F3.bp.blogspot.com%2F-IjJLtBt67co%2FVCbjSAkea1I%2FAAAAAAAAAfg%2FPfjzqtBf1bE%2Fs1600%2F21.png&container=blogger&gadget=a&rewriteMime=image%2F*

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-kPMBiQ0VLa4%2FVCbjCN8BnVI%2FAAAAAAAAAfY%2FBqidj-H_WEM%2Fs1600%2F20.png&container=blogger&gadget=a&rewriteMime=image%2F*

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

Calling WCF Web Service from Windows Phone 8 Application:
**
**

12) Open VS 2013 and Create Windows Phone Blank app.

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

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F3.bp.blogspot.com%2F-oWWDw2I58iI%2FVCbpfMggHWI%2FAAAAAAAAAfw%2FyhCWhbY8we8%2Fs1600%2F22.png&container=blogger&gadget=a&rewriteMime=image%2F*

14) Then Enter that COPIED URL and click GO. Then Enter your Service Name and Click OK.

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F4.bp.blogspot.com%2F-k6BLj3V8qbc%2FVCbpfadbK9I%2FAAAAAAAAAgA%2FP0j3-qCrdOg%2Fs1600%2F23.png&container=blogger&gadget=a&rewriteMime=image%2F*

15) In MainPage.xaml add this code.

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F2.bp.blogspot.com%2F-MisgfSfv7rU%2FVCbpfv-aaxI%2FAAAAAAAAAgM%2F7Ul2GfIVqbU%2Fs1600%2F24.png&container=blogger&gadget=a&rewriteMime=image%2F*

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

**using PhoneApp.ServiceReference2; **

17) Now add a new service object like this.

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-VVF5vlCV1xM%2FVCbpf0qQCPI%2FAAAAAAAAAgI%2FjBCKjvgIIZk%2Fs1600%2F25.png&container=blogger&gadget=a&rewriteMime=image%2F*

18) Then Add this Code behind Insert Button's click event.

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F4.bp.blogspot.com%2F-Csewtnyfk1s%2FVCbqo3MJl2I%2FAAAAAAAAAgc%2Fd4vrWidPkpk%2Fs1600%2F26.png&container=blogger&gadget=a&rewriteMime=image%2F*

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

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F3.bp.blogspot.com%2F-SYOYqnfpSAk%2FVCbqozMhyYI%2FAAAAAAAAAgY%2F3pLfZYEEfzc%2Fs1600%2F27.png&container=blogger&gadget=a&rewriteMime=image%2F*

**20) **Then add a New Page in the project and add a listbox in Page1.xaml.

**21) **Now add this code in Page1.xaml.cs

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F3.bp.blogspot.com%2F-ljjD0Y0Puck%2FVCbqpCCLw6I%2FAAAAAAAAAgg%2FxxD2rFJ1-_A%2Fs1600%2F28.png&container=blogger&gadget=a&rewriteMime=image%2F*

22) Don't forget to add namespace in Page1.xaml.cs

**using PhoneApp.ServiceReference2; **

**
23)** Now Rebuild the project and press F5 and You are good to go :)

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-709w2ybw33o%2FVCbwQTFvseI%2FAAAAAAAAAgw%2F84eWnsJqZXU%2Fs1600%2Fs1.png&container=blogger&gadget=a&rewriteMime=image%2F*

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F3.bp.blogspot.com%2F-FKoeAk4rrss%2FVCbwQQ5L4LI%2FAAAAAAAAAg8%2FGzRrStQn-oQ%2Fs1600%2Fs2.png&container=blogger&gadget=a&rewriteMime=image%2F*

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F2.bp.blogspot.com%2F-MgwJM15LCqo%2FVCbwQjVPefI%2FAAAAAAAAAg0%2Fm1mfFqUfG7Y%2Fs1600%2Fs3.png&container=blogger&gadget=a&rewriteMime=image%2F*

https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F2.bp.blogspot.com%2F-rB7UtXJo5gQ%2FVCbwRM5UjHI%2FAAAAAAAAAhA%2FbSv6SGBuqtA%2Fs1600%2Fs4.png&container=blogger&gadget=a&rewriteMime=image%2F*

NOTE: To run WCF services PERFECTLY with Windows Phone Emulator, see **THIS article carefully.

Happy Coding :)