C# .NET + PostgreSQL

C#.NetPostgresql

C# Problem Overview


I'm looking at working on a project which uses C#.NET (sitting on a windows box) as the primary language and PostgreSQL as the backend database (backend is sitting on a linux box). I've heard that ODBC.NET allows for easy integration of these two components.

Has anyone had experience actually setting C# and PostgreSQL up to work together? If so, do you have any suggestions about how to go about it, issues you've found, etc.?

C# Solutions


Solution 1 - C#

I'm working with C# and Postgres using Npgsql2 component, and they work fast, I recommend you.

You can download from https://github.com/npgsql/Npgsql/releases

Note: If you want an application that works with any database you can use the DbProviderFactory class and make your queries using IDbConnection, IDbCommand, IDataReader and/or IDbTransaction interfaces.

Solution 2 - C#

Npgsql - .Net Provider for PostGreSQL - is an excellent driver. If you have used the more traditional ADO.NET framework you are really in luck here. I have code that connects to Oracle that looks almost identical to the PostGreSQL connections. Easier to transition off of Oracle and reuse brain cells.

It supports all of the standard things you would want to do with calling SQL, but it also supports calling Functions (stored procedures). This includes the returning of reference cursors. The documentation is well written and provides useful examples without getting philosophical or arcane. Steal the code right out of the documentation and it will work instantly.

Francisco Figueiredo, Jr's and team have done a great job with this.
It is now available on Github.
https://github.com/franciscojunior/Npgsql2

The better site for info is: http://npgsql.projects.postgresql.org/

Read the documentation! http://npgsql.projects.postgresql.org/docs/manual/UserManual.html

Solution 3 - C#

There is a Linq provider for PostgreSQL at https://www.nuget.org/packages/linq2db.PostgreSQL/.

Solution 4 - C#

We have developed several applications using visual studio 2005 with the devart ado.net data provider for PostgreSql (http://www.devart.com/pgsqlnet/).

One of the advantages of this provider is that it provides full Visual Studio support. The latest versions include all the new framework features like linq.

Solution 5 - C#

Today most languages/platforms (Java, .NET, PHP, Perl etc.) can work with almost any DBMS (SQL Server, Firebird, MySQL, Oracle, PostgresSQL etc.) so I wouldn't worry for one second. Sure there might be glitches and small problems but no showstopper.

As jalcom suggested you should program against a set of interfaces or at least a set of base classes (DbConnection, DbCommand and so on) to have an easily adaptable application.

Solution 6 - C#

You shouldn't have too many problems. As others have mentioned, there's many .Net PostgreSQL data providers available. One thing you may want to look out for is that features like Linq will probably not be able to be used.

Solution 7 - C#

Dont let a lack of Linq support stop you. A pattern I use is to always return my data into lists, and then linq away. I started doing this religiously when I found that the same (admittedly obscure) Linq expression in MySQL didnt bring back the same data as it did in Sql Server.

Solution 8 - C#

Just Go to Tool-->NuGet Package Manager-->Manager Nuget Package Manager

search for NpgSql and then select your project and click Install

sample Code

public void Demo()
        {
            NpgsqlConnection connection = new NpgsqlConnection();
            connection = d_connection; // your connection string
            connection.Open();              
            NpgsqlCommand cmd = new NpgsqlCommand();
            try
            {
                cmd.Connection = connection;
                cmd.CommandText = "select * from your table name";
                cmd.CommandType = System.Data.CommandType.Text;
                using (var dataReader = cmd.ExecuteReader())
                {
                    while (dataReader.Read())
                    {
                        
                     string answer= dataReader.IsDBNull(0) ? "" : dataReader.GetString(0);
                        
                    }
                    dataReader.Dispose();
                }
            }
            catch (Exception e)
            {
            }
            finally
            {
                cmd.Dispose();
                connection.Dispose();
            }            
        }

Dont use upper case in postgreSql because its case sensitive.

Solution 9 - C#

Npgsql is excellent driver, but only issue Ive found so far is that Numeric value does not fit in a System.Decimal, so only option is correct each query or DB schema

https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/438#issuecomment-486586272

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionakdomView Question on Stackoverflow
Solution 1 - C#jalcomView Answer on Stackoverflow
Solution 2 - C#StradasView Answer on Stackoverflow
Solution 3 - C#David SchmittView Answer on Stackoverflow
Solution 4 - C#SimonView Answer on Stackoverflow
Solution 5 - C#Andrei RîneaView Answer on Stackoverflow
Solution 6 - C#KibbeeView Answer on Stackoverflow
Solution 7 - C#user542319View Answer on Stackoverflow
Solution 8 - C#JRAView Answer on Stackoverflow
Solution 9 - C#MaciejView Answer on Stackoverflow