Is there a way to query a PostgreSQL db in C# in a database agnostic way ie without Npgsql. I want use built in .NET libraries and a connection-string that contains the DB IP, Username, Password and any other info that the connection-string might require.
-
1one option would be ODBC connectionCee McSharpface– Cee McSharpface2017-09-01 17:21:20 +00:00Commented Sep 1, 2017 at 17:21
-
Could you please explain why Npgsql is not a solution?Aleks Andreev– Aleks Andreev2017-09-01 17:57:49 +00:00Commented Sep 1, 2017 at 17:57
-
I want use built in .NET libraries. Also ODBC would require the setup of a DSN and installation of a driver on the server which isn't an option. There are no built in classes or providers in the .NET framework to achieve what Im trying to do ?ranasrule– ranasrule2017-09-01 18:08:06 +00:00Commented Sep 1, 2017 at 18:08
-
With ODBC, you can pass all the connection arguments in your .net code, no need to set up the DSN. As for the driver, it is not installed on the DB server but on the client side. You use generic .net objects, but and the driver translates them to postgres.JGH– JGH2017-09-02 12:00:30 +00:00Commented Sep 2, 2017 at 12:00
-
do you have a code sample for this ?ranasrule– ranasrule2017-09-02 12:28:38 +00:00Commented Sep 2, 2017 at 12:28
Add a comment
|
1 Answer
You can use ODBC and construct the connection within your app. The Postgres driver has to be installed on the machine running the program. Your app would then only be using the generic ODBC classes. Below is a simple example.
using System.Data.Odbc;
string connectString = "Driver={PostgreSQL UNICODE};Port=5432;Server=localhost;Database=myDBname;Uid=myusername;Pwd=mypassword;";
OdbcConnection connection = new OdbcConnection(connectString);
connection.Open();
string sql = "select version()";
OdbcCommand cmd = new OdbcCommand(sql, connection);
OdbcDataReader dr = cmd.ExecuteReader();
dr.Read();
string dbVersion = dr.GetString(0);