Insert Data Into SQL Server Database Using WCF Service

This article introduces how to insert data into SQL Server 2008 using a WCF service from C# code. To insert data into a database using a WCF service, we must do the following 3 things:

Create Database Table

Create WCF Service

Create web Application

In the first step we will create a table in SQL Server; after that we create a simple function to insert data into the database using a WCF service. In a web application, add a reference of the service and data to be inserted will be sent to the web services function which will be inserted into the database table. Let's take a look at a practical example. The example application is developed in Visual Studio 2010 and SQL Server 2008.

Step 1: Creating Database Table

Database name: Registration

Database table name: RegistrationTable

RegistrationTable Table

CREATETABLE [dbo].[RegistrationTable]

(

[UserName] [varchar](100)NOTNULL,

[Password] [varchar](20)NOTNULL,

[Country] [varchar](100)NOTNULL,

[Email] [varchar](200)NOTNULL

)

Step 2: Creating WCF Service

Now you have to create a WCF Service:

Go to Visual Studio 2010

New-> Select a project

Now click on the project and select WCF Service Application and provide a name for the service:

Now click on the Ok Button. Then you will get 3 files in Solution Explorer.

IService.cs

Service.svc

Service.svc.cs

The following image shows the following files:

For inserting data into the database you need to write the following code in the IService1.cs file which contains the two sections:

OperationContract

DataContract

The OperationContract section is used to add service operations and DataContract is used to add types to service operations.

Iservice1.cs File

Now we create a function in the OperationContract section of the Iservice1.cs file:

publicinterfaceIService1

{

[OperationContract]

string InsertUserDetails(UserDetails userInfo);

}

Now add types to service operations in the DataContract section:

publicclassUserDetails

{

string username = string.Empty;

string password = string.Empty;

string country = string.Empty;

string email = string.Empty;

[DataMember]

publicstring UserName

{

get { return username; }

set { username = value; }

}

[DataMember]

publicstring Password

{

get { return password; }

set { password = value; }

}

[DataMember]

publicstring Country

{

get { return country; }

set { country = value; }

}

[DataMember]

publicstring Email

{

get { return email; }

set { email = value; }

}

}

Service.svc.cs File

In this file we define the definition of the function InsertUserDetails(UserDetails userInfo).

And replace the code with the following:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

using System.Data.SqlClient;

using System.Data;

namespace WCFServiceForInsert

{

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.