Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Configuration;
- using System.Data.SqlClient;
- namespace R904ProtegerCadenaConexionBD
- {
- class R904Programa
- {
- static void Main(string[] args)
- {
- string nombreCadenaConexion = "CadenaConexionPrincipal";
- string cadenaConexion = @"Data source =.\SQLEXPRESS; Initial catalog = Northwind;Integrated Security=SSPI;" +
- "Min Pool Size= 5; Max Pool Size = 15; Connection Reset = True; Connection Lifetime = 600";
- string proveedor = "System.Data.SqlClient";
- ProtegerCadenaConexion(nombreCadenaConexion, cadenaConexion, proveedor);
- ConnectionStringSettings configurationCadenaConexion = ConfigurationManager.ConnectionStrings[nombreCadenaConexion];
- using (SqlConnection conexion = new SqlConnection(configurationCadenaConexion.ConnectionString))
- {
- }
- Console.WriteLine("presione Enter para continuar...");
- Console.ReadLine();
- }
- private static void ProtegerCadenaConexion(string nombre, string cadenaConexion, string proveedor)
- {
- Configuration configuracion = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- ConnectionStringsSection seccionConfiguracion = configuracion.ConnectionStrings;
- if (seccionConfiguracion == null)
- {
- seccionConfiguracion = new ConnectionStringsSection();
- configuracion.Sections.Add("connectionSettings", seccionConfiguracion);
- }
- if (!seccionConfiguracion.SectionInformation.IsProtected)
- {
- seccionConfiguracion.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
- }
- ConnectionStringSettings configurarionCadena = new ConnectionStringSettings(nombre, cadenaConexion, proveedor);
- seccionConfiguracion.ConnectionStrings.Add(configurarionCadena);
- seccionConfiguracion.SectionInformation.ForceSave = true;
- configuracion.Save(ConfigurationSaveMode.Full);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement