Linq to SQL Connection in asp.net Csharp

29. June 2010

In this Example, I have Create a connecton string and establish a connection between object to database. and Select title from employees table.

string connectionString = "Data Source=.;Initial Catalog=Northwind;Integrated Security=True";
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (NorthwindDataContext context = new NorthwindDataContext(connection))
            {
                var query = from c in context.employees
                            select c.Title;

                foreach (var title in query)
                {
                    Response.Write(title);
                }
            }


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net, asp.net 4.0 ,

get Max Value With LINQ To SQL

27. June 2010

In MS SQL Syntax

SELECT MAX(COLUMN NAME) FROM [TABLE NAME]

 

In Linq to SQL Syntax

var maxValue = ( from q in [TABLE NAME] SELECT (P.[COLUMN NAME]).Max()

 

 



Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL ,

Custom Paging in Repeater Using LINQ in asp.net csharp - Part III

6. June 2010

There is no custom paging in Repeater Control developer have to create a custom paging functions.By using Linq there are two method to returns a no of record from a whole list.Method are

Take() and Skip(), Take() Method to take number of records from a whole records on which table you are applying , and Skip Method is use to skip a number of records from whole table on which you have apply a Linq query,LINQ has a set of querying operators that can be used to query in memory object collection, Sql database, XML, etc.To create pages using Linq.

Reference:

To read full article with Example Click here http://www.aspxtutorial.com/post/2010/06/06/Custom-Paging-in-Repeater-Using-LINQ-Part-III.aspx

Demo


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net , , ,

Improve performance in LINQ to SQL

15. February 2010

LINQ to SQL you can use CompiledQuery to improve performance.


public static Func<Northwnd, string, IQueryable<Customer>>
    CustomersByCity =
        CompiledQuery.Compile((Northwnd db, string city) =>
            from c in db.Customers where c.City == city select c);

public static Func<Northwnd, string, IQueryable<Customer>>
    CustomersById = CompiledQuery.Compile((Northwnd db,
    string id) => db.Customers.Where(c => c.CustomerID == id));

public IEnumerable<Customer> GetCustomersByCity(string city)
{
    var myDb = GetNorthwind();
    return Queries.CustomersByCity(myDb, city);
}


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net, SQL 2005 & 2008 , ,

Using Stored Procedures In Linq using csharp

16. January 2010

Creating Data Access Layer Using LINQ To SQL - Stored Procedures

Using Linq to SQL, you can use stored procedure to return your entities.This gives us the advantage of modularity, performance, and security to name a few. We can use these stored procedures with LINQ to SQL. LINQ to SQL maps these database-defined abstractions to code-generated client objects If you want to use the designer, you can go to the server explorer and drag a stored procedure on an existing entity. This would cause the Linq to SQL designer to generate code for calling a stored procedure that returns your entities.Using LINQ you can also call stored procedures when creating a data layer. The LINQ to SQL object relational mapping that ships in .NET 3.5 is flexible and can be used to create data model classes whose object model can be independent of the underlying database schema. It encapsulate business logic and validation rules that work regardless of whether the data model is populated via dynamic SQL or via Stored Procedures.using Stored Procedures with LINQ To SQL is as simple as dragging a Stored Procedure from the Server Explorer Tool to your Visual Designer Surface. Since the Stored Procedure in question, getProvince, generates a list of countryName Classes, I drag the stored procedure from the Server Explorer and drop it on the getProvincel Class on the Visual Designer. This tells the visual designer that the resultset of the stored procedure is a list of Province

Detail Classes. You will then see the stored procedure off to the right of the Visual Designer letting you know it has been added to the DataContext.

    The drag-and-drop operation ends up creating a method on your DataContext Class:
    [Function(Name="dbo.getProvince")]
    public ISingleResult<getProvinceResult> getProvince([Parameter(DbType="BigInt")] System.Nullable<long> country_id)
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), country_id);
        return ((ISingleResult<getProvinceResult>)(result.ReturnValue));
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        var db = new DataClassesDataContext();
        var getall = from c in db.getProvince(1)
                     select c;


        foreach (var f in getall)//Written by Aamir Hasan
        {
            Response.Write(f.pname+"<br>");//studentacad.com
        }
    }


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net, asp.net 4.0, Linq , ,



User Name: Guest

Your Ip: 38.107.191.91
Time: