public static ServiceCrmEvolutionItem GetByCode(entitiesEvolveCentralConsole ctx, string servicecode) { var item = new ServiceCrmEvolutionItem(); item = (from i in ctx.ServiceCrmEvolutionItems where i.Code == servicecode where i.IsActive == true select i).FirstOrDefault(); return(item); }
public static bool Save(entitiesEvolveCentralConsole ctx, ServiceCrmEvolutionItem item) { try { if (item.Id == 0) { ctx.ServiceCrmEvolutionItems.Add(item); } ctx.SaveChanges(); } catch (Exception ex) { return(false); } return(true); }
static void Main(string[] args) { bool isSuccessful = true; syncstartdate = DateTime.Now; Console.Write("EVOLVE CENTRAL CONSOLE STARTED!"); string filename = "Configuration.txt"; Console.Write("READING CONFIGURATION FILE..."); string servicecode = GetServiceCode(filename); if (servicecode == null) { string message = "READING SERVICE CODE FROM CONFIGURATION FILE FAILED!"; Console.Write(message); SendMail(message); isSuccessful = false; return; } Console.Write("READING CLIENT CODE FROM CONFIGURATION FILE WAS SUCCESSFUL! - SERVICE CODE: " + servicecode); Console.Write("READING SERVICE ITEM FROM THE DATABASE..."); DAL.ServiceCrmEvolutionItem serviceitem = DAL.ServiceCrmEvolution.GetByCode(ctx, servicecode); if (serviceitem == null) { string message = "READING SERVICE ITEM WITH CODE= " + servicecode + "FROM THE DATABASE FAILED!"; LogMessage(message, false); SendMail(message); isSuccessful = false; return; } int serviceid = serviceitem.Id; CurrentServiceId = serviceid; CreateLogInTheDatabase(); LogMessage("READING SERVICE ITEM FROM THE DATABASE WAS SUCESSFUL! - SERVICE ID: " + serviceid.ToString(), true); List <DAL.ServiceCrmEvolutionDetailItem> items = new List <DAL.ServiceCrmEvolutionDetailItem>(); LogMessage("READING SQL COMMANDS FROM THE DATABASE...", true); items = DAL.ServiceCrmEvolutionDetail.GetByServiceID(ctx, Convert.ToInt32(serviceid)); if (items.Count == 0) { LogMessage("READING SQL COMMANDS FROM THE DATABASE WAS SUCCESSFUL! NUMBER OF ITEMS: " + items.Count.ToString(), false); return; } LogMessage("READING SQL COMMANDS FROM THE DATABASE WAS SUCCESSFUL! NUMBER OF ITEMS: " + items.Count.ToString(), true); string connstring = null; string databasename_source = null; string databasename_destination = null; connstring = "Server=[@DATABASESERVER];User Id=[@DATABASEUSERNAME];Password=[@DATABASEPASSWORD];DataBase=[@DATABASENAME];"; connstring = connstring.Replace("[@DATABASESERVER]", serviceitem.Connection_DatabaseServer); connstring = connstring.Replace("[@DATABASEUSERNAME]", serviceitem.Connection_DatabaseUserName); connstring = connstring.Replace("[@DATABASEPASSWORD]", serviceitem.Connection_DatabasePassword); connstring = connstring.Replace("[@DATABASENAME]", serviceitem.Connection_DatabaseName); LogMessage("CONNECTING TO THE DATABASE...", true); SqlConnection sqlconn = new SqlConnection(connstring); try { sqlconn.Open(); string message = "CONNECTING TO THE DATABASE WAS SUCCESSFUL!"; LogMessage(message, true); } catch (Exception ex) { LogException(ex, "CONNECTING TO THE DATABASE FAILED!"); isSuccessful = false; } LogMessage("EXECUTION OF SQL COMMANDS IS STARTING...", true); foreach (DAL.ServiceCrmEvolutionDetailItem item in items) { try { string sqlcommand = item.Command; sqlcommand = sqlcommand.Replace("[sourceDataBase]", "[" + serviceitem.DatabaseName_Source + "]"); sqlcommand = sqlcommand.Replace("[destinationDataBase]", "[" + serviceitem.DatabaseName_Destination + "]"); SqlCommand cmd = new SqlCommand(); cmd.Connection = sqlconn; cmd.CommandType = CommandType.Text; cmd.CommandTimeout = 0; if (!string.IsNullOrEmpty(sqlcommand)) { // LogMessage("EXECUTING SQL COMMAND... - " + item.Name + " ( " + item.Id.ToString() + " )"); cmd.CommandText = sqlcommand; cmd.ExecuteNonQuery(); LogSqlCommand(item, "EXECUTING SQL COMMAND... - " + item.Name + " ( " + item.Id.ToString() + " ) WAS SUCCESSFUL!"); } } catch (Exception ex) { LogSqlCommandException(item, ex, "EXECUTING SQL COMMAND... - " + item.Name + " ( " + item.Id.ToString() + " ) HAS FAILED!"); isSuccessful = false; } finally { if (sqlconn != null) { // ((IDisposable)sqlconn).Dispose(); } } } try { sqlconn.Close(); } catch (Exception ex) { isSuccessful = false; } LogMessage("SYNCHRONIZATION FINISHED!", true); serviceitem.LastSyncDate = DateTime.Now; serviceitem.LastSyncSuccessful = Convert.ToBoolean(isSuccessful); DAL.ServiceCrmEvolution.Save(ctx, serviceitem); }