Basic Database Operations Using C#
Last Updated :
31 Jan, 2023
In this article, you are going to learn about how to perform basic database operations using system.data.SqlClient namespace in C#. The basic operations are INSERT, UPDATE, SELECT and DELETE. Although the target database system is SQL Server Database, the same techniques can be applied to other database systems because the query syntax used is standard SQL that is generally supported by all relational database systems.
Prerequisites: Microsoft SQL Server Management Studio
Open Microsoft SQL Server Management Studio and write the below script to create a database and table in it.
create database Demodb;
use Demodb;
CREATE TABLE demo(
articleID varchar(30) NOT NULL PRIMARY KEY,
articleName varchar(30) NOT NULL,
);
insert into demo values(1, 'C#');
insert into demo values(2, 'C++');
After executing the above script following table named demo is created and it contains the following data as shown in the screenshot.

Connecting C# with Database: To work with a database, the first of all you required a connection. The connection to a database normally consists of the below-mentioned parameters.
- Database name or Data Source: The database name to which the connection needs to be set up and connection can be made or you can say only work with one database at a time.
- Credentials: The username and password which needs to be used to establish a connection to the database.
- Optional Parameters: For each database type, you can specify optional parameters to provide more information on how .NET should connect to the database to handle the data.
Note: Here, we are using command prompt to execute these codes. To see the result, you can use the Microsoft SQL Server Management Studio.
Code 1#: Connecting with database in C#
csharp
// C# code to connect the database
using System;
using System.Data.SqlClient;
namespace Database_Operation {
class DBConn {
// Main Method
static void Main()
{
Connect();
Console.ReadKey();
}
static void Connect()
{
string constr;
// for the connection to
// sql server database
SqlConnection conn;
// Data Source is the name of the
// server on which the database is stored.
// The Initial Catalog is used to specify
// the name of the database
// The UserID and Password are the credentials
// required to connect to the database.
constr = @"Data Source=DESKTOP-GP8F496;Initial Catalog=Demodb;User ID=sa;Password=24518300";
conn = new SqlConnection(constr);
// to open the connection
conn.Open();
Console.WriteLine("Connection Open!");
// to close the connection
conn.Close();
}
}
}
Output:
Connection Open !
Code #2: Using Select Statement and SqlDataReader for accessing the data in C#
csharp
// C# code to demonstrate how
// to use select statement
using System;
using System.Data.SqlClient;
namespace Database_Operation {
class SelectStatement{
// Main Method
static void Main()
{
Read();
Console.ReadKey();
}
static void Read()
{
string constr;
// for the connection to
// sql server database
SqlConnection conn;
// Data Source is the name of the
// server on which the database is stored.
// The Initial Catalog is used to specify
// the name of the database
// The UserID and Password are the credentials
// required to connect to the database.
constr = @"Data Source=DESKTOP-GP8F496;Initial Catalog=Demodb;User ID=sa;Password=24518300";
conn = new SqlConnection(constr);
// to open the connection
conn.Open();
// use to perform read and write
// operations in the database
SqlCommand cmd;
// use to read a row in
// table one by one
SqlDataReader dreader;
// to store SQL command and
// the output of SQL command
string sql, output = "";
// use to fetch rows from demo table
sql = "Select articleID, articleName from demo";
// to execute the sql statement
cmd = new SqlCommand(sql, conn);
// fetch all the rows
// from the demo table
dreader = cmd.ExecuteReader();
// for one by one reading row
while (dreader.Read()) {
output = output + dreader.GetValue(0) + " - " +
dreader.GetValue(1) + "\n";
}
// to display the output
Console.Write(output);
// to close all the objects
dreader.Close();
cmd.Dispose();
conn.Close();
}
}
}
Output:
1 - C#
2 - C++
Code #3: Inserting the data into the database using Insert Statement in C#
csharp
// C# code for how to use Insert Statement
using System;
using System.Data.SqlClient;
namespace Database_Operation {
class InsertStatement {
// Main Method
static void Main()
{
Insert();
Console.ReadKey();
}
static void Insert()
{
string constr;
// for the connection to
// sql server database
SqlConnection conn;
// Data Source is the name of the
// server on which the database is stored.
// The Initial Catalog is used to specify
// the name of the database
// The UserID and Password are the credentials
// required to connect to the database.
constr = @"Data Source=DESKTOP-GP8F496;Initial Catalog=Demodb;User ID=sa;Password=24518300";
conn = new SqlConnection(constr);
// to open the connection
conn.Open();
// use to perform read and write
// operations in the database
SqlCommand cmd;
// data adapter object is use to
// insert, update or delete commands
SqlDataAdapter adap = new SqlDataAdapter();
string sql = "";
// use the defined sql statement
// against our database
sql = "insert into demo values(3, 'Python')";
// use to execute the sql command so we
// are passing query and connection object
cmd = new SqlCommand(sql, conn);
// associate the insert SQL
// command to adapter object
adap.InsertCommand = new SqlCommand(sql, conn);
// use to execute the DML statement against
// our database
adap.InsertCommand.ExecuteNonQuery();
// closing all the objects
cmd.Dispose();
conn.Close();
}
}
}
Output:

Code #4: Updating the data into the database using Update Statement in C#
csharp
// C# code for how to use Update Statement
using System;
using System.Data.SqlClient;
namespace Database_Operation {
class UpdateStatement {
// Main Method
static void Main()
{
Update();
Console.ReadKey();
}
static void Update()
{
string constr;
// for the connection to
// sql server database
SqlConnection conn;
// Data Source is the name of the
// server on which the database is stored.
// The Initial Catalog is used to specify
// the name of the database
// The UserID and Password are the credentials
// required to connect to the database.
constr = @"Data Source=DESKTOP-GP8F496;Initial Catalog=Demodb;User ID=sa;Password=24518300";
conn = new SqlConnection(constr);
// to open the connection
conn.Open();
// use to perform read and write
// operations in the database
SqlCommand cmd;
// data adapter object is use to
// insert, update or delete commands
SqlDataAdapter adap = new SqlDataAdapter();
string sql = "";
// use the define sql
// statement against our database
sql = "update demo set articleName='django' where articleID=3";
// use to execute the sql command so we
// are passing query and connection object
cmd = new SqlCommand(sql, conn);
// associate the insert SQL
// command to adapter object
adap.InsertCommand = new SqlCommand(sql, conn);
// use to execute the DML statement against
// our database
adap.InsertCommand.ExecuteNonQuery();
// closing all the objects
cmd.Dispose();
conn.Close();
}
}
}
Output:

Code #5: Deleting the data into the database using Delete Statement in C#
csharp
// C# code for how to use Delete Statement
using System;
using System.Data.SqlClient;
namespace Database_Operation {
class DeleteStatement {
// Main Method
static void Main()
{
Delete();
Console.ReadKey();
}
static void Delete()
{
string constr;
// for the connection to
// sql server database
SqlConnection conn;
// Data Source is the name of the
// server on which the database is stored.
// The Initial Catalog is used to specify
// the name of the database
// The UserID and Password are the credentials
// required to connect to the database.
constr = @"Data Source=DESKTOP-GP8F496;Initial Catalog=Demodb;User ID=sa;Password=24518300";
conn = new SqlConnection(constr);
// to open the connection
conn.Open();
// use to perform read and write
// operations in the database
SqlCommand cmd;
// data adapter object is use to
// insert, update or delete commands
SqlDataAdapter adap = new SqlDataAdapter();
string sql = "";
// use the define SQL statement
// against our database
sql = "delete from demo where articleID=3";
// use to execute the sql command so we
// are passing query and connection object
cmd = new SqlCommand(sql, conn);
// associate the insert SQL
// command to adapter object
adap.InsertCommand = new SqlCommand(sql, conn);
// use to execute the DML statement
// against our database
adap.InsertCommand.ExecuteNonQuery();
// closing all the objects
cmd.Dispose();
conn.Close();
}
}
}
Output:

Similar Reads
C# Tutorial C# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read
Introduction to .NET Framework The .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
6 min read
C# Interview Questions and Answers C# is the most popular general-purpose programming language and was developed by Microsoft in 2000, renowned for its robustness, flexibility, and extensive application range. It is simple and has an object-oriented programming concept that can be used for creating different types of applications.Her
15+ min read
C# Dictionary Dictionary in C# is a generic collection that stores key-value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of a Dictionary is, that it is a generic type. A dictionary is defined under System.Collections.Generic namespace. It is dynamic in nature mean
5 min read
C# List Class In C#, the List<T> class represents the list of objects that can be accessed by index. It comes under the System.Collections.Generic namespace. List class can be used to create a collection of different types like integers, strings, etc. List<T> class also provides the methods to search,
7 min read
C# Delegates A delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. It provides a way which tells which method is to be called when an event is triggered. For example, if you click on a Button on a form (Windows Form application),
6 min read
ASP.NET Interview Questions and Answer ASP.NET is a popular framework by Microsoft for building fast and scalable web applications. It allows developers to create dynamic websites, services, and apps, using server-side code and offering a user-friendly experience. Trusted by companies like Microsoft, Dell, and Accenture, ASP.NET is used
15+ min read
C# .NET Framework (Basic Architecture and Component Stack) C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft in 2000. It is a part of the .NET ecosystem and is widely used for building desktop, web, mobile, cloud, and enterprise applications. This is originally tied to the .NET Framework, C# has evolved to be the primary
6 min read
C# Data Types Data types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
7 min read
C# Arrays An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read