Solved

Getting Started Using Linq in C# Web Application

Posted on 2010-11-30
8
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 63

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 63

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 63

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

735 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