Home
C#
SqlConnection Example
Updated Dec 24, 2024
Dot Net Perls
SqlConnection. The C# SqlConnection class handles database connections. It initiates a connection to your SQL database. This class is best used in a using resource acquisition statement.
More details. We call Open to query the database with SqlCommand. With System.Data.SqlClient classes, we often use many together (like SqlParameter, SqlDataReader).
SqlCommand
SqlClient
SqlDataReader
OdbcConnection
First example. We use SqlConnection in a "using" statement. The SqlConnection has a constructor that requires a string reference pointing to the connection string character data.
Tip This connection string is often generated for you by the dialogs in Visual Studio, and is sometimes provided by a host.
And You must include the SqlConnection code before you can perform a database query.
using System;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        //
        // First access the connection string.
        //
        string connectionString = ConsoleApplication1.Properties.Settings.Default.ConnectionString;
        //
        // In a using statement, acquire the SqlConnection as a resource.
        //
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            //
            // Open the SqlConnection.
            //
            con.Open();
            //
            // This code uses an SqlCommand based on the SqlConnection.
            //
            using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Dogs1", con))
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine("{0} {1} {2}", reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                }
            }
        }
    }
}
57 Koko Shar Pei 130 Fido Bullmastiff
Program notes. This program will not work unless you change the connection string reference to point to a correct one in your environment.
Detail To create a connection string to a database, go to the Visual Studio Data menu and select Add New Data Source.
Also The program assumes the name of an SQL table that will not be present in most databases.
Important The using statement is a way to specify when the unmanaged resource is needed by your program, and when it is no longer needed.
using
Open method call. The using statement creates a read-only variable of type SqlConnection. You need to call Open() on the SqlConnection instance before using it in an SqlCommand.
And The SqlConnection is passed as the parameter to the SqlCommand. In this way we specify that the SqlCommand "uses" the SqlConnection.
Summary. We looked at database code. We stressed the proper usage of the SqlConnection class and the resource acquisition pattern.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 24, 2024 (simplify).
Home
Changes
© 2007-2025 Sam Allen