public bool AddMessage(MessageModel messageModel, out string error) { if (messageModel.TextOfMessage == null || string.IsNullOrWhiteSpace(messageModel.TextOfMessage)) { error = "Сообщение пусто. Введите сообщение заново."; return(false); } lock (lockObj) { messageModel.MessageID = CurrentOrderNumber.ToString(); var MessageIdInDB = Guid.NewGuid().ToString(); OrderNumber_MessageSqlID.Add(CurrentOrderNumber++, MessageIdInDB); MessagesCache.Add(messageModel); SqlConnection.Open(); var sqlCommand = new SqlCommand("INSERT INTO [Messages] (Id,Username, TextOfMessage, TimeOfMessage) " + "VALUES(@Id ,@Username, @TextOfMessage, @TimeOfMessage)", SqlConnection); sqlCommand.Parameters.AddWithValue("Id", MessageIdInDB); sqlCommand.Parameters.AddWithValue("Username", messageModel.UserName); sqlCommand.Parameters.AddWithValue("TextOfMessage", messageModel.TextOfMessage); sqlCommand.Parameters.AddWithValue("TimeOfMessage", messageModel.TimeOfMessage.ToString("dd.MM.yyyy HH:mm:ss.ffff")); sqlCommand.ExecuteNonQuery(); SqlConnection.Close(); error = null; return(true); } }
public bool AddMessage(MessageModel message, out string error) { message.MessageID = (LastMessageID++).ToString(); MessagesCache.Add(message); return(TryWriteMessage(message, MainMessagesFileName, FileMode.Append, out error)); }