62 lines
1.7 KiB
C#
62 lines
1.7 KiB
C#
using Microsoft.Data.SqlClient;
|
|
|
|
namespace DAL
|
|
{
|
|
public static class DadosDaConexao
|
|
{
|
|
// ===== CONFIG =====
|
|
public static string Host { get; set; } = string.Empty;
|
|
public static int Port { get; set; } = 1433;
|
|
|
|
public static string Banco { get; set; } = string.Empty;
|
|
|
|
public static string Usuario { get; set; } = string.Empty;
|
|
public static string Senha { get; set; } = string.Empty;
|
|
|
|
public static int ConnectTimeout { get; set; } = 3;
|
|
public static bool Encrypt { get; set; } = true;
|
|
public static bool TrustServerCertificate { get; set; } = true;
|
|
|
|
public static readonly string ObjetoStringConexao = ObterConexao();
|
|
|
|
// ===== CONEXÃO =====
|
|
public static string ObterConexao()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(Host))
|
|
return string.Empty;
|
|
|
|
var cs = new SqlConnectionStringBuilder
|
|
{
|
|
DataSource = $"{Host},{Port}",
|
|
InitialCatalog = Banco,
|
|
UserID = Usuario,
|
|
Password = Senha,
|
|
ConnectTimeout = ConnectTimeout,
|
|
Encrypt = Encrypt,
|
|
TrustServerCertificate = TrustServerCertificate
|
|
};
|
|
|
|
return cs.ConnectionString;
|
|
}
|
|
|
|
// ===== TESTE =====
|
|
public static bool TestarConexao()
|
|
{
|
|
var cs = ObterConexao();
|
|
|
|
if (string.IsNullOrWhiteSpace(cs))
|
|
return false;
|
|
|
|
try
|
|
{
|
|
using var conn = new SqlConnection(cs);
|
|
conn.Open();
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
} |