• subscribe
January 20, 2010 12:00 AM

Handling the Tree Path Request

Discover an efficient solution
SQL Server Pro
InstantDoc ID #103317
Downloads
103317.zip

Listing 2: Definition of Split Function in C#

using System;

using System.Data.SqlTypes;

using Microsoft.SqlServer.Server;

using System.Collections;

using System.Collections.Generic;

 

public partial class SplitClass

{

  // Struct used in string split functions

  struct row_item

  {

    public string item;

    public int pos;

  }

 

  // Split array of strings and return a table

  // FillRowMethodName = "ArrSplitFillRow"

  [SqlFunction(FillRowMethodName = "ArrSplitFillRow",

   DataAccess = DataAccessKind.None,

   TableDefinition = "pos INT, element NVARCHAR(MAX)")]

  public static IEnumerable Split(SqlString inpStr,

      SqlString charSeparator)

  {

    string locStr;

    string\[] splitStr;

    char\[] locSeparator = new char\[1];

    locSeparator\[0] = (char)charSeparator.Value\[0];

    if (inpStr.IsNull)

      locStr = "";

    else

      locStr = inpStr.Value;

    splitStr = locStr.Split(locSeparator,

          StringSplitOptions.RemoveEmptyEntries);

    //locStr.Split(charSeparator.ToString()[0]);

    List<row_item> SplitString = new List<row_item>();

    int i = 1;

    foreach (string s in splitStr)

    {

      row_item r = new row_item();

      r.item = s;

      r.pos = i;

      SplitString.Add(r);

      ++i;

    }

    return SplitString;

  }

 

  public static void ArrSplitFillRow(

    Object obj, out int pos, out string item)

  {

    pos = ((row_item)obj).pos;

    item = ((row_item)obj).item;

  }

}

 

 



ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here