Wednesday, September 25, 2013

linq using Store Procedure

Add class……

using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;

[Table(Name="student")]
public class E_modal
{
    [Column(Name = "roll_no", IsPrimaryKey = true)]
    public int rno { get; set; }

    [Column(Name = "stud_name")]
    public string sn { get; set; }

    [Column(Name = "stud_course")]
    public string sc { get; set; }

    [Column(Name = "stud_fees")]
    public int sf { get ;set; }
      
}

.aspx page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataContext d = new DataContext("initial catalog=batch4;data source=dev-pc;integrated security=yes");
       Table< E_modal > e1= d.GetTable< E_modal >();
       GridView1.DataSource = e1;
       GridView1.DataBind();
    }
}


 

Using stored procedure


Add class EM<ENTITY MODAL>
using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;

[Table(Name = "student")]
public class EM
{
    [Column(Name = "roll_no", IsPrimaryKey = true)]
    public int rno { get; set; }

    [Column(Name = "stud_name")]
    public string sn { get; set; }

    [Column(Name = "stud_course")]
    public string sc { get; set; }

    [Column(Name = "stud_fees")]
    public int sf { get; set; }

}

ADD CLASS DAL
using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Reflection;

public class DAL : DataContext
{
    public DAL(string x)
        : base(x)
    {

    }
    [Function(Name = "s_show")]
    public ISingleResult<EM> show()
    {
        IExecuteResult res = this.ExecuteMethodCall(this, (MethodInfo)MethodInfo.GetCurrentMethod());
        ISingleResult<EM> s = (ISingleResult<EM>)res.ReturnValue;
        return s;
    }

}


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

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DAL d = new DAL("initial catalog=batch4;data source=dev-pc;integrated security=yes");
        GridView1.DataSource = d.show();
        GridView1.DataBind();
    }
}




[3/29/2010 7:52:12 PM] sandeep karan: http://msdn.microsoft.com/en-us/library/ms998549.aspx
[3/29/2010 9:15:56 PM] sandeep karan: using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Data.Linq;
using System.Data.Linq.Mapping;     

public class Class2:DataContext
{
 public Class2(string c):base(c)

 {
 
 }
    [Function(Name="sel_prod")]
    public ISingleResult<Class1> show()
    {
      IExecuteResult res=this.ExecuteMethodCall(this,(MethodInfo)MethodInfo.GetCurrentMethod());
      ISingleResult<Class1> s = (ISingleResult<Class1>)res.ReturnValue;
      return s;
    }
}
[3/29/2010 9:20:01 PM] sandeep karan: using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Data.Linq;
using System.Data.Linq.Mapping;

public class DAL:DataContext
{
    public DAL(string connection)
        : base(connection)
    {

    }


    [Function(Name="usp_SelectCustomer")]
    public ISingleResult<st>  getdata()
    {
       IExecuteResult obj=this.ExecuteMethodCall(this, (MethodInfo)MethodInfo.GetCurrentMethod());
       ISingleResult<st> ret=(ISingleResult<st>)obj.ReturnValue;
       return ret;
    }
    [Function(Name = "customerbyid")]
    public ISingleResult<st> getdataid([Parameter(Name = "cid", DbType = "Int")] System.Nullable<int> id)

    {
        IExecuteResult obj = this.ExecuteMethodCall(this, (MethodInfo)MethodInfo.GetCurrentMethod(),id);
        ISingleResult<st> ret = (ISingleResult<st>)obj.ReturnValue;
        return ret;
    }
   
    [Function(Name = "insertc")]
    public int InsertCustomer([Parameter(Name = "Cid", DbType = "Int")]
 int  CustId, [Parameter(Name = "Cname",DbType = "nvarchar(20)")]string custname)
    {
        IExecuteResult result = this.ExecuteMethodCall(this,
        ((MethodInfo)(MethodInfo.GetCurrentMethod())), CustId, custname);
        return (int)result.ReturnValue;
    }
}


No comments:

Post a Comment