Connect application to Postgresql?

0

Rstou wanting to connect a simple C # application to a Postgresql database, what I would like to know is if the procedure is similar to Java, in which I have to create a connection class, a class with the get's and set's of entities to manipulate the data?

I saw in Visual Studio that it has a Connect to Database option and I figured Visual Studio was already automated in this regard.

    
asked by anonymous 24.06.2015 / 13:22

2 answers

1

The connection is made in a similar way. In Java, you connect to the database through JDBC. .Jar files

In C # the connection is made through ADO.NET. Just like in Java, you will need a provider to access the database. In this case, the Npgsql.dll DLL

See a simple example of connection below.


using System;
using System.Data;
using Npgsql; //Referencia do provider de conexão do Postgresql.  

public class NpgsqlUserManual { public static void Main(String[] args) { NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=joe;Password=secret;Database=joedata;"); conn.Open(); conn.Close(); } }

    
24.06.2015 / 15:11
0

Here you are:
     string serverdb="IP";      string dbdb="BaseData";

 string userdb = "Username";
 string passdb = "Password";

 string connectionString = "Server=" + serverdb + ";Port=5432;UserID=" + userdb + ";password=" + passdb + ";Database=" + dbdb + ";";


public Boolean conecta()
{
    //Estabelece Ligações a Bases de Dados
    try
    {
        this.coneccao = new NpgsqlConnection(connectionString);
        coneccao.Open();
    }
    catch (Exception )
    {
        MessageBox.Show(ex.Message, "Erro de ligação");
      }
}
    
24.06.2015 / 15:04