public static void CopyDirectoryBooks(string sPath, string dPath, Category category) { DirectoryInfo sDir = new DirectoryInfo(sPath); FileInfo[] fileArray = sDir.GetFiles(); foreach (FileInfo file in fileArray) { try { file.CopyTo(dPath + "\\" + file.Name); AddaBook(file.Name, dPath, category); } catch (IOException) { //The destination file already exists, let user choose which one will be saved. string[] folder = dPath.Split('\\'); DialogResult dialogResult = MessageBox.Show(String.Format(Resources.CategoryManagement_DeleteTreeNode_fileAlreadyExistText, file.Name, folder[folder.Length - 1]), Resources.CategoryManagement_DeleteTreeNode_fileAlreadyExistTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (dialogResult == DialogResult.Yes) { file.CopyTo(dPath + "\\" + file.Name, true); AddaBook(file.Name, dPath, category); } } catch (Exception ex) { MessageBox.Show(String.Format(Resources.CategoryManagement_DeleteTreeNode_moveFileError, file, ex.Message), Resources.Error_Title, MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
public static bool DeleteCategory(Category category) { string sql = "delete from category where id = ?id"; MySqlParameter[] mysqlParameters = { new MySqlParameter("?id", category.Id) }; int result; try { result = MySqlHelper.ExecuteNonQuery(CommandType.Text, sql, mysqlParameters); } catch (Exception ex) { return false; } return result > 0; }
/// <summary> /// add a category then return the id /// </summary> /// <param name="category"></param> /// <returns>category id</returns> public static bool AddCategory(Category category) { string sql = "insert into category (id, parentid, categoryname) values (?id, ?parentid, ?category);"; MySqlParameter[] mysqlParameter = { new MySqlParameter("?id", category.Id), new MySqlParameter("?parentid", category.ParentId), new MySqlParameter("?category", category.CategoryName) }; int result; try { result = MySqlHelper.ExecuteNonQuery(CommandType.Text, sql, mysqlParameter); } catch (Exception ex) { return false; } return result > 0; }
private static void AddaBook(string fileName, string dPath, Category category) { Book book = new Book(); book.Id = Guid.NewGuid().ToString(); book.BookName = fileName; book.CategoryId = category.Id; book.Location = dPath + "\\" + fileName; book.URI = string.Format(URI, fileName); DBInteraction.AddOneBook(book); book.BookDetail.BookId = book.Id; }
public ItemControl[] GetSubCategories(Category parentCategory) { Category[] categories = DBInteraction.GetSubCategories(parentCategory); ItemControl[] categoryControls = new ItemControl[categories.Length + parentCategory.Books.Length]; for (int i = 0; i < categories.Length; i++) { ItemControl bookControl = new ItemControl(ShowType.Category); bookControl.Category = categories[i]; categoryControls[i] = bookControl; } for (int j = 0; j < parentCategory.Books.Length; j++) { ItemControl bookControl = new ItemControl(ShowType.Book); bookControl.Book = parentCategory.Books[j]; categoryControls[categories.Length + j] = bookControl; } return categoryControls; }
public int GetLevelSubCategoryNumber(Category category) { return Convert.ToInt32(DBInteraction.GetTotalSubCategoryNumbers(category)); }
public static bool UpdateCategory(Category category) { string sql = "update category set categoryname = ?categoryname where id = ?id"; MySqlParameter[] parameters = { new MySqlParameter("?categoryname", category.CategoryName), new MySqlParameter("?id", category.Id) }; int result; try { result = MySqlHelper.ExecuteNonQuery(CommandType.Text, sql, parameters); } catch (Exception ex) { MessageBox.Show("UpdateCategory error:" + ex); return false; } if (result > 0) { // update book location return UpdateBookLocation(category); } return false; }
public static bool UpdateBookLocation(Category category) { // this sql need joint string string sql = "update book set location = CONCAT(?location, bookname) where categoryid = ?categoryid;"; MySqlParameter[] parameter = { new MySqlParameter("?location", CategoryManagement.BookStoreFolder + "\\" + category.CategoryName + "\\"), new MySqlParameter("?categoryid", category.Id) }; int a; try { a = MySqlHelper.ExecuteNonQuery(CommandType.Text, sql, parameter); } catch (Exception ex) { return false; } return a > 0; }
public static bool IfHasCategory(Category category) { string sql = "select id from category where categoryname = ?categoryname"; MySqlParameter[] parameters = { new MySqlParameter("?categoryname", category.CategoryName) }; object result; try { result = MySqlHelper.ExecuteScalar(CommandType.Text, sql, parameters); } catch (Exception ex) { MessageBox.Show("IfHasCategory error: " + ex); return false; } category.Id = result as string ?? String.Empty; return true; }
public static object GetTotalSubCategoryNumbers(Category category) { string sql = "select count(*) as categorynumber from category where parentid = ?id"; MySqlParameter[] parameters = { new MySqlParameter("?id", category.Id) }; object result; try { result = MySqlHelper.ExecuteScalar(CommandType.Text, sql, parameters); } catch (Exception ex) { MessageBox.Show("GetTotalSubCategoryNumbers error: " + ex.Message); return 0; } return result ?? 0; }
public static Category[] GetSubCategories(Category parentCategory) { List<Category> categories = new List<Category>(); string sql = "SELECT id, parentid, categoryname FROM `category` where parentid = ?parentid order by categoryname desc;"; MySqlParameter[] parameters = { new MySqlParameter("?parentid", parentCategory.Id) }; try { using (MySqlDataReader msdr = MySqlHelper.ExecuteReader(CommandType.Text, sql, parameters)) { while (msdr.Read()) { Category category = new Category(); category.Id = msdr["id"].ToString(); category.ParentId = msdr["parentid"].ToString(); category.CategoryName = msdr["categoryname"].ToString(); GetBooksbyCategoryId(category); categories.Add(category); } } } catch (Exception ex) { MessageBox.Show("GetSubCategories error: " + ex.Message); } return categories.ToArray(); }
public static Category GetNodeCategory(string categoryName, string parentId) { Category category = new Category(); category.CategoryName = categoryName; IfHasCategory(category); if (String.IsNullOrEmpty(category.Id)) { category.Id = Guid.NewGuid().ToString(); category.ParentId = parentId; AddCategory(category); } return category; }
public static Category GetBookStoreRootCategory() { Category category = new Category(); string sql = "select id, categoryname from category where categoryname = 'BookStore'"; try { using (MySqlDataReader result = MySqlHelper.ExecuteReader(CommandType.Text, sql, null)) { while (result.Read()) { category.Id = result["id"].ToString(); category.CategoryName = result["categoryname"].ToString(); } } } catch (Exception ex) { MessageBox.Show("GetBookStoreCategory error: " + ex); return null; } return category; }
public static bool GetBooksbyCategoryId(Category category) { string sql = "select id, categoryid, bookname, location, picture, uri, readtimes from book where categoryid = ?categoryid order by bookname desc;"; MySqlParameter[] parameters = { new MySqlParameter("?categoryid", category.Id) }; try { using (MySqlDataReader msdr = MySqlHelper.ExecuteReader(CommandType.Text, sql, parameters)) { List<Book> categoryBooks = new List<Book>(); while (msdr.Read()) { Book book = new Book(); book.Id = msdr["id"].ToString(); book.CategoryId = category.Id; book.BookName = msdr["bookname"].ToString(); book.Location = msdr["location"].ToString(); book.Picutre = msdr["picture"].ToString(); book.URI = msdr["uri"].ToString(); book.ReadTimes = (int)msdr["readtimes"]; categoryBooks.Add(book); } category.Books = categoryBooks.ToArray(); } return true; } catch (Exception ex) { return false; } }
private void btnReturnCategory_Click(object sender, EventArgs e) { if (_categoryStack.Count > 0) { _currentCategory = _categoryStack.Pop(); showUI(true); } else { lblCategoryMessage.Text = "已到达跟目录!"; } }
private void MainForm_Load(object sender, EventArgs e) { lblPageMessage.Text = string.Empty; lblCategoryMessage.Text = string.Empty; _currentCategory = categoryManagement.GetCurrentRootCategory(); separatePageNum(); }