178 lines
6.7 KiB
C#
178 lines
6.7 KiB
C#
using Exferia_Aplicacion.General;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.EntityClient;
|
|
using System.Data.SqlClient;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Exferia_KairosPRO_EF
|
|
{
|
|
public class Conexion
|
|
{
|
|
public static string ConnectionString
|
|
{
|
|
get
|
|
{
|
|
if (Variables.G_STR_CONEXION_USUARIO_KAIROSPRO != "")//Con Usuario y Contraseña
|
|
{
|
|
var con = new EntityConnectionStringBuilder()
|
|
{
|
|
Metadata = @"res://*/EF_BaseDatos_KairosPRO.csdl|res://*/EF_BaseDatos_KairosPRO.ssdl|res://*/EF_BaseDatos_KairosPRO.msl",
|
|
Provider = @"System.Data.SqlClient",
|
|
ProviderConnectionString = new SqlConnectionStringBuilder()
|
|
{
|
|
DataSource = Variables.G_STR_CONEXION_SERVIDOR_KAIROSPRO,
|
|
InitialCatalog = Variables.G_STR_CONEXION_BASEDATOS_KAIROSPRO,
|
|
UserID = Variables.G_STR_CONEXION_USUARIO_KAIROSPRO,
|
|
Password = Variables.G_STR_CONEXION_CLAVE_KAIROSPRO,
|
|
ConnectTimeout = 100,
|
|
}.ConnectionString,
|
|
};
|
|
|
|
return con.ConnectionString;
|
|
}
|
|
else//Sin usuario y Contraseña
|
|
{
|
|
var con = new EntityConnectionStringBuilder()
|
|
{
|
|
Metadata = @"res://*/EF_BaseDatos_KairosPRO.csdl|res://*/EF_BaseDatos_KairosPRO.ssdl|res://*/EF_BaseDatos_KairosPRO.msl",
|
|
Provider = @"System.Data.SqlClient",
|
|
ProviderConnectionString = new SqlConnectionStringBuilder()
|
|
{
|
|
DataSource = Variables.G_STR_CONEXION_SERVIDOR_KAIROSPRO,
|
|
InitialCatalog = Variables.G_STR_CONEXION_BASEDATOS_KAIROSPRO,
|
|
IntegratedSecurity = true,
|
|
ConnectTimeout = 100,
|
|
}.ConnectionString,
|
|
};
|
|
|
|
return con.ConnectionString;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static int Existe_BaseDatos(string _str_Servidor, string _str_BaseDatos, string _str_Usuario, string _str_Clave)
|
|
{
|
|
int int_ExisteBD = -1;//No se Conecto
|
|
SqlConnection _conexion = null;
|
|
try
|
|
{
|
|
Stopwatch sw = new Stopwatch();
|
|
|
|
SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();
|
|
csb.DataSource = _str_Servidor;
|
|
csb.InitialCatalog = "master";
|
|
|
|
if (_str_Usuario.Trim().Equals("") || _str_Clave.Trim().Equals(""))
|
|
{
|
|
csb.IntegratedSecurity = true;
|
|
}
|
|
else
|
|
{
|
|
csb.Password = _str_Clave;
|
|
csb.UserID = _str_Usuario;
|
|
}
|
|
csb.ConnectTimeout = 10;
|
|
|
|
_conexion = new SqlConnection(csb.ConnectionString);
|
|
|
|
//A los 10 Segundos sino se ha conectado que salga ################
|
|
Thread t = new Thread(delegate ()
|
|
{
|
|
try
|
|
{
|
|
sw.Start();
|
|
_conexion.Open();
|
|
int_ExisteBD = 0;//Correcta
|
|
}
|
|
catch { }
|
|
});
|
|
t.IsBackground = true;
|
|
t.Start();
|
|
//################################################################
|
|
|
|
while (10000 > sw.ElapsedMilliseconds)
|
|
if (t.Join(1))
|
|
break;
|
|
|
|
if (int_ExisteBD == 0)
|
|
{
|
|
//Buscar si Existe la Base De Datos
|
|
SqlCommand cmd_ExisteBD = new SqlCommand("SELECT count(name) as Total FROM sysdatabases where name='" + _str_BaseDatos + "'", _conexion);
|
|
int _valorDevuelto = (int)cmd_ExisteBD.ExecuteScalar();
|
|
|
|
//Si no es 1 , es que la base de datos no existe
|
|
if (_valorDevuelto == 1)
|
|
{ int_ExisteBD = 1; }//Existe
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Control_Errores.Errores_Log(ex.Message, ex, nameof(Conexion) + "/" + nameof(Existe_BaseDatos));
|
|
int_ExisteBD = -1;//Error
|
|
}
|
|
finally
|
|
{
|
|
//Si la Conexion no es nula y esta abierta se cierra
|
|
if (_conexion != null)
|
|
{
|
|
if (_conexion.State == ConnectionState.Open)
|
|
{
|
|
_conexion.Close();
|
|
}
|
|
_conexion.Dispose();
|
|
}
|
|
}
|
|
return int_ExisteBD;
|
|
}
|
|
|
|
public static Boolean ComprobarConexion(string _str_Servidor, string _str_BaseDatos, string _str_Usuario, string _str_Clave)
|
|
{
|
|
SqlConnection _conexion = null;
|
|
Boolean ValorDevuelto = false;
|
|
try
|
|
{
|
|
if (_str_Servidor.Trim().Length > 0)
|
|
{
|
|
string UsuarioClave = "Integrated Security=SSPI;";
|
|
if (_str_Usuario != "")
|
|
{
|
|
UsuarioClave = "User Id=" + _str_Usuario + ";Password=" + _str_Clave + ";";
|
|
}
|
|
|
|
_conexion = new SqlConnection();
|
|
_conexion.ConnectionString = "Data Source=" + _str_Servidor + ";Initial Catalog=" + _str_BaseDatos + ";" + UsuarioClave + "Connect Timeout=10;";
|
|
_conexion.Open();
|
|
|
|
if (_conexion != null && _conexion.State == ConnectionState.Open)
|
|
{
|
|
ValorDevuelto = true;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Control_Errores.Errores_Log(ex.Message, ex, nameof(Conexion) + "/" + nameof(ComprobarConexion));
|
|
}
|
|
finally
|
|
{
|
|
//Si la Conexion no es nula y esta abierta se cierra
|
|
if (_conexion != null)
|
|
{
|
|
if (_conexion.State == ConnectionState.Open)
|
|
{
|
|
_conexion.Close();
|
|
}
|
|
_conexion.Dispose();
|
|
}
|
|
}
|
|
return ValorDevuelto;
|
|
}
|
|
}
|
|
}
|