Vista previa 
            
             | 
            |
| Requisito | |
| SQL Server 2008 0 Superior | |
| 
                     | 
            |
| 
                     Objectivo General  | 
            |
| Crear una consulta que divide una cadena delimitada por comas en diferente valores | |
| 
                     | 
            |
                    
USE [Test]]
GO
IF OBJECT_ID('TempTable', N'U') IS NOT NULL
  DROP TABLE TempTable
GO
CREATE TABLE TempTable
(
 [id]   INT IDENTITY(1,1) NOT NULL,
 [index]   INT,
 [valor]   NVARCHAR(50) NULL,
 [modificado] NVARCHAR(50) NULL,
 [original]  NVARCHAR(50) NULL
)
GO
DECLARE @ORIGINAL NVARCHAR(50);
DECLARE @MODIFICADO NVARCHAR(50);
DECLARE @job  NVARCHAR(50);
DECLARE @INDEX  INT = 0;
DECLARE @VALOR  NVARCHAR(50) = 0;
SELECT @ORIGINAL = COLUMNB FROM dbo.Table_3 WHERE ID = 7; --19,2,3,14,15,16
INSERT INTO TempTable([valor], [modificado], [original]) VALUES (@VALOR, RTRIM(LTRIM(@ORIGINAL)), RTRIM(LTRIM(@ORIGINAL)))
WHILE ((SELECT LEN([modificado]) from TempTable where id = (select MAX(id) from TempTable)) > 0)
 BEGIN
  BEGIN TRY
   SET @job = (SELECT [modificado] from TempTable where id = (select MAX(id) from TempTable))
   SET @VALOR = SUBSTRING(RTRIM(LTRIM(@job)), 1 , CHARINDEX(N',', RTRIM(LTRIM(@job))) - 1);
   SET @INDEX = CHARINDEX(N',', @job);
   SET @MODIFICADO = SUBSTRING(RTRIM(LTRIM(@job)), @INDEX + 1, LEN(RTRIM(LTRIM(@job))));
 
    IF (((SELECT [index] from TempTable where id = (select MAX(id) from TempTable)) = 0))
     BEGIN
      INSERT INTO TempTable([index], [valor], [modificado], [original]) VALUES (@INDEX, RTRIM(LTRIM(@job)), RTRIM(LTRIM(@MODIFICADO)), RTRIM(LTRIM(@ORIGINAL)))
      BREAK
     END
    ELSE
     INSERT INTO TempTable([index], [valor], [modificado], [original]) VALUES (@INDEX, RTRIM(LTRIM(@VALOR)), RTRIM(LTRIM(@MODIFICADO)), RTRIM(LTRIM(@ORIGINAL)))
     CONTINUE
  END TRY
 
  BEGIN CATCH
   INSERT INTO TempTable([index], [valor], [modificado], [original]) VALUES (@INDEX, RTRIM(LTRIM(@job)), 0, RTRIM(LTRIM(@ORIGINAL)))
   BREAK
  END CATCH;
 END
GO
                 | 
            |
| 
                     | 
            |
| 
                     Descripcion  | 
            |
                    
  | 
            |
martes, 27 de diciembre de 2011
Separar cadena delimitada por comas en SQL Server
lunes, 22 de agosto de 2011
Metodo Generico
| Objectivo | 
| Crear un metodo que no este sujeto a un query especifico y que retorne un control poblado de datos. | 
| Requisito: | 
  | 
        
| Nota: | 
  | 
        
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Globalization;
using System.ComponentModel;
public partial class _Default : System.Web.UI.Page
{
    #region Evento Load de la pagina
    protected void Page_Load(object sender, EventArgs e)
    {
        AdventureWorks_DataModel.AdventureWorks_DataEntities db = new AdventureWorks_DataModel.AdventureWorks_DataEntities();
        form1.Controls.Add(ControlBuilder<AdventureWorks_DataModel.Product, DropDownList>(db.Products.Select(dd => dd).ToList(), "ProductID", "Name", "dd1", "hola"));
    }
    #endregion Evento Load de la pagina
    #region Generic Method
    private static U ControlBuilder<T, U>(List<T> row, string datavaluefieldname, string datatextfieldname, string id, string Etiqueta)
        where T : class
        where U : class
    {
        Type ControlToReturnType = typeof(U);
        DropDownList ControlToReturn = (DropDownList)ControlToReturnType.InvokeMember
            (
            null,
            BindingFlags.Public |
            BindingFlags.Instance |
            BindingFlags.CreateInstance,
            null,
            null,
            null
            );
        
        ControlToReturn.ID = id;
        ControlToReturn.AutoPostBack = true;
        ControlToReturn.CausesValidation = false;
        ControlToReturn.Width = System.Web.UI.WebControls.Unit.Percentage(100);
        ControlToReturn.SelectedIndexChanged += new EventHandler(ControlToReturn_SelectedIndexChanged);
        foreach (T item in row)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(item);
            System.ComponentModel.PropertyDescriptor MyProperty1 = properties.Find(datatextfieldname, false);
            System.ComponentModel.PropertyDescriptor MyProperty2 = properties.Find(datavaluefieldname, false);
            ListItem li = new ListItem();
            li.Text = MyProperty1.GetValue(item).ToString();
            li.Value = MyProperty2.GetValue(item).ToString();
            ControlToReturn.Items.Add(li);
        }
        ListItem EtiquetaApertura = new ListItem();
        EtiquetaApertura.Enabled = true;
        EtiquetaApertura.Selected = true;
        EtiquetaApertura.Value = null;
        EtiquetaApertura.Text = Etiqueta;
        ControlToReturn.Items.Insert(0, EtiquetaApertura);
        return ControlToReturn as U;
    }
    #endregion GenericMethod
    #region Manejador de evento para el control
    static void ControlToReturn_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList dl = (DropDownList)sender;
        Page page = (Page)dl.NamingContainer;
        Label lb = (Label)page.FindControl("lb1");
        lb.Text = dl.SelectedValue;
    }
    #endregion Manejador de evento para el control
}
            
 | 
        
| Descripcion: | 
 private static U ControlBuilder<T, U>(List<T> row, string datavaluefieldname, string datatextfieldname, string id, string Encabezado)
        where T : class
        where U : class
    {
    }
                    
Type ControlToReturnType = typeof(U); DropDownList ControlToReturn = (DropDownList)ControlToReturnType.InvokeMember
            (
            null,
            BindingFlags.Public |
            BindingFlags.Instance |
            BindingFlags.CreateInstance,
            null,
            null,
            null
            );
ControlToReturn.ID = id; ControlToReturn.AutoPostBack = true; ControlToReturn.CausesValidation = false; ControlToReturn.Width = System.Web.UI.WebControls.Unit.Percentage(100); ControlToReturn.SelectedIndexChanged += new EventHandler(ControlToReturn_SelectedIndexChanged); foreach (T item in row)
        {
            // Creo una nueva colleccion y le asigno a esta las prepiedades de item.
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(item);
            // Seteo el PropertyDescriptor a la propiedad especificada.
            System.ComponentModel.PropertyDescriptor MyProperty1 = properties.Find(datatextfieldname, false);
            System.ComponentModel.PropertyDescriptor MyProperty2 = properties.Find(datavaluefieldname, false);
            ListItem li = new ListItem();
            li.Text = MyProperty1.GetValue(item).ToString();
            li.Value = MyProperty2.GetValue(item).ToString();
            ControlToReturn.Items.Add(li);
        }
        ListItem EtiquetaApertura = new ListItem();
        EtiquetaApertura.Enabled = true;
        EtiquetaApertura.Selected = true;
        EtiquetaApertura.Value = null;
        EtiquetaApertura.Text = Encabezado;
        ControlToReturn.Items.Insert(0, EtiquetaApertura);
        return ControlToReturn as U;
    }
System.ComponentModel.PropertyDescriptor MyProperty1 = properties.Find(datatextfieldname, falseSystem.ComponentModel.PropertyDescriptor MyProperty2 = properties.Find(datavaluefieldname, false); ListItem li = new ListItem();m(); li.Text = MyProperty1.GetValue(item).ToString(); li.Value = MyProperty2.GetValue(item).ToString(); ControlToReturn.Items.Add(li); ListItem EtiquetaApertura = new ListItem(); EtiquetaApertura.Enabled = true; EtiquetaApertura.Selected = true; EtiquetaApertura.Value = null; EtiquetaApertura.Text = Encabezado; ControlToReturn.Items.Insert(0, EtiquetaApertura); return ControlToReturn as U; static void ControlToReturn_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList dl = (DropDownList)sender;
        Page page = (Page)dl.NamingContainer;
        Label lb = (Label)page.FindControl("lb1");
        lb.Text = dl.SelectedValue;
    }
 | 
        
Suscribirse a:
Comentarios (Atom)