Creando Clase sql para obtener o enviar en c#

Anteriormente establecimos 4 clases para efectuar una acción diferente como ser las clases de

Insertar Datos

Actualizar Datos

Borrar Datos

Seleccionar Datos

Las cuales se resumen en dos simples acciones como ser el obtener datos o enviar datos, clases que podríamos crear para efectuar las 4 opciones en dos simple clases como ser:

GetFromSQL
xPOSTToSQL


Ahora en la Clase sql c#  Consideremos que queremos efectuar una sentencia sql tendríamos que pensar que airamos para seleccionar que clase usar(Insertar, Actualizar, Borrar o seleccionar) pero en esta ocasión crearemos una clase que trabaje en viceversa, de tal manera que tengamos que especificar el código sql que deseamos ejecutar..




aqui el codigo:


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Configuration;
using System.Web;

namespace _13GetAndPostSql
{

    class Program
    {
        private static string conexion
        {
            //Especificando datos de conexion    Nombre del servidor/PC                  nombre de la tabla
            get
            {
                return "data source=WINDOWS-1HTKO0B\\MSSQLSERVER4;initial catalog=alola;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework";
            }

         }
        public static DataTable xGetFromSQL(string xSql)
        {
            try
            {
                DataTable xDT = new DataTable();
                using (SqlDataAdapter xDA = new SqlDataAdapter(xSql, conexion))
                {
                    xDA.SelectCommand.CommandTimeout = 600000;
                    xDA.Fill(xDT);
                }

                return xDT;
            }
            catch { throw; }
        }

        public static void xPOSTToSQL(string xSQL)
        {
            using (SqlCommand xCommand = new SqlCommand(xSQL, new SqlConnection(conexion)))
            {
                xCommand.Connection.Open();
                xCommand.ExecuteNonQuery();
                xCommand.Connection.Close();
            }
        }

        static void Main(string[] args)
        {

            Console.WriteLine("Ejecutando xGetFromSQL c# ");
            Console.WriteLine(String.Format("{0}", xGetFromSQL("  select prueba from prueba where id =5").Rows[0][0].ToString()));

            Console.WriteLine("Ejecutando xPOSTToSQL c# ");
            Console.WriteLine(String.Format("{0}", xGetFromSQL("  update prueba set prueba ='Fin' where id =5")));

            Console.WriteLine("Ejecutando xGetFromSQL c# ");
            Console.WriteLine(String.Format("{0}", xGetFromSQL("  select prueba from prueba order by id").Rows[0][0].ToString()));


            Console.ReadLine();
        }
    }
}









Comentarios

Populares

Buscar en este blog