Tuesday 2 October 2012

c# for dummies: Inserting into database using WCF

Insert into Database using WCF.

Write These Code :  App_Code/Service.cs


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.Xml.Linq;
using System.Data.SqlClient;
using System.Data;

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

    bool flag = false;
    public bool saveStudent(string matricno,string name)
    {
        string conectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Abayomi\Documents\Visual Studio 2010\WebSites\UnderstandingWCF2\App_Data\studentDB.mdf;Integrated Security=True;User Instance=True";
        SqlConnection sqlconn = new SqlConnection(conectionString);
        sqlconn.Open();
        string command = "insert into studentTB(MatricNo,name)values('"+matricno+"','"+name+"')";
        SqlCommand sqlcooma = new SqlCommand(command, sqlconn);
        sqlcooma.ExecuteNonQuery();
        flag = true;
        return flag;
    }
 
      

          }
}




Write these code in Interface: App_Code/IService.cs




public interface IService
{

   

    [OperationContract]
    bool saveStudent(string matricno,string name);

 }



Then create another windows application


Go to add  service refrence on the project solution explorer and enter the wcf address url.


No comments:

Post a Comment