"I have a script of insert and update in a folder which is executed by window service. In the window service, there is a function that will check and get the batch of 10 files and executes and move to the success or failure folder. Now the script volume is increased and the window service is taking a long time to process the scripts."
public bool CheckQueueExist()
{
bool commitstatus = false;
string fullfilepath = string.Empty;
StringBuilder strQueryBuild = new StringBuilder();
try
{
if (QueueVariables.NewFolderPath == "")
{
LoadFilePath();
}
string path = QueueVariables.NewFolderPath;
DirectoryInfo info = new DirectoryInfo(path);
FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ThenBy(a => a.Name).Take(10).ToArray();
foreach (FileInfo file in files)
{
fullfilepath = file.FullName;
string Filename = file.Name;
try
{
string[] lines = System.IO.File.ReadAllLines(file.FullName);
string query = "";
foreach (string line in lines)
{
query = query + line;
}
strQueryBuild.Append(query);
}
catch (Exception ex)
{
commitstatus = false;
AppHelper.ErrrorLog(ex, "File in USE: " + fullfilepath);
}
}
int RowsAffected = 0;
try
{
if (!string.IsNullOrEmpty(strQueryBuild.ToString()))
{
string connstr = System.Configuration.ConfigurationManager.ConnectionStrings["xyz"].ConnectionString;
using (var conn = new NpgsqlConnection(connstr))
{
conn.Open();
var tra = conn.BeginTransaction();
try
{
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 0;
cmd.CommandText = strQueryBuild.ToString();
RowsAffected = cmd.ExecuteNonQuery();
if (RowsAffected == 0)
{
RowsAffected = 1;
// Even Condition not Match, the Query has accepted.So it should be consider as 1
}
tra.Commit();
commitstatus = true;
}
catch (Exception ex)
{
commitstatus = false;
AppHelper.ErrrorLog(ex, "UploadFileData-Error1");
RowsAffected = -1;
tra.Rollback();
}
finally
{
}
}
}
}
catch (Exception ex)
{
commitstatus = false;
AppHelper.ErrrorLog(ex, "ProcessQueue-Error2");
}
}
}