using System;
using MySql.Data.MySqlClient;
namespace clases
{
public class ConexionMySQL
{
private MySqlConnection mySqlConnection = null;
public ConexionMySQL()
{
Console.WriteLine("Objeto ConexionMySQL creado e inicializado");
}
public ConexionMySQL(string servidor, string datos, string usuario, string password)
{
this.Servidor = servidor;
this.Datos = datos;
this.Usuario = usuario;
this.Password = password;
}
~ConexionMySQL()
{
Console.WriteLine("Objeto ConexionMySQL destruido");
}
public void Inicio()
{
Console.WriteLine("Iniciando...");
}
public MySqlConnection Connection
{
get { return mySqlConnection; }
}
public bool IsConnect()
{
if (Connection == null)
{
if (String.IsNullOrEmpty(this.Datos))
return false;
string cadena = "Server="+this.Servidor+"; database={0};";
cadena += "UID="+this.Usuario+";";
cadena += "password="+ this.Password;
//string connstring = string.Format("Server=localhost; database={0}; UID=UserName; password=your password", this.Datos);
string cadenaStr = string.Format(cadena, this.Datos);
mySqlConnection = new MySqlConnection(cadenaStr);
mySqlConnection.Open();
}
return true;
}
private static ConexionMySQL _instance = null;
public static ConexionMySQL Instance()
{
if (_instance == null)
_instance = new ConexionMySQL();
return _instance;
}
public void Close()
{
mySqlConnection.Close();
Console.WriteLine("Se ha cerrado la conexion a la base de datos: {0}",this.Datos);
}
public string Servidor
{
get; set;
}
public string Datos
{
get; set;
}
public string Usuario
{
get; set;
}
public string Password
{
get; set;
}
public override string ToString(){
return string.Format("ConexionMySQL(servidor = {0} , base = {1} , usuario = {2}, password = {3})", this.Servidor,this.Datos, this.Usuario,this.Password);
}
}
}
Conexión MySQL con C# (usando dotnet y Nugets)
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.