【问题标题】:Differences between DbConnectionStringBuilder and OdbcConnectionStringBuilder Connection StringsDbConnectionStringBuilder 和 OdbcConnectionStringBuilder 连接字符串的区别
【发布时间】:2019-01-12 21:42:03
【问题描述】:

我正在尝试使我的应用程序数据库不可知,因此我选择使用 DBConnection 类而不是当前乱扔我的应用程序的 SqlConnection、OracleConnection 等。所以我遇到了 DBConnectionStringBulder 在我的连接字符串中添加双引号的问题。当我使用 OdbcConnectionStringBuilder 时,省略了额外的双引号。所以现在我需要在构建连接字符串后替换双引号以避免任何问题。

OdbcConnectionStringBulder 连接字符串:

Driver={Microsoft 文本驱动程序 (*.txt; *.csv)};Dbq=C:\Temp\;Extensions=asc,csv,tab,txt;Persist Security Info=False

DbConnectionStringBulder 连接字符串:

Driver="{Microsoft 文本驱动程序 (*.txt; *.csv)}";Dbq=C:\Temp\;Extensions=asc,csv,tab,txt;Persist Security Info=False

所以我猜测这是 DbConnectionStringBuilder 中的一个错误,但我想看看我是否做错了什么。这是我的解决方法的适用代码:

        OdbcConnectionStringBuilder odbcConnectionString = new OdbcConnectionStringBuilder();
        odbcConnectionString.Add("Driver", "{Microsoft Text Driver (*.txt; *.csv)}");
        odbcConnectionString.Add("Dbq", @"C:\Temp\");
        odbcConnectionString.Add("Extensions", "asc,csv,tab,txt");
        odbcConnectionString.Add("Persist Security Info", "False");
        Console.WriteLine(odbcConnectionString.ConnectionString);

        DbConnectionStringBuilder dbConnectionString = new DbConnectionStringBuilder();
        dbConnectionString.Add("Driver", "{Microsoft Text Driver (*.txt; *.csv)}");
        dbConnectionString.Add("Dbq", @"C:\Temp\");
        dbConnectionString.Add("Extensions", "asc,csv,tab,txt");
        dbConnectionString.Add("Persist Security Info", "False");
        Console.WriteLine(dbConnectionString.ConnectionString.Replace("\"", string.Empty)); //<-- My Workaround.

这是 .Net Fiddle 的链接:https://dotnetfiddle.net/3qhB5S

【问题讨论】:

    标签: c# visual-studio-2017 database-connection dbconnection


    【解决方案1】:

    几年前我也尝试过,我最终围绕 UI 的 DBConnection 类进行了抽象,最终用户必须选择并连接到数据库服务器。 然后我实现了 sqlserver 和 mysql 的规则,以便最终结果会根据实现的数据库服务可以处理的规则进行调整。

    对于查询执行,我基于 ado.net 中的 DbConnection 和 DbCommand 类创建了一个包装器。

    【讨论】:

    • 谢谢我试图避免做类似的事情,但这似乎是我需要做的。
    • 每个数据库服务器都有自己特定的一组选项,因此很难将它们统一到一个配置路径中。我自己尝试过,但失败了。我并没有离开 ADO.net 界面。不管我们用mysql、sql server、postgres、sqlite,做一个统一的wrapper,才是我们真正需要的。
    猜你喜欢
    • 2017-08-08
    • 2012-10-10
    • 2014-06-12
    • 1970-01-01
    • 2015-07-11
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多