using System;
using Npgsql;
namespace clases
{
public class PostgreSQLConexion
{
private NpgsqlConnection postgresSQL = null;
public string Servidor
{
get; set;
}
public string Datos
{
get; set;
}
public string Usuario
{
get; set;
}
public string Password
{
get; set;
}
public PostgreSQLConexion()
{
Console.WriteLine("Objeto PostgreSQLConexion creado e inicializado");
}
public PostgreSQLConexion(string servidor, string datos, string usuario, string password)
{
this.Servidor = servidor;
this.Datos = datos;
this.Usuario = usuario;
this.Password = password;
Console.WriteLine(this.ToString());
postgresSQL = new NpgsqlConnection("Server="+this.Servidor+";User Id="+this.Usuario+";Password="+this.Password+";Database="+this.Datos+";");
}
~PostgreSQLConexion(){}
public void Abrir()
{
postgresSQL.Open();
Console.WriteLine("Abriendo conexion a la base de datos: {0}",this.Datos);
}
public void Cerrar()
{
postgresSQL.Close();
Console.WriteLine("Cerrando conexion...");
}
public override string ToString(){
return String.Format("PostgreSQLConexion(servidor = {0}, base = {1}, usuario = {2}, password = {3})",this.Servidor, this.Datos,this.Usuario,this.Password);
}
public void EjecutarConsulta()
{
NpgsqlCommand cmd = new NpgsqlCommand("SELECT comentario, fecha FROM public.visitante", this.postgresSQL);
NpgsqlDataReader dr = cmd.ExecuteReader();
Console.WriteLine("Datos:");
while (dr.Read())
{
Console.Write("{0} : {1}\n", dr[0],dr[1]);
}
}
}
}
Conectar una bd Postgres con C#
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.