HEADER DETAIL AND FOOTER IN C#

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
/*
Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.
*/
using System;
using System.IO;
using System.Text;
using System.Data;
//using System.Math;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
namespace ST_8b99b23ffd944c6abe15bca3437320d8.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
public void Main()
{
Dts.TaskResult = (int)ScriptResults.Success;
Boolean fireagain = false;
int taskResult;
string fileSourcePath;
string fileDestPath;
StreamReader sr;
StreamWriter sw;
string Str;
string delim = ",";
string RecordType;
// Dim FileCreateDate As Date
string LastRecordType = " ";
string FilesProcessed;
string CurrentDate;
string SourceFilePath;
string FileName;


// Dim ret As Integer
CurrentDate = DateTime.Now.ToString("yyyyMMdd");
Dts.Events.FireInformation(-1, "CurrentDate", CurrentDate.ToString(), String.Empty, 0, ref fireagain);
try
{
fileSourcePath= Dts.Variables["User::SourceFilePath"].Value.ToString(); //SourceFilePath
fileDestPath = Dts.Variables["User::Destination"].Value.ToString();//Destination
FilesProcessed = Dts.Variables["User::FilesProcessed"].Value.ToString();//FilesProcessed
FileName = Dts.Variables["User::FileName"].Value.ToString();
//fileSourcePath = Dts.Variables["SourceFilePath"].Value.ToString();//FilePath
//fileDestPath = Dts.Variable["Destination"].Value.ToString;//Destination
//FilesProcessed = ReadVariable("FilesProcessed").ToString();//FilesProcessed
//FileName = Dts.Variables["FileName"].Value.ToString();//FileName
//the value before we change it here
Dts.Events.FireInformation(1, "fileSourcePath", fileSourcePath, String.Empty, 0, ref fireagain);
Dts.Events.FireInformation(-1, "FileName", FileName, String.Empty, 0, ref fireagain);
// Dim valid As Boolean
// Dim fileID As String
// Dim contract As Stri
int sep;
sep = FileName.IndexOf("//");
SourceFilePath = FileName.Substring((sep + 1));//fileSourcePath.Substring((sep + 1));
Dts.Events.FireInformation(-1, "SourceFilePath", SourceFilePath, String.Empty, 0, ref fireagain);
//sr = File.OpenText(fileSourcePath + FileName);//File.OpenText(fileSourcePath);
sr = File.OpenText(FileName);
//string test = Dts.Variables[sr].Value.ToString();
Str = sr.ReadLine();
//Dts.Variables[Str].Value.ToString();
//Dts.Events.FireInformation(-1, "Str-ReadLine", Str.ToString(), String.Empty, 0, ref fireagain);
// Get the first data line of the file
// contract = Str.Substring(10, 5)
// fileID = Str.Substring(15, 12)
Int32 Counter;
Counter = (int)ReadVariable("Counter");
Counter = (Counter + 1);
WriteVariable("Counter", Counter);
Dts.Events.FireInformation(-1, "Counter", Counter.ToString(), String.Empty, 0, ref fireagain);
//int loc = fileDestPath.LastIndexOf(".");
//string ext = fileDestPath.Substring(loc, 4);
//string filename = fileDestPath.Substring(0, loc);
// fileDestPath = filename + "_" + DateTime.Now.ToString("yyyyMMdd") + ext
fileDestPath = Dts.Variables["User::Destination"].Value.ToString() + "_" + CurrentDate + ".CSV";
sw = File.CreateText(fileDestPath);
WriteVariable("NewFileName", fileDestPath);
Dts.Events.FireInformation(-1, "NewFileName", ReadVariable("NewFileName").ToString(), String.Empty, 0, ref fireagain);
//cnt = 1;
while (!string.IsNullOrEmpty(Str))
{
StringBuilder sb = new StringBuilder();
if ((Str.Length > 3))
{
// No Valid records have less than 3 chars
RecordType = Str.Substring(0, 3);
switch (RecordType)
{
case "CHD":
sw.WriteLine("RECORD ID,SEQUENCE NO,CONTRACT IDENTIFIER,CONTRACT NAME,COVERAGE YEAR DATE,RECONCILIATION NUMBER,SYSTEM DATE,SYSTEM TIME,REPORT ID,FILLER");
sb.Append((Str.Substring(0, 3) + delim));
//RECORD ID
sb.Append((Str.Substring(3, 7) + delim));
//SEQUENCE ID
sb.Append((Str.Substring(10, 5) + delim));
//CONTRACT IDENTIFIER
sb.Append((Str.Substring(15, 50) + delim));
//CONTRACT NAME
sb.Append((Str.Substring(65, 4) + delim));
// CONVERAGE YEAR DATE
sb.Append((Str.Substring(69, 4) + delim));
// RECONCILIATION NUMBER
sb.Append((Str.Substring(73, 8) + delim));
// SYSTEM DATE
sb.Append((Str.Substring(81, 6) + delim));
// SYSTEM TIME
sb.Append((Str.Substring(87, 9) + delim));
// REPORT ID
sb.Append((Str.Substring(96, 204) + delim));
// FILLER
sw.WriteLine(sb.ToString());
Dts.Events.FireInformation(-1, "sb", sb.ToString(), String.Empty, 0, ref fireagain);
break;
case "DET":
if (!(LastRecordType == RecordType))
{
sw.WriteLine("RECORD ID," +
"SEQUENCE NO," +
"CONTRACT IDENTIFIER," +
"PLAN BENEFIT PACKAGE IDENTIFIER," +
"CONTRACT NAME," +
"PBP NAME," +
"CURRENT RECONCILIATION NUMBER," +
"PREVIOUS RECOUNCILIATION NUMBER," +
"CURRENT TOTAL COVERAGE GAP DISCOUNT PAYMENT AMOUNT," +
"PREVIOUS TOTAL COVERAGE GAP DISCOUNT PAYMENT AMOUNT," +
"DELTA TOTAL COVERAGE GAP DISCOUNT PAYMENT AMOUNT," +
"CURRENT TOTAL COVERAGE GAP DISCOUNT INVOICED AMOUNT," +
"PREVIOUS TOTAL COVERAGE GAP DISCOUNT INVOICED AMOUNT," +
"DELTA TOTAL COVERAGE GAP DISCOUNT INVOICED AMOUNT," +
"CURRENT COVERAGE GAP DISCOUNT RECONCILIATION AMOUNT," +
"PREVIOUS COVERAGE GAP DISCOUNT RECONCILIATION AMOUNT," +
"DELTA COVERAGE GAP DISCOUNT RECONCILIATION AMOUNT," +
"FILLER"
);
}
sb.Append((Str.Substring(0, 3) + delim));
sb.Append((Str.Substring(3, 7) + delim));
// SEQUENCE-NO
sb.Append((Str.Substring(10, 5) + delim));
// CONTRACT-IDENTIFIER
sb.Append((Str.Substring(15, 3) + delim));
// PLAN-BENEFIT-PACKAGE-IDENTIFIER
sb.Append((Str.Substring(18, 50) + delim));
// CONTRACT-NAME
sb.Append((Str.Substring(68, 50) + delim));
// PBP-NAME
sb.Append((Str.Substring(118, 4) + delim));
// CURRENT RECONCILIATION NUMBER
sb.Append((Str.Substring(122, 4) + delim));
// PREVIOUS RECONCILIATION NUMBER
sb.Append(("$" + ConvertEBCDIC(Str.Substring(126, 14)).ToString("F2") + delim));
//CURRENT TOTAL COVERAGE GAP DISCOUNT PAYMENT AMOUNT
sb.Append(("$"
+ ConvertEBCDIC(Str.Substring(140, 14)).ToString("F2") + delim));
// PREVIOUS TOTAL COVERAGE GAP DISCOUNT PAYMENT AMOUNT
sb.Append(("$" + ConvertEBCDIC(Str.Substring(154, 14)).ToString("F2") + delim));
// DELTA TOTAL COVERAGE GAP DISCOUNT PAYMENT AMOUNT
sb.Append(("$"
+ ConvertEBCDIC(Str.Substring(168, 14)).ToString("F2") + delim));
// CURRENT TOTAL COVERAGE GAP DISCOUNT INVOICED AMOUNT
sb.Append(("$"
+ ConvertEBCDIC(Str.Substring(182, 14)).ToString("F2") + delim));
// PREVIOUS TOTAL COVERAGE GAP DISCOUNT INVOICED AMOUNT
sb.Append(("$"
+ ConvertEBCDIC(Str.Substring(196, 14)).ToString("F2") + delim));
// DELTA TOTAL COVERAGE GAP DISCOUNT INVOICED AMOUNT
sb.Append(("$"
+ ConvertEBCDIC(Str.Substring(210, 14)).ToString("F2") + delim));
// CURRENT COVERAGE GAP DISCOUNT RECONCILIATION AMOUNT
sb.Append(("$"
+ ConvertEBCDIC(Str.Substring(224, 14)).ToString("F2") + delim));
// PREVIOUS COVERAGE GAP DISCOUNT RECONCILIATION AMOUNT
sb.Append(("$"
+ ConvertEBCDIC(Str.Substring(238, 14)).ToString("F2") + delim));
// DELTA COVERAGE GAP DISCOUNT RECONCILIATION AMOUNT
sb.Append((Str.Substring(252, 48) + delim));
// FILLER
sw.WriteLine(sb.ToString());
Dts.Events.FireInformation(-1, "sb", sb.ToString(), String.Empty, 0, ref fireagain);
break;


case "CTR":

sw.WriteLine("RECORD ID," +
"SEQUENCE NO," +
"CONTRACT IDENTIFIER," +
"CONTRACT NAME," +
"CURRENT RECONCILATION NUMBER," +
"CURRENT TOTAL COVERAGE GAP DISCOUNT PAYMENT AMOUNT," +
"PREVIOUS TOTAL COVERAGE GAP DISCOUNT PAYMENT AMOUNT," +
"DELTA TOTAL COVERAGE GAP DISCOUNT PAYMENT AMOUNT," +
"CURRENT TOTAL COVERGAE GAP DISCOUNT INVOICED AMOUNT," +
"PREVIOUS TOTAL COVERAGE GAP DISCOUNT INVOICED AMOUNT," +
"DELTA TOTAL COVERAGE GAP DISCOUNT INVOICED AMOUNT," +
"CURRENT COVERAGE GAP DISCOUNT RECONCILIATION AMOUNT," +
"PREVIOUS COVERAGE GAP DISCOUNT RECONCILIATION AMOUNT," +
"DELTA COVERAGE GAP DISCOUNT RECONCILIATION AMOUNT," +
"COUNT OF PBPs," +
"FILLER"
);
sb.Append((Str.Substring(0, 3) + delim));
// RECORD-ID
sb.Append((Str.Substring(3, 7) + delim));
// SEQUENCE-NO
sb.Append((Str.Substring(10, 5) + delim));
// CONTRACT-IDENTIFIER
sb.Append((Str.Substring(15, 50) + delim));
// CONTRACT-NAME
sb.Append((Str.Substring(65, 4) + delim));
// CURRENT RECONCILATION NUMBER
sb.Append(("$"
+ ConvertEBCDIC(Str.Substring(69, 14)).ToString("F2") + delim));
// CURRENT TOTAL COVERAGE GAP DISCOUNT PAYMENT AMOUNT
sb.Append(("$"
+ ConvertEBCDIC(Str.Substring(83, 14)).ToString("F2") + delim));
// PREVIOUS TOTAL COVERAGE GAP DISCOUNT PAYMENT AMOUNT
sb.Append(("$"
+ ConvertEBCDIC(Str.Substring(97, 14)).ToString("F2") + delim));
// DELTA TOTAL COVERAGE GAP DISCOUNT PAYMENT AMOUNT
sb.Append(("$"
+ ConvertEBCDIC(Str.Substring(111, 14)).ToString("F2") + delim));
// CURRENT TOTAL COVERAGE GAP DISCOUNT INVOICED AMOUNT
sb.Append(("$"
+ ConvertEBCDIC(Str.Substring(125, 14)).ToString("F2") + delim));
// PREVIOUS TOTAL COVERAGE GAP DISCOUNT INVOICED AMOUNT
sb.Append(("$"
+ ConvertEBCDIC(Str.Substring(139, 14)).ToString("F2") + delim));
// DELTA TOTAL COVERAGE GAP DISCOUNT INVOICED AMOUNT
sb.Append(("$"
+ ConvertEBCDIC(Str.Substring(153, 14)).ToString("F2") + delim));
// CURRENT COVERAGE GAP DISCOUNT RECONCILIATION AMOUNT
sb.Append(("$"
+ ConvertEBCDIC(Str.Substring(167, 14)).ToString("F2") + delim));
// PREVIOUS COVERAGE GAP DISCOUNT RECONCILIATION AMOUNT
sb.Append(("$"
+ ConvertEBCDIC(Str.Substring(181, 14)).ToString("F2") + delim));
// DELTA COVERAGE GAP DISCOUNT RECONCILIATION AMOUNT
sb.Append((Str.Substring(195, 9) + delim));
// COUNT OF PBPs
sb.Append((Str.Substring(204, 96) + delim));
// FILLER
sw.WriteLine(sb.ToString());
Dts.Events.FireInformation(-1, "sb Tail", sb.ToString(), String.Empty, 0, ref fireagain);
break;
default:
sw.WriteLine("INVALID");
break;
}
}
//LastRecordType = RecordType.ToString();
Str = sr.ReadLine();
}
sr.Close();
sw.Close();
// WriteVariable("FilesProcessed", (FilesProcessed
// + (fileDestPath.Replace(ReadVariable("P2PPayableReportPath").ToString(), "").ToString + "rn")));
}
catch (Exception ex)
{
taskResult = Dts.TaskResult = (int)ScriptResults.Failure;
Dts.Events.FireError(-1, "Exception", ex.Message, String.Empty, 0);
}
Dts.TaskResult = (int)ScriptResults.Success;
}
Decimal ConvertEBCDIC(string signed)
{
string signChar;
string numb;
Decimal nn = 0;
Decimal final = 0;
int len;
Boolean fireagain = false;
bool negative = false;
Dts.Events.FireInformation(-1, "function input string", signed.ToString(), String.Empty, 0, ref fireagain);
if ((signed.Length > 1))
{
len = signed.Length;
signChar = signed.Substring((len - 1), 1);
// last character only, which includes sign
numb = signed.Substring(0, (len - 1));
Dts.Events.FireInformation(-1, "ssignChar", signChar.ToString(), String.Empty, 0, ref fireagain);
Dts.Events.FireInformation(-1, "numb", numb.ToString(), String.Empty, 0, ref fireagain);
// number with out the last digit
switch (signChar)
{
case "{":
nn = 0;
break;
case "A":
nn = 1;
break;
case "B":
nn = 2;
break;
case "C":
nn = 3;
break;
case "D":
nn = 4;
break;
case "E":
nn = 5;
break;
case "F":
nn = 6;
break;
case "G":
nn = 7;
break;
case "H":
nn = 8;
break;
case "I":
nn = 9;
break;
case "}":
nn = 0;
negative = true;
break;
case "J":
nn = 1;
negative = true;
break;
case "K":
nn = 2;
negative = true;
break;
case "L":
nn = 2;
negative = true;
break;
case "M":
nn = 4;
negative = true;
break;
case "N":
nn = 5;
negative = true;
break;
case "O":
nn = 6;
negative = true;
break;
case "P":
nn = 7;
negative = true;
break;
case "Q":
nn = 8;
negative = true;
break;
case "R":
nn = 9;
negative = true;
break;
}
final = Decimal.Parse(numb);
Dts.Events.FireInformation(-1, "final 1", final.ToString(), String.Empty, 0, ref fireagain);
final = (final * 10);
Dts.Events.FireInformation(-1, "final 2", final.ToString(), String.Empty, 0, ref fireagain);
final = (final + nn);
Dts.Events.FireInformation(-1, "final 3", final.ToString(), String.Empty, 0, ref fireagain);
final = (final / 100);
if (negative)
{
final = (final * -1);
}
Dts.Events.FireInformation(-1, "final 4", final.ToString(), String.Empty, 0, ref fireagain);
}
return final;
}
Decimal ConvertEBCDICDivideBy10000(string signed)
{
string signChar;
string numb;
Decimal nn = 0;
Decimal final = 0;
int len;
Boolean fireagain = false;
bool negative = false;
// Dts.Events.FireInformation(-1, "function input string", signed.ToString(), String.Empty, 0, ref fireagain);
if ((signed.Length > 1))
{
len = signed.Length;
signChar = signed.Substring((len - 1), 1);
// last character only, which includes sign
numb = signed.Substring(0, (len - 1));
Dts.Events.FireInformation(-1, "ssignChar", signChar.ToString(), String.Empty, 0, ref fireagain);
Dts.Events.FireInformation(-1, "numb", numb.ToString(), String.Empty, 0, ref fireagain);
// number with out the last digit
switch (signChar)
{
case "{":
nn = 0;
break;
case "A":
nn = 1;
break;
case "B":
nn = 2;
break;
case "C":
nn = 3;
break;
case "D":
nn = 4;
break;
case "E":
nn = 5;
break;
case "F":
nn = 6;
break;
case "G":
nn = 7;
break;
case "H":
nn = 8;
break;
case "I":
nn = 9;
break;
case "}":
nn = 0;
negative = true;
break;
case "J":
nn = 1;
negative = true;
break;
case "K":
nn = 2;
negative = true;
break;
case "L":
nn = 2;
negative = true;
break;
case "M":
nn = 4;
negative = true;
break;
case "N":
nn = 5;
negative = true;
break;
case "O":
nn = 6;
negative = true;
break;
case "P":
nn = 7;
negative = true;
break;
case "Q":
nn = 8;
negative = true;
break;
case "R":
nn = 9;
negative = true;
break;
}
final = Decimal.Parse(numb);
// Dts.Events.FireInformation(-1, "final 1", final.ToString(), String.Empty, 0, ref fireagain);
final = (final * 10);
// Dts.Events.FireInformation(-1, "final 2", final.ToString(), String.Empty, 0, ref fireagain);
final = (final + nn);
// Dts.Events.FireInformation(-1, "final 3", final.ToString(), String.Empty, 0, ref fireagain);
final = (final / 10000);
if (negative)
{
final = (final * -1);
}
// Dts.Events.FireInformation(-1, "final 4", final.ToString(), String.Empty, 0, ref fireagain);
}
return final;
}
private object ReadVariable(string varName)
{
object result;
try
{
Variables vars = null;
Dts.VariableDispenser.LockForRead(varName);
Dts.VariableDispenser.GetVariables(ref vars);
try
{
result = vars[varName].Value;
}
catch (Exception ex)
{
throw ex;
}
finally
{
vars.Unlock();
}
}
catch (Exception ex)
{
throw ex;
}
return result;
}
private void WriteVariable(string varName, object varValue)
{
try
{
Variables vars = null;
Dts.VariableDispenser.LockForWrite(varName);
Dts.VariableDispenser.GetVariables(ref vars);
try
{
vars[varName].Value = varValue;
}
catch (Exception ex)
{
throw ex;
}
finally
{
vars.Unlock();
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}

Hi,
I want an output as follow.
RECORD ID
SEQUENCE NO
CONTRACT NAME
CONTRACT ID
AMOUNT
CHD
59
H1026
HEALTH OPTION​
$6525
RECORD ID
SEQUENCE NO
CONTRACT NAME
CONTRACT ID
AMOUNT
DET
59
H1026
HEALTH OPTION​
$6525
59
H1026
HEALTH OPTION​
$6525
59
H1026
HEALTH OPTION​
$6525
RECORD ID
SEQUENCE NO
CONTRACT NAME
CONTRACT ID
AMOUNT
CTR
59
H1026
HEALTH OPTION​
$6525
HOW AND WHAT TO WRITE IN SCRIPT TO GET THIS OUTPUT. PLEASE HELP ME . can i make changes in existing script or else create another one and connect with current one?
surabhi dave

View the full article
 
Back
Top