Jump to content
Fórum Script Brasil
  • 0

Conectar PostGree via C#

Dan Karlinha


 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();

                using (pgsqlConnection = new NpgsqlConnection(connString))
                    // abre a conexão com o PgSQL

                  //define a instrução SQL
                    string cmdSeleciona = "Select * from product order by id";

                    using (NpgsqlDataAdapter Adpt = new NpgsqlDataAdapter(cmdSeleciona, pgsqlConnection))
            catch (NpgsqlException ex)
                throw ex;
            catch (Exception ex)
                throw ex;

            return dt;

Edited by Dan Karlinha
Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Create New...