C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Connection: Connect To The Database

In this tutorial, we'll show you how to connect to a database using ADO.NET SqlConnection in C#. We'll use SQL Server as an example, but the process is similar for other databases supported by ADO.NET.

  • Set up the environment

To start, create a new C# Console Application project in Visual Studio. You'll need to install the System.Data.SqlClient NuGet package for SQL Server support:

Install-Package System.Data.SqlClient
  • Add the necessary namespaces

Add the following namespaces to your project:

using System;
using System.Data;
using System.Data.SqlClient;
  • Create a connection string

Create a connection string that includes the necessary credentials to connect to your SQL Server instance:

string connectionString = "Server=localhost;Database=myDatabase;User Id=myUser;Password=myPassword;";

Replace localhost, myDatabase, myUser, and myPassword with the appropriate values for your SQL Server instance.

  • Connect to the database

Now, create a new SqlConnection object and open the connection to the database:

SqlConnection connection = new SqlConnection(connectionString);

try
{
    connection.Open();
    Console.WriteLine("Connected to the database successfully.");
}
catch (SqlException ex)
{
    Console.WriteLine("Error connecting to the database: " + ex.Message);
}
finally
{
    connection.Close();
}
  • Test the connection

Run your Console Application, and you should see the message "Connected to the database successfully." If there is an error, you'll see an error message with details about the issue.

This tutorial shows you the basics of connecting to a SQL Server database using ADO.NET SqlConnection in C#. To work with data in the database, you'll need to use other ADO.NET components like SqlCommand, SqlDataReader, and SqlDataAdapter.

  1. C# Connect to Database Example:

    using System;
    using System.Data.SqlClient;
    
    class Program
    {
        static void Main()
        {
            // Database connection string
            string connectionString = "YourConnectionStringHere";
    
            // Create a SqlConnection
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                try
                {
                    // Open the database connection
                    connection.Open();
    
                    // Perform database operations here
    
                    // Close the connection
                    connection.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                }
            }
        }
    }
    
  2. Database Connection String in C#:

    • The connection string contains information required to connect to the database. It includes details like the server, database name, authentication, etc.
    string connectionString = "Server=myServerAddress;Database=myDatabase;User Id=myUsername;Password=myPassword;";
    
  3. C# SqlConnection for Database Connection:

    • Use the SqlConnection class to represent a connection to a SQL Server database.
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        // Connection-related code here
    }
    
  4. C# OleDbConnection for Connecting to Access Database:

    • For connecting to an Access database, use OleDbConnection.
    • The connection string will include details like the provider, data source, and any required credentials.
    using System.Data.OleDb;
    
    string accessConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Path\\To\\Your\\Database.accdb;Persist Security Info=False;";
    using (OleDbConnection accessConnection = new OleDbConnection(accessConnectionString))
    {
        // Connection-related code here
    }