Monday, 24 February 2014

WCF Services Example Of Insertion Of Data Into SQL Server

***************** Code Behind Page *******************



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WcfServiceLibrary1;

public partial class home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void submitbtn_Click(object sender, EventArgs e)
    {
        Service1 s = new Service1();
        s.addStudent(txtname.Text, txtrollno.Text);
       
    }
}

***************** Services ******************* Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using businesslayer;

namespace WcfServiceLibrary1
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class Service1 : IService1
    {
        studentbl bl = new studentbl();
      public  int addStudent(string name, string rollno)
        {
          return  bl.addstudent( name,  rollno);
        }
    }
}

***************** IServices ******************* Interface

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;

using System.Text;

namespace WcfServiceLibrary1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
 
     interface IService1
    {
        [OperationContract]

        int addStudent(string name,string rollno);

        // TODO: Add your service operations here
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations
 
 
}


***************** BLL*******************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using dataacess_layer;

namespace businesslayer
{
    public class studentbl
    {
        studentda stdda = new studentda();
        public int addstudent(string name, string rollno)
        {
           return stdda.addstudent(name, rollno);
        }
    }
}

***************** Dal*******************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using dl;
namespace dataacess_layer
{
    public class studentda
    {
        public int addstudent(string name, string rollno)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd=new SqlCommand ();
            studentdl.OpenConnection();
            cmd.CommandText = "insert into studenttbl (name,rollno) values(@name,@roll)";
            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@roll", rollno);
            cmd.Connection = studentdl.MyConnection;
            cmd.ExecuteNonQuery();
            studentdl.CloseConnection();
            return 1;
        }
    }
}

***************** DbConnection******************* 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;

namespace dl
{
    public static class studentdl
    {
        static SqlConnection stdconn = new SqlConnection();

        public static SqlConnection MyConnection
        {
            get { return stdconn; }

        }
        public static void OpenConnection()
        {
            string ConnectionSring = string.Empty;
            if (ConfigurationManager.ConnectionStrings["stdcon"] != null)
            {
                ConnectionSring = ConfigurationManager.ConnectionStrings["stdcon"].ConnectionString;
                stdconn.ConnectionString = ConnectionSring;
                if (stdconn.State == ConnectionState.Closed)
                {
                    stdconn.Open();
                }
            }

        }
        public static void CloseConnection()
        {
            stdconn.Close();
        }
    }
}







No comments:

Post a Comment