Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

Swapneel
baskar
More...


Forums » .NET » LINQ »

How to perform LINQ query on a SQL server database?


Posted Date: 07 Feb 2012      Posted By:: KUNDAN KUMAR SRIVASTAVA     Member Level: Gold    Member Rank: 146     Points: 1   Responses: 4



Can LINQ query work on SQL tables ?

If yes then what are the syntax to perform the LINQ query on a SQL server database.

Actually I have a table called 'student' with two fields 'Name' and 'Marks'.

So kindly provide the LINQ syntax.

Thanks,
Kundan




Responses

#656052    Author: Prasad kulkarni        Member Level: Diamond      Member Rank: 8     Date: 07/Feb/2012   Rating: 2 out of 52 out of 5     Points: 0

here is small demo, check following link
http://www.codeproject.com/Articles/188935/LINQ-Demo-with-ASP-NET-Web-Application

Thanks
Koolprasd2003
[DotNetSpider MVM]



 
#656071    Author: Naveen Reddy      Member Level: Gold      Member Rank: 16     Date: 08/Feb/2012   Rating: 2 out of 52 out of 5     Points: 4

Hello,
LINQ is a Language Integrated Query, It is Unified technologies to qurying the differenct sources line Object, Var,XML data or SQl server. Below is the sample queries.

Selecting the data based on the condition username and password


DBNameDataContext myDataBase = new DBNameDataContext();
var userResults = from u in myDataBase.Users
where u.Username == userName
&& u.Password == passWord
select u;
return Enumerable.Count(userResults) > 0;


Listing out the users who is greater than 35 years.



DBNameDataContext Mydatabase = new DBNameDataContext();

var q = from c in Mydatabase.Contact
where c.UserDateOfBirth.AddYears(35) > DateTime.Now
orderby c.UserDateOfBirth descending
select c;

foreach (var c in q)
Console.WriteLine("{0} {1} b.{2}",
c.FirstName.Trim(),
c.LastName.Trim(), c.UserDateOfBirth.ToString("dd-MMM-yyyy"));


I hope this may be helpful to you.

Regards,
Naveen



 
#656252    Author: Dharmaraj Nagarajan        Member Level: Gold      Member Rank: 13     Date: 08/Feb/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

1. Switch to Visual Studio.
2. Go to File, point to New and click on Project.
3. From the left Pane, choose Visual C# | Windows, and select the "Console Application" template.
4. Leave the default parameters and hit OK.
5. On Solution Explorer, right click on ConsoleApplication1 project and choose Add->New Item.
6. In the Add New Item dialog, choose "LINQ to SQL Classes". Set the file name to Northwind.dbml and hit OK.
7. Expand Server Explorer.
8. Choose the Customers table and drag it to the main designer area.
9. Choose the Orders table and drag it to the main designer area.
10. Drag the CustOrdersDetail stored procedure to the design area on the right
11. In Solution Explorer, double click on the Program.cs file.
12. Type inside the Main method:


using (NorthwindDataContext db = new NorthwindDataContext())
{
var q = from c in db.Customers
where c.City == "London"
select c;
}


All is well.
Thanks,
Dharma
Editor,Mentor,MVM
Try and fail but don't fail and try
Me and DNS






 
#659242    Author: Sravani      Member Level: Bronze      Member Rank: 0     Date: 25/Feb/2012   Rating: 2 out of 52 out of 5     Points: 4

-First create a database
-Open VS.Net and create a website and add LinqtoSql classes to that.
-Drag and drop the tables into the LinqToSql classes.
-Then add a web form to the project.
-Create your interface:
<div>
<asp:GridView ID="gvUser" runat="server"></asp:GridView>
<br/>

<label>UserName:</label>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<br/>
<label>PhoneNo:</label>
<asp:TextBox ID="txtPhoneNo" runat="server"></asp:TextBox>
<br/>
<label>Address:</label>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnInsert" runat="server" Text="Insert" onclick="btnInsert_Click" />
</div>

-Use the datacontext object to access the tables which are created as classes in your designer.cs file.

Code behind file:

DataClassesDataContext dc = new DataClassesDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvUser.DataSource = GetUserDetails();
gvUser.DataBind();
}
}

protected void btnInsert_Click(object sender, EventArgs e)
{
InsertUser();
gvUser.DataBind();
Response.Redirect(Request.RawUrl);
}

-Methods for linq queries:

public IEnumerable<User> GetUserDetails()
{
return dc.Users;
}
public void InsertUser()
{
User newUser = new User();

newUser.UserName = txtUserName.Text;
newUser.PhoneNo = Convert.ToInt32(txtPhoneNo.Text);
newUser.Address = txtAddress.Text;
dc.Users.InsertOnSubmit(newUser);
dc.SubmitChanges();
}

For more Information follow the below link:
http://www.bestdotnettraining.com/Online/Training/LINQ/LINQ-to-SQL/109



 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : How to get the employee information using linq from the list
Previous : How to bind data to dropdownlist in c#
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.