Database access techniques used in C#

WBOY
Release: 2024-02-19 10:01:06
Original
485 people have browsed it

Database access techniques used in C#

What are the database access technologies in C#, specific code examples are required

In C# development, database access is a very common and important part. This article will introduce commonly used database access technologies in C# and provide some specific code examples to help readers understand and apply these technologies.

  1. ADO.NET: ADO.NET is one of the most commonly used database access technologies in C#. It uses a set of classes related to a specific database, such as Connection, Command, DataReader, etc., to realize the addition, deletion, modification and query of data by operating on the relevant objects of the database. The following is a sample code that uses ADO.NET to access the database:
using System;
using System.Data.SqlClient;

namespace DatabaseAccess
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = "YourConnectionString";
            string query = "SELECT * FROM Customers";
            
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(query, connection);
                connection.Open();
                
                SqlDataReader reader = command.ExecuteReader();
                
                while (reader.Read())
                {
                    Console.WriteLine(reader["FirstName"] + " " + reader["LastName"]);
                }
                
                reader.Close();
            }
        }
    }
}
Copy after login
  1. Entity Framework (EF): Entity Framework is an object-relational mapping (ORM) framework that allows developers to Define domain models to operate the database without writing raw SQL queries. EF automatically converts the object into the corresponding SQL statement and executes it. The following is a sample code that uses Entity Framework to access the database:
using System;
using System.Linq;

namespace DatabaseAccess
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new YourDbContext())
            {
                var customers = context.Customers.Where(c => c.Age > 18);
                
                foreach (var customer in customers)
                {
                    Console.WriteLine(customer.FirstName + " " + customer.LastName);
                }
            }
        }
    }
}
Copy after login
  1. Dapper: Dapper is a lightweight ORM framework that provides a simple and efficient way to access the database. Compared with EF, it focuses more on performance and flexibility. The following is a sample code for using Dapper to access the database:
using System;
using System.Data;
using System.Data.SqlClient;
using Dapper;

namespace DatabaseAccess
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = "YourConnectionString";
            string query = "SELECT * FROM Customers WHERE Age > @Age";
            
            using (IDbConnection connection = new SqlConnection(connectionString))
            {
                var customers = connection.Query<Customer>(query, new { Age = 18 });
                
                foreach (var customer in customers)
                {
                    Console.WriteLine(customer.FirstName + " " + customer.LastName);
                }
            }
        }
        
        class Customer
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
    }
}
Copy after login

The above are three database access technologies commonly used in C#. They each have their own characteristics. Developers can choose the appropriate technology according to actual needs. By mastering these technologies, developers can interact with the database more conveniently and realize various business needs. We hope that the code examples provided in this article will be helpful to readers in their learning and development work in database access.

The above is the detailed content of Database access techniques used in C#. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!