Esempio n. 1
0
        /// <summary>
        /// Обновление блокнота
        /// </summary>
        /// <param name="structureNotepad">Модель блокнота</param>
        /// <returns></returns>
        public static bool UpdateNotepad(StructureNotepad structureNotepad)
        {
            using (var connection = new SqlConnection(ConnectionString))
            {
                connection.Open();

                using (var cmd = connection.CreateCommand())
                {
                    cmd.CommandType = CommandType.Text;

                    cmd.CommandText = "UPDATE [Data] " +
                                      "SET " +
                                      "content = @content " +
                                      "WHERE Id = @Id;";

                    cmd.Parameters.AddWithValue("@content", structureNotepad.Content);
                    cmd.Parameters.AddWithValue("@Id", structureNotepad.Id);


                    try
                    {
                        cmd.ExecuteNonQuery();     // синхронно
                        return(true);
                    }
                    catch (Exception e)
                    {
                        return(false);
                        // TODO сделать лог
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Создать новый блокнот
        /// </summary>
        /// <param name="name">Название блокнота</param>
        /// <returns></returns>
        public static bool AddNotepad(string name)
        {
            if (Database.LoadNotepads().Any(notepad => notepad.Name == name))
            {
                return(false);
            }

            StructureNotepad sn = new StructureNotepad();

            sn.Name = name;

            return(Database.AddNotepad(name));
        }
Esempio n. 3
0
        /// <summary>
        /// Получить данные о всех блокнотах
        /// </summary>
        /// <returns></returns>
        public static List <StructureNotepad> LoadNotepads()
        {
            using (var connection = new SqlConnection(ConnectionString))
            {
                connection.Open();

                using (var cmd = connection.CreateCommand())
                {
                    cmd.CommandType = CommandType.Text;

                    cmd.CommandText = "SELECT * " +
                                      "FROM [Data] ";

                    using (var reader = cmd.ExecuteReader())
                    {
                        List <StructureNotepad> structureNotepads = new List <StructureNotepad>();

                        while (reader.Read())
                        {
                            int id;

                            try
                            {
                                id = Convert.ToInt32(reader["Id"]);
                            }
                            catch (Exception)
                            {
                                id = 0;
                            }

                            StructureNotepad structure = new StructureNotepad();

                            structure.Id      = id;
                            structure.Name    = reader["name"].ToString();
                            structure.Content = reader["content"].ToString();

                            structureNotepads.Add(structure);
                        }

                        return(structureNotepads);
                    }
                }
            }
            return(null);
        }