views:

679

answers:

6

Up to now in my programming career (two years) I have not had much database experience, but the company where I now work uses databases extensively for their product, and I feel behind the curve.

So I would like to know how best to start learning database interaction with C#. I've read about LINQ-to-SQL and ADO.net. Are these the right technologies to look into?

Where do I start?

EDIT: Thanks for all the responses. There were many good ones - I had a hard time choosing one as "the" answer. This helps me greatly!

+10  A: 

I would suggest investing your time in learning Microsoft SQL Server itself, Data Access Application Block from Enterprise Library and ADO.NET Entity Framework.

Entry point for learning SQL Server is here -> SQL Server Developer Center
Entry point for learning ADO.NET is here -> Learning ADO.NET at MSDN

First of all, in order to gain a good understanding of what ADO.NET is, check the links below:

Learn how to write direct queries in C# to SQL Server without using any frameworks and ORM tools, then proceed to learning more advanced technologies in ADO.NET family.

See also:

You may also want to download LINQPad, which is perfect tool for playing with LINQ.

Also I suggest subscribing to ADO.NET related RSS feeds:

Also check existing open source projects at CodePlex.com which use these technologies and digg into their source codes.

Great books on the subject for you:

Koistya Navin
Be aware that Linq To SQL has been suspended, so you'll be better off in the long run focussing on the Entity Framework.
madcolor
Yeah, ADO.NET is a must-learn if you're to do anything database-oriented in .NET. All other data frameworks are based on top of it (Entities, LINQ to Datasets, LINQ to SQL, etc.). Of course, ADO.NET can be used by itself, and this is quite acceptable for small projects/minor databases, though you'll probably what to go with the Entities framework for anything larger, ORM (Object Relational Mappers) being favoured nowadays.
Noldorin
+6  A: 

UPDATE: One thing this answer didn't have in the past is links to information for SQL and database newbies, so I will put some relevant links here as well so that you (or anyone else) can brush up on their SQL and other database design skills.

A lot of this is taken from another answer I have written today, but it goes into detail about your exact issues:

Original Answer:

This sounds like you more or less need a basic introduction to connecting and manipulating a database from C#. The above poster said to look into LINQ to SQL, but you can also look into the more basic underlying framework of ADO.NET which will get you to understand the basics of how it works.

Also, you can use this site right here for a number of different database tutorials for C#.

Edit: More information from C# Station, CodeProject, and Codersource

Edit 2: If you are interested in things like Linq to SQL as others have mentioned above, here are some tutorials from C# Corner, and C-Sharp Online

Edit 3: Others would also suggest things such as the ADO.NET Entity Framework. I would not necessarily suggest this for beginners who still need to grasp the fundamentals of working with a database. Here is some information from the MSDN Overview

Simple Example (This is pulled directly from the C# Station link given above)

Listing 1. Using a SqlConnection

using System;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Demonstrates how to work with SqlConnection objects
/// </summary>
class SqlConnectionDemo
{
    static void Main()
    {
        // 1. Instantiate the connection
        SqlConnection conn = new SqlConnection(
            "Data Source=(local);Initial Catalog=Northwind;
             Integrated Security=SSPI");

        SqlDataReader rdr = null;

        try
        {
            // 2. Open the connection
            conn.Open();

            // 3. Pass the connection to a command object
            SqlCommand cmd = 
                new SqlCommand("select * from Customers", conn);

            //
            // 4. Use the connection
            //

            // get query results
            rdr = cmd.ExecuteReader();

            // print the CustomerID of each record
            while (rdr.Read())
            {
                Console.WriteLine(rdr[0]);
            }
        }
        finally
        {
            // close the reader
            if (rdr != null)
            {
                rdr.Close();
            }

            // 5. Close the connection
            if (conn != null)
            {
                conn.Close();
            }
        }
    }
}
TheTXI
+1: LINQ and Entities are many layers above ADO.NET. If you're just starting out with database programming you really should start with the core System.Data classes and work your way up from there through the Enterprise Library wrappers.
Dave Swersky
Dave: I agree whole heartedly. All too often we see people recommend solutions, tools, frameworks, etc. to users who barely understand the basics. That just leaves the door wide open to make huge mistakes because you don't understand what is going on underneath all those layers.
TheTXI
+2  A: 

Personally, I think it is good to understand what it is these "frameworks" do. I would start with some basic queries to attempt to get a handle on how SQL works. You may not end up using it that much once you get started, but I've always found it is easier to understand why I need a framework if I can understand what it is doing for me.

Nick DeVore
Agreed (and I want to vote up but I'm out for the rest of the day). People forget to preach the basics before evangelizing the new products which are supposed to make everything so much better.
TheTXI
A: 

First pick up any sql books from Ben Forta. Once you have the basic fundamentals, then you can get on with understanding the libraries, blocks and other frameworks. It will help you a lot in your career to have a good understanding of databases.

CodeToGlory
+1  A: 

Learn database fundamentals and t-sql first before even considering using LINQ. If you don't understand how joins work and how databases should be structured you cannot effectively query using any tool.

Here are some links to help: http://www.tek-tips.com/faqs.cfm?fid=4785 http://www.deeptraining.com/litwin/dbdesign/FundamentalsOfRelationalDatabaseDesign.aspx

HLGEM
+2  A: 

Yes, you have nailed down the right technologies.

I have some fantastic and free pdfs/eBooks for you to look at:

ADO.NET

http://www.murach.com/books/adon/chapters.htm (vb and ado.net

http://www.springerlink.com/content/w2126101r8qr2052/ (ado.net fundamentals with c#)

http://archive.visualstudiomagazine.com/books/chapters/1590595122.pdf (vb & c#)

http://dotnet.jku.at/courses/tutorial/05.ADO.NET.pdf (great overview powerpoint)

http://docs.msdnaa.net/ark_new3.0/cd3/content/Courses%5CVargas%5CCh12.pdf (ado.net book)

http://media.wiley.com/product_data/excerpt/38/07821418/0782141838-1.pdf (nice)

LINQ-TO-SQL Scott Gu's Linq to Sql is probably all you need for a good start:

http://it-box.blogturk.net/wp-content/themes/it-box/files/LINQToSql.pdf

Pita.O