public void Main()
{
// TODO: Add your code here
string strFileName = string.Empty;
bool bFileSuccess;
string strUniqueFileName = string.Empty;
strFileName = GetSourceFileName(out bFileSuccess);
if (!strFileName.Equals(string.Empty))
{
if (bFileSuccess)
{
LogFileName(strFileName, bFileSuccess);
Dts.Variables["pExtractDate"].Value = dtFileExtract;//Set File extract date
}
else
{
Dts.TaskResult = (int)ScriptResults.Failure;
LogFileName(strFileName,bFileSuccess, "Wrong source file format");
}
}
else
{
Dts.TaskResult = (int)ScriptResults.Success;
}
Dts.Variables["pVarFileName"].Value = strFileName;
////strUniqueFileName = System.DateTime.Now.ToString("dd-MM-yyyy") + System.DateTime.Now.TimeOfDay.Milliseconds.ToString() + ".xls";
//////Create a copy of file
////FileInfo fiSourceFile = new FileInfo(strSourceFile);
////fiSourceFile.CopyTo(Dts.Variables["InputFileFolder"].Value.ToString() + strUniqueFileName, true);
////Dts.Variables["TabFileSource"].Value = Dts.Variables["InputFileFolder"].Value.ToString() + strUniqueFileName;
}
public String GetSourceFileName(out bool bFileSuccess)
{
String strRetSourceFile = string.Empty;
Boolean blDisableTaskFlag = false;
String strSourceFolder = Dts.Variables["InputFileFolder"].Value.ToString();
String strSourceFileTemplate = Dts.Variables["FileNameTemplate"].Value.ToString().ToLower();
DirectoryInfo diSourceFolder = new DirectoryInfo(strSourceFolder);
FileInfo[] fiSourceFiles = diSourceFolder.GetFiles();
bFileSuccess = true;
foreach (FileInfo fiSourceFile in fiSourceFiles)
{
if (fiSourceFile.Name.ToLower().Contains(strSourceFileTemplate))
{
strRetSourceFile = fiSourceFile.Name;
String strDatePart = strRetSourceFile.Substring(strSourceFileTemplate.Length);
strDatePart = strDatePart.Substring(0, strDatePart.Length - 4);
Dts.Variables["FileSource"].Value = fiSourceFile.FullName;
//Form File extraction date
if (!DateTime.TryParse(strDatePart, out dtFileExtract))
{
bFileSuccess = false;
}
blDisableTaskFlag = false;
break;
}
else
{
blDisableTaskFlag = true;
}
}
if (strRetSourceFile.Equals(string.Empty))
{
blDisableTaskFlag = true;
//blDisableTaskFlag = false;
}
Dts.Variables["DisableTask"].Value = blDisableTaskFlag;
return strRetSourceFile;
}
public void LogFileName(string strFileName,bool bSuccess)
{
LogFileNameEx(strFileName,bSuccess, "");
}
public void LogFileName(string strFileName,bool bSuccess,string strMessage)
{
LogFileNameEx(strFileName,bSuccess, strMessage);
}
private void LogFileNameEx(string strFileName, bool bSuccess,string strMessage)
{
OleDbConnection sqlConnection = new OleDbConnection(Dts.Connections["ITGWarehouse"].ConnectionString);
using (OleDbCommand cmdUspFileProcessingDetailsOnBegin = new OleDbCommand("MD.usp_GetFileProcessingList"))
{
cmdUspFileProcessingDetailsOnBegin.Connection = sqlConnection;
cmdUspFileProcessingDetailsOnBegin.CommandType = CommandType.StoredProcedure;
cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pExecutionLogId", SqlDbType.Int).Value = Dts.Variables["LogID"].Value;
cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pFileType", SqlDbType.VarChar).Value = Dts.Variables["FileNameTemplate"].Value;
cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pFileName", SqlDbType.VarChar).Value = strFileName;
cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pMessage", SqlDbType.VarChar).Value = strMessage;
if (bSuccess)
cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pExtractDate", SqlDbType.DateTime).Value = dtFileExtract;
else
cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pExtractDate", SqlDbType.DateTime).Value = DBNull.Value;
try
{
sqlConnection.Open();
cmdUspFileProcessingDetailsOnBegin.ExecuteNonQuery();
}
finally
{
sqlConnection.Close();
sqlConnection.Dispose();
}
}
}
No comments:
Post a Comment