C# tutorial CRUD tutorials with example
C# Tutorial: CRUD Operations with Example
In this tutorial, we'll cover the basics of CRUD operations (Create, Read, Update, Delete) in C#. We'll go through the key concepts and implement them using C# and a SQL Server database to perform the database operations.
1. Prerequisites
- C# and .NET Framework: Basic knowledge of C# and .NET framework.
- SQL Server: We’ll use SQL Server for the database.
- Visual Studio: A popular IDE to write and test C# code.
- SQL Server Management Studio (SSMS): To interact with SQL Server.
2. Setting Up the Environment
Before we dive into the code, make sure that:
- You have Visual Studio installed.
- You have SQL Server running, either locally or remotely, and can connect to it.
3. SQL Database Setup
We will start by creating a simple database called SampleDB
and a table called Users
for storing user information.
SQL Script to Create the Database and Table:
The Users
table will store the following fields:
Id
: A unique identifier for each user (Primary Key).FirstName
: The user's first name.LastName
: The user's last name.Email
: The user's email address.
4. C# Code Setup
We will now create a C# application to perform CRUD operations. Below are the steps.
Step 1: Create a Console Application in Visual Studio
- Open Visual Studio and create a new Console App project.
- Name the project something like
CRUDApp
.
Step 2: Install ADO.NET for Database Connection
To interact with the SQL Server, we need to use ADO.NET.
In your Visual Studio project:
- Right-click on References > Manage NuGet Packages.
- Search for
System.Data.SqlClient
and install it.
5. C# CRUD Code
We will create methods for Create, Read, Update, and Delete operations.
Step 3: Create a Class for User Entity
Let's start by creating a User
class to represent the user entity:
This class will represent the user data that we'll be working with.
Step 4: Create the CRUD Operations
Next, we'll create a DatabaseHelper
class to interact with the SQL Server database.
Explanation of CRUD Methods:
- CreateUser(User user): This method inserts a new record into the
Users
table. - GetUsers(): This method retrieves all the users from the
Users
table and returns them as a list ofUser
objects. - UpdateUser(User user): This method updates an existing user’s details based on the
Id
. - DeleteUser(int userId): This method deletes a user record from the
Users
table based on theId
.
Step 5: Implement CRUD Operations in the Main Program
Now, let’s use the DatabaseHelper
class in our Main
method to test the CRUD operations.
Explanation of the Main Program:
- Create a User: We create a new
User
object and pass it to theCreateUser
method to insert it into the database. - Read Users: We retrieve all users from the database using the
GetUsers
method and display them. - Update User: We update the first user’s details and call the
UpdateUser
method. - Delete User: We delete the first user by calling the
DeleteUser
method.
6. Testing the Application
- After running the application, the following operations will be executed in sequence:
- A new user will be created and added to the database.
- All users will be displayed in the console.
- The first user’s details will be updated.
- The first user will be deleted from the database.
You should see the output similar to the following in your console:
7. Conclusion
In this tutorial, we learned how to perform CRUD operations (Create, Read, Update, Delete) using C# and SQL Server. The key concepts we covered include:
- Setting up a database and creating a table.
- Using ADO.NET to interact with the database in C#.
- Creating methods for each CRUD operation.
- Testing the operations with sample data.
This pattern is the foundation for building data-driven applications using C# and SQL Server.
Comments
Post a Comment