PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/RAW File Viewer/DataFlow.cs

http://rawviewer.codeplex.com
C# | 264 lines | 181 code | 32 blank | 51 comment | 20 complexity | 5daeca151f032a8e2a1138643385210a MD5 | raw file
  1. //*************************************************************************************************************************
  2. // Description: SSIS 2008 Package Programming to move data from the RAWFile to DataGridView
  3. // using Data Reader as Destination
  4. // Created by: Mitulkumar Brahmbhatt
  5. // Modified by: Simon Trigona
  6. //*************************************************************************************************************************
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using Microsoft.SqlServer.Dts;
  12. using Microsoft.SqlServer.Dts.Runtime;
  13. using Microsoft.SqlServer.Dts.Runtime.Wrapper;
  14. using Microsoft.SqlServer.Dts.Pipeline;
  15. using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
  16. using Microsoft.SqlServer.Dts.DtsClient;
  17. namespace RAW_File_Viewer
  18. {
  19. internal class clsDataFlow
  20. {
  21. #region Private Members
  22. // Package, Pipe, Connection Members
  23. private Microsoft.SqlServer.Dts.Runtime.Wrapper.PackageClass _objPackage = new Microsoft.SqlServer.Dts.Runtime.Wrapper.PackageClass();
  24. private MainPipe _objMainPipe = null;
  25. private IDTSPath100 _objIDTSPath = null;
  26. private Microsoft.SqlServer.Dts.Runtime.Wrapper.Application app = null;
  27. // Metadata Members
  28. private Microsoft.SqlServer.Dts.Pipeline.Wrapper.IDTSComponentMetaData100 _objIDTSSRCMetaData;
  29. private Microsoft.SqlServer.Dts.Pipeline.Wrapper.IDTSComponentMetaData100 _objIDTSDSTReaderMetaData;
  30. // Wrapper Members
  31. private CManagedComponentWrapper _objSourceWrapper;
  32. private CManagedComponentWrapper _objDestinationReaderWrapper;
  33. // Moniker & Constants
  34. private const string _strDataFlowTaskMoniker = "STOCK:PipelineTask";
  35. // DataFlow Component Id
  36. private const string _strSourceDFComponentID = "DTSAdapter.RawSource.2";
  37. private const string _strDestinationDFReaderComponentID = "Microsoft.SqlServer.Dts.Pipeline.DataReaderDestinationAdapter, Microsoft.SqlServer.DataReaderDest, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91";
  38. // Other Members
  39. private string _strRAWFileName = string.Empty;
  40. private string _strPKGFileName = string.Empty;
  41. private Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSVariable100 _dataVariable = null;
  42. private StringBuilder _strRAWColNames = new StringBuilder();
  43. private char _delim = ',';
  44. private int _iDestInputID = 0;
  45. private string _strIndex = string.Empty;
  46. private int _iIndex = 0;
  47. private IDTSInput100 _objIDTSInput = null;
  48. private IDTSVirtualInput100 _objIDTSVirtualInput = null;
  49. private string[] _strFilterArray = null;
  50. #endregion
  51. #region Constructor
  52. internal clsDataFlow()
  53. {
  54. // Constructor
  55. }
  56. #endregion
  57. #region Internal Members/Properties
  58. internal string strRAWFileName
  59. {
  60. get { return this._strRAWFileName; }
  61. set { this._strRAWFileName = value; }
  62. }
  63. internal string strPKGFileName
  64. {
  65. get { return this._strPKGFileName; }
  66. set { this._strPKGFileName = value; }
  67. }
  68. internal string strRAWColNames
  69. {
  70. get { return this._strRAWColNames.ToString(); }
  71. set { this._strRAWColNames.Append(value); }
  72. }
  73. #endregion
  74. #region Creates Package
  75. // Creates Runtime Package
  76. internal void CreatePackage()
  77. {
  78. _objPackage = new Microsoft.SqlServer.Dts.Runtime.Wrapper.PackageClass();
  79. _objPackage.CreationDate = DateTime.Now;
  80. _objPackage.ProtectionLevel = Microsoft.SqlServer.Dts.Runtime.Wrapper.DTSProtectionLevel.DTSPL_DONTSAVESENSITIVE;
  81. _objPackage.Name = "RAWReader";
  82. _objPackage.Description = "RAW To Reader Conversion Package";
  83. _objPackage.DelayValidation = false;
  84. _objPackage.PackageType = DTSPackageType.DTSPKT_DTSDESIGNER100;
  85. _dataVariable = _objPackage.Variables.Add("DataRecords", false, "User", new System.Object());
  86. }
  87. #endregion
  88. #region Call Dataflow Component
  89. #region Source and Destination Component Methods
  90. // Creates Source Component (Output Collection)
  91. internal void CreateSourceComponent(string strRAWFilePath)
  92. {
  93. // Creates mainpipe for the executable component
  94. _objMainPipe = ((TaskHost)_objPackage.Executables.Add(_strDataFlowTaskMoniker)).InnerObject as MainPipe;
  95. // Adds a component from the MainPipe to the Source Metadata
  96. _objIDTSSRCMetaData = _objMainPipe.ComponentMetaDataCollection.New();
  97. // Sets the source component class id
  98. _objIDTSSRCMetaData.ComponentClassID = _strSourceDFComponentID;
  99. // Sets the locale property
  100. _objIDTSSRCMetaData.LocaleID = -1;
  101. // Instantiates the Wrapper, adding Source Metadata
  102. _objSourceWrapper = _objIDTSSRCMetaData.Instantiate();
  103. // Provides default properties
  104. _objSourceWrapper.ProvideComponentProperties();
  105. // Sets RAWFile Component Property
  106. _objSourceWrapper.SetComponentProperty("AccessMode", 0);
  107. _objSourceWrapper.SetComponentProperty("FileName", strRAWFileName);
  108. _objSourceWrapper.SetComponentProperty("FileNameVariable", null);
  109. // Sets the connection
  110. _objSourceWrapper.AcquireConnections(null);
  111. // Reinitializes the Source Metadata
  112. _objSourceWrapper.ReinitializeMetaData();
  113. // Fetch ColumnNames for the Metadata
  114. if (_strRAWColNames.Length == 0 && _strRAWColNames.ToString() == string.Empty)
  115. {
  116. foreach (IDTSOutputColumn100 idtsOutPutColumn in _objIDTSSRCMetaData.OutputCollection[0].OutputColumnCollection)
  117. {
  118. _strRAWColNames.Append(idtsOutPutColumn + ",");
  119. }
  120. }
  121. // Releases the Wrapper connection
  122. _objSourceWrapper.ReleaseConnections();
  123. }
  124. // Creates Destination Component (Input Collection)
  125. internal void CreateDestinationReaderComponent()
  126. {
  127. // 1. DataReader String: the class name of the DataReader destination.
  128. // 2. FailOnTimeout Boolean:Indicates wheather to fail when a ReadTimeout occurs. The default value is False.
  129. // 3. ReadeTimeout Integer: The number of milliseconds before a timeout occurs. The default value of this property is 30000 (30 seconds).
  130. // Adds a component from MainPipe to the Destination Recordset Metadata
  131. _objIDTSDSTReaderMetaData = _objMainPipe.ComponentMetaDataCollection.New();
  132. // Sets the Destination recordset component name
  133. _objIDTSDSTReaderMetaData.Name = "Test";
  134. // Sets the Destination recordset component class id
  135. _objIDTSDSTReaderMetaData.ComponentClassID = _strDestinationDFReaderComponentID;
  136. IDTSCustomProperty100 _property = _objIDTSDSTReaderMetaData.CustomPropertyCollection.New();
  137. _property.Name = "DataReader";
  138. _property.Value = new object();
  139. _property = _objIDTSDSTReaderMetaData.CustomPropertyCollection.New();
  140. _property.Name = "FailOnTimeout";
  141. _property.Value = false;
  142. _property = _objIDTSDSTReaderMetaData.CustomPropertyCollection.New();
  143. _property.Name = "ReadTimeout";
  144. _property.Value = 30000;
  145. // Instantiates the Wrapper adding Destination Recordset Metadata
  146. _objDestinationReaderWrapper = _objIDTSDSTReaderMetaData.Instantiate();
  147. // Provides default properties
  148. _objDestinationReaderWrapper.ProvideComponentProperties();
  149. // Sets the connection
  150. _objDestinationReaderWrapper.AcquireConnections(null);
  151. // Reinitializes the Destination Metadata
  152. _objDestinationReaderWrapper.ReinitializeMetaData();
  153. // Releases the Wrapper connection
  154. _objDestinationReaderWrapper.ReleaseConnections();
  155. // Creates the IDTSPath from the MainPipe
  156. _objIDTSPath = _objMainPipe.PathCollection.New();
  157. _objIDTSPath.AttachPathAndPropagateNotifications(_objIDTSSRCMetaData.OutputCollection[0], _objIDTSDSTReaderMetaData.InputCollection[0]);
  158. _objIDTSInput = _objIDTSDSTReaderMetaData.InputCollection[0];
  159. //Gets the Virtual Input Column Collection from the Destination Input Collection
  160. _objIDTSVirtualInput = _objIDTSInput.GetVirtualInput();
  161. _iDestInputID = Convert.ToInt32(_objIDTSInput.ID);
  162. // Splits the RAW Column Names into an array of strings
  163. if (strRAWColNames != null && strRAWColNames.Equals(string.Empty) == false && strRAWColNames != "")
  164. {
  165. if (strRAWColNames.EndsWith(","))
  166. {
  167. _iIndex = strRAWColNames.LastIndexOf(_delim);
  168. _strIndex = strRAWColNames.Remove(_iIndex);
  169. }
  170. _strFilterArray = _strIndex.Split(_delim);
  171. }
  172. // Sets Usagetype According to FilterArray
  173. foreach (IDTSVirtualInputColumn100 objIDTSVirtualInputColumn in _objIDTSVirtualInput.VirtualInputColumnCollection)
  174. {
  175. if (_strFilterArray == null)
  176. {
  177. // When FilterArray string is null
  178. _objDestinationReaderWrapper.SetUsageType(_iDestInputID, _objIDTSVirtualInput, objIDTSVirtualInputColumn.LineageID, DTSUsageType.UT_READONLY);
  179. }
  180. else
  181. {
  182. if (FilterField(objIDTSVirtualInputColumn.Name, _strFilterArray) == false)
  183. {
  184. // When FilterArray string is not null
  185. _objDestinationReaderWrapper.SetUsageType(_iDestInputID, _objIDTSVirtualInput, objIDTSVirtualInputColumn.LineageID, DTSUsageType.UT_READONLY);
  186. }
  187. }
  188. }
  189. // Sets the connection
  190. _objDestinationReaderWrapper.AcquireConnections(null);
  191. // Reinitializes the Destination Metadata
  192. _objDestinationReaderWrapper.ReinitializeMetaData();
  193. // Releases the Wrapper connection
  194. _objDestinationReaderWrapper.ReleaseConnections();
  195. }
  196. #endregion
  197. #region Other Supporting Dataflow Methods
  198. // Returns true if column name is in unchecked columns array, false otherwise
  199. internal bool FilterField(string strDestInputColumnName, string[] strArrUncheckedColumns)
  200. {
  201. if (strArrUncheckedColumns != null || strArrUncheckedColumns.ToString() != string.Empty)
  202. {
  203. foreach (string strUncheckedCol in strArrUncheckedColumns)
  204. {
  205. if (strDestInputColumnName.Equals(strArrUncheckedColumns))
  206. {
  207. return true;
  208. }
  209. }
  210. }
  211. return false;
  212. }
  213. internal void ClearRawColumnNames()
  214. {
  215. _strRAWColNames.Remove(0, _strRAWColNames.Length);
  216. }
  217. #endregion
  218. #region Save Package
  219. // Saves a new package to file
  220. internal string SavePackage()
  221. {
  222. // Creates DTS Runtime Application instance
  223. app = new Microsoft.SqlServer.Dts.Runtime.Wrapper.Application();
  224. // Save DTSX file to temp folder
  225. string strPath = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".dtsx";
  226. app.SaveToXML(strPath, _objPackage, null);
  227. return strPath;
  228. }
  229. #endregion
  230. #endregion
  231. }
  232. }