C# data access technology SqlClient/Odbc/OleDb/EF Core

System.Data.SqlClient, System.Data.Odbcand System.Data.OleDbare namespaces for data access in the .NET Framework .

These namespaces provide different data providers for connecting, manipulating and managing different types of databases.

  • System.Data.SqlClientThe namespace provides the classes and interfaces required to connect and operate with a SQL Server database. It is a .NET Framework data provider for SQL Server databases.

  • System.Data.OdbcThe namespace provides the classes and interfaces required to connect and operate with ODBC (Open Database Connectivity) data sources. It is a .NET Framework data provider for databases using ODBC drivers.

  • System.Data.OleDbThe namespace provides the classes and interfaces required to connect and operate with OLE DB (Object Linking and Embedding, Database) data sources. It is a .NET Framework data provider for databases that use the OLE DB provider.

In .NET 6, Microsoft recommends using Entity Framework Core (EF Core) as the primary data access technology to replace traditional System.Datanamespaced classes. EF Core provides a more modern, flexible and database-agnostic approach to data access. (System.Data Namespaces are still available in .NET 5, .NET 6, and .NET 7, but using EF Core provides more powerful and easy-to-use data access capabilities, especially in modern applications. )

  • System.Data.SqlClient

using System;
using System.Data.SqlClient;

namespace MyApp
{
    class Program
    {
        static void Main()
        {
            string connectionString = "Data Source=(local);Initial Catalog=MyDatabase;Integrated Security=True";

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                string query = "SELECT * FROM Customers";

                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string customerName = reader["Name"].ToString();
                            Console.WriteLine(customerName);
                        }
                    }
                }
            }
        }
    }
}
  •  System.Data.Odbc

 System.Data.OdbcThe namespace provides classes and interfaces for connecting, executing queries, and manipulating ODBC data sources. It contains some important classes, such as OdbcConnection, OdbcCommand, OdbcDataReaderetc., which are used to establish connections, execute SQL statements and read query results.

using System;
using System.Data.Odbc;

namespace MyApp
{
    class Program
    {
        static void Main()
        {
            string connectionString = "Driver={SQL Server};Server=(local);Database=MyDatabase;Trusted_Connection=yes;";

            using (OdbcConnection connection = new OdbcConnection(connectionString))
            {
                connection.Open();

                string query = "SELECT * FROM Customers";

                using (OdbcCommand command = new OdbcCommand(query, connection))
                {
                    using (OdbcDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string customerName = reader["Name"].ToString();
                            Console.WriteLine(customerName);
                        }
                    }
                }
            }
        }
    }
}
  •  System.Data.OleDb

System.Data.OleDbThe namespace is the namespace used in the .NET Framework to connect and operate with OLE DB (Object Linking and Embedding, Database) data sources.

OLE DB is an object-oriented database access interface , which provides a standardized way to connect and operate different types of databases, such as SQL Server, Oracle, Access, etc. Using OLE DB, you can access various database systems through a unified interface.

System.Data.OleDbThe namespace provides classes and interfaces for connecting, executing queries, and manipulating OLE DB data sources. It contains some important classes, such as OleDbConnection, OleDbCommand, OleDbDataReaderetc., which are used to establish connections, execute SQL statements and read query results.

using System;
using System.Data.OleDb;

namespace MyApp
{
    class Program
    {
        static void Main()
        {
            string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\MyDatabase.mdb;";

            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                connection.Open();

                string query = "SELECT * FROM Customers";

                using (OleDbCommand command = new OleDbCommand(query, connection))
                {
                    using (OleDbDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string customerName = reader["Name"].ToString();
                            Console.WriteLine(customerName);
                        }
                    }
                }
            }
        }
    }
}
  • Entity Framework Core(EF Core)

In .NET 6, System.Datanamespaces still exist and provide core classes and interfaces for data access and database manipulation. The namespace contains some important classes, such as DbConnection, DbCommand, DbDataReaderetc., which are used to establish database connections, execute SQL commands and read query results.

However, it's worth noting that in .NET 6, Microsoft recommends using Entity Framework Core (EF Core) as the primary data access technology instead of traditional System.Datanamespaced classes. EF Core provides a more modern, flexible and database-agnostic approach to data access.

EF Core provides ORM (Object-Relational Mapping) function, which enables developers to perform database operations by defining entity classes and relationships without having to write SQL commands directly. It supports various database providers such as SQL Server, MySQL, PostgreSQL, etc.

If you are using .NET 6 and need to perform database operations, it is recommended to use EF Core to simplify the process of data access. You can use the NuGet package manager or .NET CLI to add EF Core related packages to your project, and then configure and use them according to the EF Core documentation.

using Microsoft.EntityFrameworkCore;
using System;

namespace MyApp
{
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class MyDbContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("YourConnectionString");
        }
    }

    class Program
    {
        static void Main()
        {
            using (var context = new MyDbContext())
            {
                var customers = context.Customers.ToList();

                foreach (var customer in customers)
                {
                    Console.WriteLine(customer.Name);
                }
            }
        }
    }
}
  • System.DataNamespaces

In .NET Framework, System.Datanamespace is one of the core namespaces for data access. It provides a set of classes and interfaces for connecting to data sources, executing commands, reading and manipulating data, and more.

System.DataThe namespace contains some important classes and interfaces, such as:

  • DataSet: Represents an in-memory dataset that can contain multiple data tables, relationships, and constraints.
  • DataTable: Represents a data table in memory, containing multiple data rows and columns.
  • DataColumn: Represents a column in the data table.
  • DataRow: Indicates a row in a data table.
  • DataAdapter: DataSetClass used to populate data in and update data sources.
  • DbConnection: Indicates the connection to the database.
  • DbCommand: Indicates the command to be executed in the database.
  • DbDataReader: The class used to read data from the database.

These classes and interfaces provide a common way to access data, no matter what type of database you are using (such as SQL Server, Oracle, MySQL, etc.), you can use these classes and interfaces to connect to the database, execute commands and manipulate data.

using System;
using System.Data;

namespace MyApp
{
    class Program
    {
        static void Main()
        {
            // 创建连接对象
            using (var connection = new System.Data.SqlClient.SqlConnection("YourConnectionString"))
            {
                // 打开连接
                connection.Open();

                // 创建命令对象
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = "SELECT * FROM Customers";

                    // 创建数据适配器
                    using (var adapter = new System.Data.SqlClient.SqlDataAdapter(command))
                    {
                        // 创建数据集
                        var dataSet = new DataSet();

                        // 填充数据集
                        adapter.Fill(dataSet);

                        // 遍历数据集中的数据表和行
                        foreach (DataTable table in dataSet.Tables)
                        {
                            foreach (DataRow row in table.Rows)
                            {
                                Console.WriteLine(row["Name"]);
                            }
                        }
                    }
                }
            }
        }
    }
}

 

Guess you like

Origin blog.csdn.net/book_dw5189/article/details/131484827