Jump to content
Fórum Script Brasil
  • 0

Conectar PostGree via C#


Dan Karlinha
 Share

Question

 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;
        }
 

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.

Guest
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.

 Share

  • Forum Statistics

    • Total Topics
      149.6k
    • Total Posts
      646.2k
×
×
  • Create New...