public class DAL
{
static string serverName = "SERVIDOR_BANCO"; //localhost
static string port = "5432"; //porta default
static string userName = "USuURIO_BANCO"; //nome do administrador
static string password = "SENHA_BANCO"; //senha do administrador
static string databaseName = "sambalife"; //nome do banco de dados
NpgsqlConnection pgsqlConnection = null; // instalar via nugget NpgsqlConnection -- Install-Package Npgsql -Version 3.2.6
string connString = null;
public DAL()
{
connString = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};",
serverName, port, userName, password, databaseName);
}
public DataTable GetTodosRegistros()
{
DataTable dt = new DataTable();
try
{
using (pgsqlConnection = new NpgsqlConnection(connString))
{
// abre a conexão com o PgSQL
pgsqlConnection.Open();
//define a instrução SQL
string cmdSeleciona = "Select * from product order by id";
using (NpgsqlDataAdapter Adpt = new NpgsqlDataAdapter(cmdSeleciona, pgsqlConnection))
{
Adpt.Fill(dt);
}
}
}
catch (NpgsqlException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
finally
{
pgsqlConnection.Close();
}
return dt;
}