Solved

Getting Started Using Linq in C# Web Application

Posted on 2010-11-30
8
435 Views
Last Modified: 2012-05-10
I am programming a internal database web application in Visual Studio 2010 with C#/ASP.NET.  I am fairly familiar with SQL and ADO.net, but I would like to try LINQ because I've heard it can make my job easier.

I have succeeded in connecting to my database using ADO.net.  But I don't even know where to begin with LINQ.  LINQ doesn't use "connection strings," does it?

I've added a connection to my database in Visual Studio's "Data Connections" and I can see all my databases and tables.  And from my web searches it looks like I need to add some kind of reference to a "data context" to the database I want to access with LINQ, but how do I go about doing that?  Do I need to add something to web.config?  Or add a class to code behind...?  I've read about DBML files, is this what I need to generate?
0
Comment
Question by:KTN-IT
  • 3
  • 3
  • 2
8 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34239558
You need to create a Linq-To-Sql model, or an Entity Framework model, and then run your Linq queries against that.

This should help you get started:

    http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx
0
 
LVL 5

Author Comment

by:KTN-IT
ID: 34239664
I've looked at this page many times.  Am I supposed to find a "Linq to SQL" designer in Visual Studio?  Where might I find that?
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34239719
Hi KTN-IT;

To your question, " LINQ doesn't use "connection strings," does it?", Linq does use a connection string. It uses by default the connection string that was created when you established a connection from Visual Studio IDE to the database. This can be changed by adding a connection string to the constructor of the datacontext when you instantiate one.

To your question,  "I've read about DBML files, is this what I need to generate?", You need to create a DBML file.

To create the model that maps the class in code to the columns in a database follow the steps in this article.

LINQ to SQL (Part 2 - Defining our Data Model Classes)
http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

Fernando
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34239735
Right-click your project, choose Add > New Item. In the dialog that appears choose, under Data, choose "LINQ to SQL Classes" or "ADO.NET Entity Data Model" depending on which you want to use.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34239776
See the linq I posted above for how to use the designer.
0
 
LVL 5

Author Closing Comment

by:KTN-IT
ID: 34240456
Thanks again!
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34240587
Did you read at all the link I post?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34240610
The link you posted was the same one i posted earlier!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now