C# SqlConnection 异常:不支持关键字“端口" [英] C# SqlConnection Exception: Keyword not Supported 'Port'

查看:110
本文介绍了C# SqlConnection 异常:不支持关键字“端口"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多相同主题的帖子.但两者都没有帮助我.我想将 Postgresql 与 CrossPlatform-App 一起使用.稍后我将使用云中的服务器,但为了尝试,我认为使用本地数据库更容易.因此,我已经安装了数据库本身和带有NuGet"的npgsql"包,就像 Postgres 人推荐的那样.在连接时总是出现相同的错误后,我将代码简化为一个控制台应用程序,只有十行代码.

I have looked at a lot of posts with the same topic. But neither helped me. I want to use Postgresql with a CrossPlatform-App. Later I will use a server in the cloud but for trying I think it's easier to use a local database. Therefore I have installed database itself and the "npgsql" Package with "NuGet", like recommended from the Postgres guys. After getting always the same error while connecting, I reduced the code to a console-App with really ten lines of code.

namespace SqlTester_ConsolApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string MyConnection = "Server=127.0.0.1;Port=5432;Database=sample;UserId=Postgresql;Password=zzzzzzzzz;";

            try
            {
                SqlConnection Connection = new SqlConnection(MyConnection);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("---------------------------------------------------------------------");
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
            System.Diagnostics.Debug.WriteLine("Ready");
        }
    }
}

new SqlConnection 总是以 System.ArgumentException 结束,告诉 Keyword not supported 'port'.原因很明显,.Net Framework 尝试使用 SQL Server 而不是将我的 String 传递给 npgsql 驱动程序.因此,从字符串中剪切端口"是没有意义的.

The new SqlConnection always terminates with and System.ArgumentException telling Keyword not supported 'port'. The cause is clear, the .Net Framework tries to use SQL Server instead of passing my String to the npgsql driver. Therefore it does not make sense to cut the "port" from the string.

我的问题是如何将字符串解析更改为 Progresql 提供程序?我积极地没有为错误的选择做任何事情,这显然是 .Net Framework 的默认行为的一部分,它要么是错误或意图仅支持 MS 产品.

My question is how can I change the string parsing to the Progresql Provider? Actively I did nothing for that wrong selection, it's obviously part of the default behavior from the .Net Framework and it's either an error or intention to support only MS-Products.

我尝试了一些方法来改变行为.澄清更新:因为NpgsqlConnection"的使用对我来说是不可接受的.我的代码应该独立于提供者.

I tried somethings to change the behavior. Update for clarification: Because the usage of "NpgsqlConnection" is not acceptable for me. My code should be provider-independent.

正如我在 Postgresql-Docu 到 npgsql 和另一个 问题 有同样的问题,我在控制台应用程序的 App.Config 文件中添加了以下几行.

As I found in the Postgresql-Docu to npgsql and to another question with the same problem I added the following lines to the App.Configfile of the Console-App.

<system.data>
  <DbProviderFactories>
    <add name="Npgsql Data Provider" invariant="Npgsql" description=".Net Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7"/>
  </DbProviderFactories>
  <defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7"></defaultConnectionFactory>
</system.data>

它没有帮助.另一件事是,如果它对 App.Config 有帮助,我如何将其转移到我的跨平台应用程序.没有 App.Config.

It did not help. The other thing is, if it would help with the App.Config how can I transfer that to my Cross-Platform-App. There is no App.Config.

我的问题是环境而不是编程本身.在从事其他工作多年后,我再次开始编程.所以所有的环境对我来说都是全新的.在那之前,Visual Studio 会自动处理任何事情.所以我不必学习很多细节.我试图找到一些关于 DbProviderFactories 的 .Net-internals 和应用程序配置的信息.但是我没有找到有用的信息,主要是因为不知道如何搜索.所以任何帮助对我都有用.提前致谢.

My Problem is the environment not the programming itself. I start programming again after years in other jobs. So all the environment is absolutely new to me. Until that point Visual Studio handled anything automatically. So I have not to learn many details. I tried to find some information about the .Net-internals for DbProviderFactories and configuring of apps. But I did not find useful information, mostly because of not knowing how to search. So any help is useful to me. Thanks in advance.

推荐答案

SqlConnection 特定于 SQL Server.您需要使用 NpgsqlConnection 来使用 Postgres 连接,这意味着您将需要对 Postgres 程序集的引用.

SqlConnection is specific to SQL Server. You would need to use NpgsqlConnection to use a Postgres connection, which means you will need the references to the Postgres assemblies.

但是您打算将来迁移到不同的提供程序,因此为了尽量减少迁移的影响,请尝试在您的代码中使用通用基类.例如:

But you are intending to move to a different provider in future, so to minimise the impact of that move, try to use the generic base classes in your code. For example:

var connString = "Host=myserver;Username=mylogin;Password=mypass;Database=mydatabase";
using (DbConnection conn = new NpgsqlConnection(connString))
{
    conn.Open()
    using (DbCommand command = conn.CreateCommand())
    {
        // etc
    }
}

这样,当您切换时,您需要做的就是将整个解决方案中的 NpgsqlConnection 替换为 SqlConnection.如果您想使用 DbProviderFactory(那里有一个很好的例子)然后你就可以了,但基本上你只是保存了这个单一的搜索/替换,删除了引用,并发布了一个新版本的代码.

This way, when you swap over, all you need to do is replace NpgsqlConnection across your solution with SqlConnection. If you want to use DbProviderFactory (there's a good example in there) then you can, but basically you are only saving yourself this single search/replace, removing the references, and releasing a new version of the code.

我建议 将连接字符串放在配置文件中而不是代码(如问题所示),以避免将其放在多个位置,并使其更容易更改而无需重建.

I'd suggest putting the connection string in the config file rather than code (as shown in the question) to avoid having it in multiple places, and make it easier to change without a rebuild.

当然,您可能需要修复 SQL 本身中任何特定于实现的细节.

And of course you may have to fix any implementation-specific details in the SQL itself.

这篇关于C# SqlConnection 异常:不支持关键字“端口"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆