Click here to Skip to main content
15,881,856 members
Articles / Programming Languages / C#

How To Run SQL Statement from LINQ

Rate me:
Please Sign up or sign in to vote.
1.82/5 (6 votes)
30 Mar 2009CPOL 49.7K   7   11
An article on how to run SQL statement from LINQ

Introduction

Sometimes we need to run a SQL statement from LINQ. This article will show how to do this.

Using the Code

First you create a *.dbml file for mapping your database from 'Add New Item' in Visual Studio.NET 2008. As you know, a query in LINQ is different from a query in SQL. Here I create a method called GetMember which contains a SQL statement as a string which I need to execute from LINQ, and returns a list of members which make this method as datasource to grid view later.

I create this method for searching members by any parameter entered and with each other:

C#
GeoDataContext MemberConnection = new GeoDataContext(); //connection
public List<Member> GetMemberSearchAll(string name, string fname, string MemberNo,
    string QualificationClass, string WorkClass, string GeoID)
{
    var sql = @"SELECT DISTINCT * FROM dbo.Member where MemberCode <> 0";
    if (name != "")
    {
        sql = sql + " and (dbo.Member.MemberName like '%{0}%')";
    }
    if (fname != "")
    {
        sql = sql + "and (dbo.Member.MemberFameName like '%{1}%')";
    }
    if (MemberNo != "")
    {
        sql = sql + "and (dbo.Member.MemberNo = {2})";
    }
    if (QualificationClass != "")
    {
        sql = sql + " and (dbo.Member.QualificationClass = {3})";
    }
    if (WorkClass != "")
    {
        sql = sql + " and (dbo.Member.WorkClass = {4})";
    }
    if (GeoID != "")
    {
        sql = sql + "and (dbo.Member.GeoID ={5})";
    }
    sql = string.Format(sql, name, fname, MemberNo, 
		QualificationClass, WorkClass, GeoID);
    var quary = MemberConnection.ExecuteQuery<Member>(sql);
    return quary.ToList();
}

Here I first formulate strings by both SQL statement and parameter by order, then I execute the method called ExecuteQuery from my connection which takes a parameter as a SQL query. Its summary in documentation of Visual Studio is "Executes SQL queries directly on the database and returns objects" then pass this SQL formatted to it.

Thanks, I hope this will help somebody.

History

  • 30th March, 2009: Initial post 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 1 Pin
Howard Richards26-Jan-15 6:00
Howard Richards26-Jan-15 6:00 
GeneralMy vote of 1 Pin
Richard Deeming7-Apr-09 9:15
mveRichard Deeming7-Apr-09 9:15 
GeneralMy vote of 2 Pin
Member 44820466-Apr-09 21:13
Member 44820466-Apr-09 21:13 
GeneralSQL Injection Pin
Thomas Gerber31-Mar-09 11:58
Thomas Gerber31-Mar-09 11:58 
GeneralRe: SQL Injection there is no injection Pin
Ahmed Farag Ibrahim1-Apr-09 7:16
Ahmed Farag Ibrahim1-Apr-09 7:16 
GeneralRe: SQL Injection there is no injection Pin
Thomas Gerber1-Apr-09 9:06
Thomas Gerber1-Apr-09 9:06 
GeneralRe: SQL Injection there is no injection [modified] Pin
Ahmed Farag Ibrahim12-Apr-09 7:09
Ahmed Farag Ibrahim12-Apr-09 7:09 
GeneralMy vote of 1 Pin
Win Myan30-Mar-09 17:49
Win Myan30-Mar-09 17:49 
GeneralRe: My vote of 1 Pin
Ahmed Farag Ibrahim31-Mar-09 0:51
Ahmed Farag Ibrahim31-Mar-09 0:51 
GeneralRe: My vote of 1 Pin
TimMerksem7-Apr-09 9:24
TimMerksem7-Apr-09 9:24 
GeneralRe: My vote of 1 Pin
Ahmed Farag Ibrahim8-Apr-09 2:09
Ahmed Farag Ibrahim8-Apr-09 2:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.