Esempio n. 1
0
        public ActionResult QueryBook([FromBody] BookQuery bookQuery)
        {
            if (!BookChecker.existBook(bookQuery))
            {
                return(BadRequest(new { message = "没有此书" }));
            }
            String titledBookName = StringChecker.AddBookTitle(bookQuery.Name);

            using (MySqlConnection conn = SqlProvider.GetMySqlConnection())
            {
                MySqlCommand cmd = new MySqlCommand($"SELECT * from books WHERE name='{titledBookName}'OR GUID='{bookQuery.guid}';", conn);

                MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
                //创建DataSet类的对象
                DataSet dataset = new DataSet();

                //使用SQLDataAdapter对象sda将查询结果填充到DataTable对象ds中
                adapter.Fill(dataset);

                MySqlCommandBuilder cmdBuilder = new MySqlCommandBuilder(adapter);

                DataRow dataRow = dataset.Tables[0].Rows[0];

                Book book = new Book()
                {
                    GUID         = Guid.Parse(dataRow["GUID"].ToString()),
                    Name         = dataRow["Name"].ToString(),
                    TotalNumber  = (int)dataRow["TotalNumber"],
                    RemainNumber = (int)dataRow["RemainNumber"]
                };
                return(Ok(book));
            }
        }
Esempio n. 2
0
        public ActionResult Post([FromBody] Book book)
        {
            var    titledBookName = StringChecker.AddBookTitle(book.Name);
            String guid           = null;

            if (BookChecker.existBook(titledBookName))
            {
                return(BadRequest(new { message = "已有重复书籍" }));
            }
            using (MySqlConnection conn = SqlProvider.GetMySqlConnection())
            {
                MySqlCommand cmd = new MySqlCommand("SELECT * from books;", conn);
                //int row = cmd.ExecuteNonQuery();
                //if(row==0)
                //{
                //    return BadRequest(new { message="插入失败"});
                //}
                MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
                //创建DataSet类的对象
                DataSet dataset = new DataSet();

                //使用SQLDataAdapter对象sda将查询结果填充到DataTable对象ds中
                adapter.Fill(dataset);

                //创建SqlCommandBuilder类的对象
                MySqlCommandBuilder cmdBuilder = new MySqlCommandBuilder(adapter);


                DataTable dataTable = dataset.Tables[0];

                DataRow dataRow = dataTable.NewRow();
                guid            = Guid.NewGuid().ToString();
                dataRow["GUID"] = guid;
                dataRow["Name"] = titledBookName;

                dataRow["TotalNumber"]  = book.TotalNumber;
                dataRow["RemainNumber"] = book.RemainNumber;
                //向DataTable对象中添加一行
                dataset.Tables[0].Rows.Add(dataRow);
                //更新数据库
                if (adapter.Update(dataset) == 0)
                {
                    conn.Close();
                    return(BadRequest(new { message = "插入失败" }));
                }
            }
            return(Ok(new { message = "插入成功", guid = guid }));
        }
Esempio n. 3
0
        public ActionResult deleteBook(BookQuery bookQuery)
        {
            if (!BookChecker.existBook(bookQuery))
            {
                return(BadRequest(new { message = "没有此书" }));
            }
            String titledBookName = StringChecker.AddBookTitle(bookQuery.Name);

            using (MySqlConnection conn = SqlProvider.GetMySqlConnection())
            {
                conn.Open();
                MySqlCommand cmd       = new MySqlCommand($"DELETE  from books WHERE name='{titledBookName}'OR GUID='{bookQuery.guid}';", conn);
                var          rowsCount = cmd.ExecuteNonQuery();
                if (rowsCount != 0)
                {
                    conn.Close();
                    return(Ok(new { message = "删除成功" }));
                }
                conn.Close();
                return(BadRequest(new { message = "删除失败" }));
            }
        }