Solved

Getting Started Using Linq in C# Web Application

Posted on 2010-11-30
8
439 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 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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