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#
Connection Open !
Code #2: Using Select Statement and SqlDataReader for accessing the data in C#
1 - C# 2 - C++
Code #3: Inserting the data into the database using Insert Statement in C#
Code #4: Updating the data into the database using Update Statement in C#
Code #5: Deleting the data into the database using Delete Statement in C#
- Basic CRUD (Create, Read, Update, Delete) in ASP.NET MVC Using C# and Entity Framework
- C# | .NET Framework (Basic Architecture and Component Stack)
- Basics Operations of File and Directory in C#
- How to implement is functionality without using is keyword in C#
- C# | Swap two Strings without using third user defined variable
- C# | Using foreach loop in arrays
- Invoking an overloaded constructor using this keyword in C#
- C# | Multiple inheritance using interfaces
- How to access structure elements using Pointers in C#
- Late Binding using Reflection in C#
- Writing to Excel Sheet Using EPPlus in C#
- Automation Using Selenium in C# With Example
- Adding Text with a Float number Using String.Format() Method in C#
- Getting the Total Number of Days in a Month Using If-else and Switch Statements in C#
- How to Get a Total Number of Days in a Month using built in Functions in C#?
- Program to swap numbers using XOR operator in C#
- C# - Indexers Using String as an Index
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.