R
COMO HACER UNA CONEXION A BD C#
webconfig
<!--CADENA DE CONEXIÓN-->
<connectionStrings>
<add name="ConPrueba"
connectionString="data source= SALA5; database = AGENDA_PRIS; integrated security = true"
providerName ="System.Data.SqlClient"/>
</connectionStrings>
<!--FIN CADENA DE CONEXION-->
<connectionStrings>
<add name="bdConexion" connectionString="data source = IP; user id = sa; password= $sql2017; initial catalog = DB" providerName="System.Data.SqlClient"/>
</connectionStrings>
En bdConext
// Solo cadena de conexion, si se necesita ver detalles, editar o agregar una cadena de conexion nueva, ir al archivo
// Web.config de FACPYA.PracticasProfesionales.Presentacion
// Nombre del ConnectionStrings a libre eleccion, si se desea cambiar afectar tambien al archivo web.config de Presentacion
static private readonly string appConexion = ConfigurationManager.ConnectionStrings["bdConexion"].ConnectionString;
// Se ejecuta stored con paramaetros
public static DataTable funcionStored(string sentencia, SqlParameter[] parametroSQL)
{
DataTable dt = new DataTable();
try
{
using (var conn = new SqlConnection(appConexion))
{
conn.Open();
using (var comando = new SqlCommand(sentencia, conn))
{
comando.CommandType = CommandType.StoredProcedure;
comando.Parameters.AddRange(parametroSQL);
using (var adaptador = new SqlDataAdapter(comando))
{
adaptador.Fill(dt);
}
}
}
return dt;
}
catch (Exception e)
{
throw e;
}
}
// Se ejecuta stored sin parametros
public static DataTable funcionStored(string sentencia)
{
DataTable dt = new DataTable();
try
{
using (var conn = new SqlConnection(appConexion))
{
conn.Open();
using (var comando = new SqlCommand(sentencia, conn))
{
comando.CommandType = CommandType.StoredProcedure;
using (var adaptador = new SqlDataAdapter(comando))
{
adaptador.Fill(dt);
}
}
}
return dt;
}
catch (Exception e)
{
throw e;
}
}
}
Se agradecen los comentarios ;)
Comentarios
Publicar un comentario