SqlQuantumLeap

SQLCLR SP Parses CSV file to Result Set - Source Code

Apr 15th, 2016 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.04 KB | None | 0 0
  1. /*
  2.     *** DO NOT USE IN PRODUCTION -- TESTING / DEMONSTRATION CODE ONLY ***
  3.  
  4.     This script relates to the following SQL Server Central Forum topic:
  5.     Processing strings ( https://www.sqlservercentral.com/forums/topic/processing-strings )
  6.  
  7.     *** DO NOT USE IN PRODUCTION -- TESTING / DEMONSTRATION CODE ONLY ***
  8.  
  9.     This script provides the .NET / C# source code for the [ParseCSV] SQLCLR Stored Procedure.
  10.  
  11.     *** DO NOT USE IN PRODUCTION -- TESTING / DEMONSTRATION CODE ONLY ***
  12.  
  13.     A T-SQL installation script (no external DLL) containing only two Stored Procedures is located at:
  14.     https://pastebin.com/aqsWiX1e
  15.  
  16.     *** DO NOT USE IN PRODUCTION -- TESTING / DEMONSTRATION CODE ONLY ***
  17.  
  18.     A T-SQL script containing tests for the [ParseCSV] SQLCLR Stored Procedure is located at:
  19.     https://pastebin.com/WGDkQTxt
  20.  
  21.     *** DO NOT USE IN PRODUCTION -- TESTING / DEMONSTRATION CODE ONLY ***
  22.  
  23.     Date: 2016-04-11
  24.     Version: 1.0.0
  25.  
  26.     For more functions like this, please visit: https://SQLsharp.com
  27.  
  28.     Stairway to SQLCLR series: https://www.sqlservercentral.com/stairways/stairway-to-sqlclr
  29.  
  30.     Copyright (c) 2016 Sql Quantum Leap. All rights reserved.
  31.     https://SqlQuantumLeap.com
  32. */
  33.  
  34. using System;
  35. using System.Data.SqlTypes;
  36. using System.IO;
  37. using Microsoft.SqlServer.Server;
  38. using Microsoft.VisualBasic.FileIO;
  39.  
  40.  
  41. public class CSVUtils
  42. {
  43.     [SqlProcedure()]
  44.     public static SqlInt32 ParseCSV(
  45.         [SqlFacet(MaxSize = 1)] SqlString Delimiter,
  46.         [SqlFacet(MaxSize = -1)] SqlString InputString,
  47.         [SqlFacet(MaxSize = 500)] SqlString FilePath)
  48.     {
  49.         if (Delimiter.IsNull)
  50.         {
  51.             return 1;
  52.         }
  53.  
  54.         if (!FilePath.IsNull && !FilePath.Value.Trim().Equals(string.Empty))
  55.         {
  56.             using (TextFieldParser _CSV = new TextFieldParser(FilePath.Value))
  57.             {
  58.                 ParseStuff(_CSV, Delimiter.Value);
  59.             }
  60.         }
  61.         else
  62.         {
  63.             if (InputString.IsNull || InputString.Value.Trim().Equals(string.Empty))
  64.             {
  65.                 return 0;
  66.             }
  67.  
  68.             using (StringReader _Reader = new StringReader(InputString.Value))
  69.             {
  70.                 using (TextFieldParser _CSV = new TextFieldParser(_Reader))
  71.                 {
  72.                     ParseStuff(_CSV, Delimiter.Value);
  73.                 }
  74.             }
  75.         }
  76.         return 0;
  77.     }
  78.  
  79.     private static void ParseStuff(TextFieldParser CSV, string Delimiter)
  80.     {
  81.         bool _IsResultSchemaSet = false;
  82.         SqlDataRecord _ResultSet = null;
  83.         int _FieldCount = 0;
  84.  
  85.         CSV.SetDelimiters(Delimiter); // this also sets TextFieldType = FieldType.Delimited;
  86.         CSV.HasFieldsEnclosedInQuotes = true;
  87.         string[] _Fields;
  88.  
  89.         while (!CSV.EndOfData)
  90.         {
  91.             _Fields = CSV.ReadFields();
  92.  
  93.             if (!_IsResultSchemaSet)
  94.             {
  95.                 _FieldCount = _Fields.Length;
  96.                 SqlMetaData[] _TempStructure = new SqlMetaData[_FieldCount];
  97.  
  98.                 for (int _Index = 0; _Index < _FieldCount; _Index++)
  99.                 {
  100.                     _TempStructure[_Index] = new SqlMetaData(string.Format("Field{0:D3}", _Index + 1),
  101.                         System.Data.SqlDbType.NVarChar, SqlMetaData.Max);
  102.                 }
  103.                 _ResultSet = new SqlDataRecord(_TempStructure);
  104.  
  105.                 SqlContext.Pipe.SendResultsStart(_ResultSet);
  106.  
  107.                 _IsResultSchemaSet = true;
  108.             }
  109.  
  110.             _ResultSet.SetValues(_Fields);
  111.             if (_Fields.Length < _FieldCount)
  112.             {
  113.                 for (int _Index = _Fields.Length; _Index < _FieldCount; _Index++)
  114.                 {
  115.                     _ResultSet.SetDBNull(_Index);
  116.                 }
  117.             }
  118.             SqlContext.Pipe.SendResultsRow(_ResultSet);
  119.         }
  120.  
  121.         SqlContext.Pipe.SendResultsEnd();
  122.  
  123.         return;
  124.     }
  125.  
  126.     [SqlProcedure]
  127.     public static void GarbageCollect()
  128.     {
  129.         GC.Collect();
  130.  
  131.         return;
  132.     }
  133.  
  134. }
Add Comment
Please, Sign In to add comment