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
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.
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 following namespaces to your project:
using System; using System.Data; using System.Data.SqlClient;
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.
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(); }
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.
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); } } } }
Database Connection String in C#:
string connectionString = "Server=myServerAddress;Database=myDatabase;User Id=myUsername;Password=myPassword;";
C# SqlConnection for Database Connection:
SqlConnection
class to represent a connection to a SQL Server database.using (SqlConnection connection = new SqlConnection(connectionString)) { // Connection-related code here }
C# OleDbConnection for Connecting to Access Database:
OleDbConnection
.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 }