Esempio n. 1
0
        public void AddCategory(ToDoCategory category)
        {
            SqlConnection connection = new SqlConnection(_connectionString);
            SqlCommand    command    = connection.CreateCommand();

            command.CommandText = "INSERT INTO Categories VALUES (@name); SELECT @@Identity";
            command.Parameters.AddWithValue("@name", category.Name);
            connection.Open();
            category.Id = (int)(decimal)command.ExecuteScalar();
        }
Esempio n. 2
0
        public void UpdateCategory(ToDoCategory category)
        {
            SqlConnection connection = new SqlConnection(_connectionString);
            SqlCommand    command    = connection.CreateCommand();

            command.CommandText = "UPDATE Categories SET Name = @name WHERE Id = @id";
            command.Parameters.AddWithValue("@name", category.Name);
            command.Parameters.AddWithValue("@id", category.Id);
            connection.Open();
            command.ExecuteNonQuery();
        }
Esempio n. 3
0
        public IEnumerable <ToDoCategory> GetCategories()
        {
            SqlConnection connection = new SqlConnection(_connectionString);
            SqlCommand    command    = connection.CreateCommand();

            command.CommandText = "SELECT * FROM Categories";
            connection.Open();
            List <ToDoCategory> categories = new List <ToDoCategory>();
            SqlDataReader       reader     = command.ExecuteReader();

            while (reader.Read())
            {
                ToDoCategory c = new ToDoCategory
                {
                    Id   = (int)reader["Id"],
                    Name = (string)reader["Name"]
                };
                categories.Add(c);
            }

            return(categories);
        }