c# - 代码中的数据库驱动程序规范

标签 c# python sql sql-server oracle

大家早上好,

最近我遇到了 Python 和 C# 中的数据库连接问题。

例如,假设我们要连接 SQL Server。在 .NET (C#) 中,我们只需要知道服务器名称、凭据,就可以使用 SqlConnection 对象连接到数据库。这很清楚。

现在,在Python中,当使用pypyodbc时,我们需要另外指定驱动程序。那么问题来了:

为什么在Python中我们必须指定驱动程序?我们不必在 C# 中指定它。

另一方面,如果指定驱动程序如此重要,那么为什么我们不必在 C# 中指定它呢?

我知道,在 C# 中,我们有专门用于 SQL Server 的类(Python 吗?或 pypyodbc 是唯一的选择吗?),这是否意味着它有某种方法来解析要使用哪个驱动程序使用?和Oracle一样吗?

最佳答案

我对 python 一无所知,但我认为这应该回答你的主要问题:

SqlConnection 是专门为与 SQL Server 配合使用而设计的,因此您不需要指定驱动程序,因为它使用自己指定的驱动程序。
来自 SqlConnection.ConnectionString Property Microsoft 文档页面:

The ConnectionString is similar to an OLE DB connection string, but is not identical. Unlike OLE DB or ADO, the connection string that is returned is the same as the user-set ConnectionString, minus security information if the Persist Security Info value is set to false (default). The .NET Framework Data Provider for SQL Server does not persist or return the password in a connection string unless you set Persist Security Info to true.

(强调我的,引文中的其他内容只是为了提供上下文。)

然后在页面下方:

The .NET Framework Data Provider for SQL Server uses its own protocol to communicate with SQL Server. Therefore, it does not support the use of an ODBC data source name (DSN) when connecting to SQL Server because it does not add an ODBC layer.

(再次强调我的。)

使用其他连接类时,例如 OdbcConnectionOleDbConnection ,您需要在连接字符串中指定驱动程序。

Odbc Connection Strings使用关键字Driver
并与 OleDb Connection Strings使用关键字Provider

关于 Oracle,.Net 框架有一个名为 System.Data.OracleClient 的命名空间,但已弃用(来自 Oracle and ADO.NET):

The types in System.Data.OracleClient are deprecated. The types remain supported in the current version of.NET Framework but will be removed in a future release. Microsoft recommends that you use a third-party Oracle provider.

我猜测 Oracle 自己的 ADO.Net 实现也可以使用它自己的驱动程序。如果您确实想知道,可以在他们的文档中查找。

关于c# - 代码中的数据库驱动程序规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49788675/

相关文章:

c# - appSettings.json 中的自定义键在 ASP.NET Core 2.0 中不起作用

c# - 如何将现有数据库与 IdentityServer4 一起使用

python - 在 Python 中抓取 javascript 呈现的网站的 "Script part"

python - Pandas 按日期范围分组和对多列进行不同的计算

SQL 查询 - 从两个不同级别的层次结构中获取总和

c# - .Net Core IdentityServer4 获取认证用户

python - 从预签名 url curl S3 对象时访问被拒绝错误

SQL 连接和排除

mysql - (SQL 帮助)有人可以告诉我我在哪里出错了吗?

c# - 在 C++ 和 C# 或 VB 之间共享数据(2 个正在运行的程序)