using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.Collections;
using System.IO;
using System.Data;
using System.Web.UI.WebControls;
using System.Configuration;
namespace SistemaExpediente.Clases
{
public class Conexion
{
public SqlConnection CON;
private SqlCommand Comando = new SqlCommand();
public Conexion()
{
//string cnx = "Data Source=NOMBRESERVIDOR;Initial Catalog=NOMBREDEMIBASEDEDATOS;Persist Security Info=True;User ID=sa;Password=MICONTRASENA";
ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["DB_CHSJConnectionString"];
string cnx =settings.ConnectionString;
CON = new SqlConnection(cnx); }
~Conexion()
{
try { CON.Close(); }
catch (Exception e) { Console.WriteLine(e.Message); }
CON.Dispose();
}
public void cerrar()
{ CON.Close(); }
public int Accion(string Cad)
{
int x;
try
{
CON.Open();
Comando.Connection = CON;
Comando.CommandText = Cad;
x = Comando.ExecuteNonQuery();
cerrar();
return x;
}
catch
{
cerrar();
CON.Open();
Comando.Connection = CON;
Comando.CommandText = Cad;
x = Comando.ExecuteNonQuery();
cerrar();
return x;
}
}
public DataTable GetSelect(string commandText)
{
DataTable TABLA = new DataTable("Tmp");
try
{
int i;
CON.Close();//movi
CON.Open();
SqlCommand CMD = new SqlCommand(commandText, CON);
SqlDataReader REG = CMD.ExecuteReader();
for (i = 0; i < REG.FieldCount; i++)
{
TABLA.Columns.Add(REG.GetName(i), REG.GetFieldType(i));
}
while (REG.Read())
{
DataRow ROWS = TABLA.NewRow();
for (i = 0; i < REG.FieldCount; i++)
{
ROWS.SetField(i, REG[i]);
}
TABLA.Rows.Add(ROWS);
}
REG.Close();
CON.Close();
return TABLA;
}
catch (SqlException Ex)
{
}
return TABLA;
}
public SqlDataReader Reg(string Cad)
{
try
{
cerrar();
}
finally
{
try
{
CON.Open();
Comando.CommandText = Cad;
Comando.Connection = CON;
}
catch (Exception Er)
{
}
}
try { return Comando.ExecuteReader(); }
catch { return null; }
}
}
}
No hay comentarios.:
Publicar un comentario