.NET Standard数据库连接:ADO.Net是否适用?有无相关新标准需了解?
Great question! Let’s break this down step by step so it makes total sense.
Absolutely—ADO.NET is still a perfectly valid and appropriate choice for database access in .NET Standard. Here’s why:
- .NET Standard’s core API set explicitly includes the foundational ADO.NET abstractions (like
System.Data.Common,DbConnection,DbCommand, andDbDataReader). These are the building blocks that all cross-platform database drivers rely on, so using them ensures your code is portable across any .NET platform that supports .NET Standard (think .NET Framework, .NET Core/.NET 5+, Xamarin, etc.). - If you need fine-grained control over database connections, queries, or performance, ADO.NET is unbeatable. It lets you work directly with database resources without the overhead of an ORM, which is ideal for writing low-level data access layers or optimizing critical database operations.
There’s no "new standard" that replaces ADO.NET entirely—instead, modern tooling builds on top of ADO.NET’s core abstractions while adding quality-of-life improvements:
Microsoft.Data.SqlClient: This is the modern, official SQL Server driver (replacing the oldSystem.Data.SqlClient). It’s fully compatible with .NET Standard, supports modern features like Azure AD authentication and UTF-8 database compatibility, but still adheres to ADO.NET’s core interfaces.- ORMs like Entity Framework Core (EF Core): EF Core is a cross-platform ORM that uses ADO.NET under the hood. It provides higher-level features like LINQ queries and entity mapping, but you can still drop down to raw ADO.NET when you need to.
- Lightweight libraries like Dapper: Dapper is a micro-ORM that extends ADO.NET with simple object mapping, keeping the performance of raw ADO.NET while reducing boilerplate code.
You’re spot-on with this thought! ADO.NET’s core abstractions have already been fully integrated into .NET Standard’s specification. .NET Standard didn’t replace ADO.NET—it formalized its core APIs as the baseline for cross-platform database access. This means any database driver targeting .NET Standard must implement ADO.NET’s interfaces, ensuring consistency across platforms and libraries.
To illustrate, here’s a simple .NET Standard-compatible ADO.NET snippet that works across all .NET platforms:
using System.Data.Common; using System.Threading.Tasks; public async Task<int> RunNonQueryAsync(DbConnection connection, string query) { await connection.OpenAsync(); using var command = connection.CreateCommand(); command.CommandText = query; return await command.ExecuteNonQueryAsync(); }
You can pass a SqlConnection (for SQL Server), NpgsqlConnection (for PostgreSQL), or any other .NET Standard-compliant connection to this method, and it’ll work without modification.
内容的提问来源于stack exchange,提问作者McCarson

