Solved

Getting Started Using Linq in C# Web Application

Posted on 2010-11-30
8
441 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
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!

 
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

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!

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

739 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