如何使用 ADO.NET 请求数据库产品名称?

发布于 2024-07-13 01:17:57 字数 120 浏览 4 评论 0原文

我开发了一个具有自定义数据库配置的产品。 我将 ADO.NET 与 System.Data.Odbc.OdbcConnection 结合使用。 为了在数据库之间做出一些区别,有一个简单的解决方案来检测连接的当前 DBMS。

I develop a product with a custom database configuration. I use ADO.NET with System.Data.Odbc.OdbcConnection for it. To make some differences between the databases is there a simple solution to detect the current DBMS of a connection.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帥小哥 2024-07-20 01:17:57

我认为 OdbcConnection.Driver 属性在OP的上下文中可能更合适,因为 ServerVersion 应该只返回版本号。

Driver 属性将返回 DLL 的名称,例如对于 SQL Server,“Sqlsrv32.dll”。 然后OP可以应用基于案例的逻辑。

I think that the OdbcConnection.Driver property may be more appropriate in the OP's context, since ServerVersion should return only the version number.

The Driver property will return the name of the DLL, such as "Sqlsrv32.dll" in case of SQL server. Then the OP can apply case based logic.

不醒的梦 2024-07-20 01:17:57

connection.Database 可以工作,或者您可以执行

select db_name()

以扩展 connection.Database

connection.Open();
        Console.WriteLine("ServerVersion: " + connection.ServerVersion
            + "\nDatabase: " + connection.Database);
        connection.ChangeDatabase("master");
        Console.WriteLine("ServerVersion: " + connection.ServerVersion
            + "\nDatabase: " + connection.Database);
        Console.ReadLine();

connection.Database would work or you can execute

select db_name()

to expand on connection.Database

connection.Open();
        Console.WriteLine("ServerVersion: " + connection.ServerVersion
            + "\nDatabase: " + connection.Database);
        connection.ChangeDatabase("master");
        Console.WriteLine("ServerVersion: " + connection.ServerVersion
            + "\nDatabase: " + connection.Database);
        Console.ReadLine();
无力看清 2024-07-20 01:17:57

我找到了以下解决方案:

DataTable td = conn.GetSchema(DbMetaDataCollectionNames.DataSourceInformation);
DataRow info = td.Rows[0];
String name = info[DbMetaDataColumnNames.DataSourceProductName];

例如,这返回 Microsoft SQL Server 并且与驱动程序无关。

I found the follow solution self:

DataTable td = conn.GetSchema(DbMetaDataCollectionNames.DataSourceInformation);
DataRow info = td.Rows[0];
String name = info[DbMetaDataColumnNames.DataSourceProductName];

For example this returns Microsoft SQL Server and is driver independent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文