Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Introduction to database interaction with C#

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!

like image 527
JimDaniel Avatar asked Apr 17 '09 19:04

JimDaniel


2 Answers

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:

  • ADO.NET at Wikipedia
  • ADO.NET Entity Framework at Wikipedia

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:

  • Data Access Application Block at MSDN
  • The ADO.NET Entity Framework Overview at MSDN
  • Learn ADO.NET Entity Framework ("How Do I" videos and more)
  • Videos, Screencasts, Tutorials at ASP.NET
  • Microsoft SQL Server 2008: ADO.NET Entity Framework

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:

  • ADO.NET Team Blog at MSDN

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:

  • Microsoft SQL Server 2008 T-SQL Fundamentals by Itzik Ben-gan (Oct, 2008)
  • Programming Entity Framework by Julia Lerman (Feb, 2009)
  • Murach's ADO.NET 3.5, LINQ, and the Entity Framework by Anne Boehm (Mar, 2009)
like image 187
10 revs, 2 users 99% Avatar answered Sep 20 '22 01:09

10 revs, 2 users 99%


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.

  • W3 Schools SQL Tutorial
  • Database Design Tutorial

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();
            }
        }
    }
}
like image 23
TheTXI Avatar answered Sep 21 '22 01:09

TheTXI