public bool UpdateSms(Sms sms)
 {
     var cn = new SqlConnection(connectionString);
     int affectedRows = 0;
     try
     {
         var cmd =
             new SqlCommand(
                 "UPDATE SmsArchive SET Message=@Message, CellNr=@CellNr, DateCreated=@DateCreated, DateSend=@DateSend, TryNr=@TryNr WHERE Id=@Id");
         cmd.Parameters.AddWithValue("@Id", sms.Id);
         cmd.Parameters.AddWithValue("@Message", sms.Message);
         cmd.Parameters.AddWithValue("@CellNr", sms.CellNr);
         cmd.Parameters.AddWithValue("@DateCreated", sms.DateCreated);
         if (sms.DateSend == DateTime.MinValue)
             cmd.Parameters.AddWithValue("@DateSend", DBNull.Value);
         else
             cmd.Parameters.AddWithValue("@DateSend", sms.DateSend);
         cmd.Parameters.AddWithValue("@TryNr", sms.TryNr);
         cmd.Connection = cn;
         cn.Open();
         affectedRows = cmd.ExecuteNonQuery();
     }
     catch (Exception ex)
     {
         logger.Error(string.Format("UpdateSms|sms id:{0} ERROR:{1}", sms.Id, ex.Message));
     }
     finally
     {
         cn.Close();
     }
     return affectedRows > 0;
 }
Exemple #2
0
        public static void SendSms(Sms sms)
        {
            var smsArchiveManager = new SmsArchiveManager(ConfigurationManager.ConnectionStrings["fccl_v2"].ConnectionString);
            SerialPort port = new SerialPort();
            clsSMS objclsSMS = new clsSMS();
            string portname = ConfigurationManager.AppSettings["ComPort"].ToString();
            string baudrate = "57600";
            port = objclsSMS.OpenPort(portname, baudrate);
            try
            {
                if (objclsSMS.sendMsg(port, portname, "57600", sms.CellNr, sms.Message))
                {
                    Logger.Info(string.Format("SendSmsWorker | Sms-ul cu id-ul: {0} a fost trimis", sms.Id));
                    sms.DateSend = DateTime.Now;
                    smsArchiveManager.UpdateSms(sms);
                }
                else
                {
                    sms.TryNr++;
                    smsArchiveManager.UpdateSms(sms);
                    Logger.Error(string.Format("SendSmsWorker | Trimitere esuata a sms-ului cu id-ul: {0}", sms.Id));
                }
                objclsSMS.ClosePort(port);
            }
            catch (Exception ex)
            {
                sms.TryNr++;
                smsArchiveManager.UpdateSms(sms);
                Logger.Error(string.Format("SendSmsWorker | Trimitere esuata a sms-ului cu id-ul: {0} error: {1}",
                    sms.Id, ex.Message));

            }
            finally
            {
                objclsSMS.ClosePort(port);
            }
        }
 public Sms GetSmsById(string id)
 {
     Sms sms = null;
     var cnn = new SqlConnection(connectionString);
     var cmd = new SqlCommand();
     SqlDataReader reader = null;
     try
     {
         cmd.Connection = cnn;
         cmd.CommandText = "SELECT Id, DateCreated, Message, CellNr, TryNr FROM SmsArchive WHERE Id=@Id";
         cmd.Parameters.AddWithValue("Id", id);
         cnn.Open();
         reader = cmd.ExecuteReader();
         while (reader.Read())
         {
             sms = new Sms
             {
                 Id = new Guid(reader[0].ToString()),
                 DateCreated = Convert.ToDateTime(reader[1].ToString()),
                 Message = reader[2].ToString(),
                 CellNr = reader[3].ToString(),
                 TryNr = Convert.ToInt32(reader[4].ToString())
             };
         }
     }
     catch (Exception ex)
     {
         logger.Error(string.Format("GetSmsById|error:{0}", ex.Message));
     }
     finally
     {
         if (reader != null)
             reader.Close();
         cnn.Close();
     }
     return sms;
 }