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");
}
}
}
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.
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.
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.
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>
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.
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, then it's going to try and connect to SQL Server. UseNpgsqlConnectioninstead.DbConnectionclass rather than the SQL Server-specificSqlConnection