当前位置:编程学习 > C#/ASP.NET >>

一个图书管理系统我怎么样才能记录次数有没有被借完?

我正在做一个图书管理系统,对图书管理的时候怎么用代码实现这本书有没有被借完??
我希望能又高手进来给我代码 --------------------编程问答-------------------- 我会这样思考.
book 类,
书名.数量,
每借去一本书.
将数量减1 .

熟悉触发器么? 触发器可帮助实现复杂业务逻辑. 
例如 书有没有被借完. 
class Book
{
  string 书名;
  int 数量;
  Book()
  {
    数量= 从数据库查找数量();
  }
}
class Student
{
  string 借书证;
  public int 借书(string 借书证)
  {
    Book book = new Book();
    if(book.数量>0)
       book.数量 -= 1;
    return book.数量;
  }
} --------------------编程问答-------------------- 创建Book 类的对象时,将会访问数据库,并得到书的数量.

然后如果还有书,就借出去一本, 并将数据库中书的数量减去1 --------------------编程问答-------------------- 要存书的数量啊,借走一本就减一 --------------------编程问答-------------------- 下面的代码中也许有你想要的东西,但是我就不说明它了,很早的东西了,忘的差不多了,这是从一个图书馆管理系统中拿出来的,你可以放心用,咱有版权。

        private void BorrowBook_Click(object sender, EventArgs e)
        {
           
            string Sql1, Sql2,Sql = "begin tran;insert into BRinf (Readerid,readername,bookid,bookname,borrowtime,"
                +"limitReturntime,Returntime,overtime,Spend,Money,id,isReturn,operater) values({0})";
            DataSet ds;
            if (Value.Borrowreturn == "续借")
            {
                Sql = "update brinf set limitReturntime = '"
                    + (DateTime.Now.AddMonths(2)).ToString().Substring(0, DateTime.Now.ToString().IndexOf(" ")-1)
                    + "' where Readerid = '" + B_ReaderID.Text.Trim() + "' and Bookid = '"
                    + B_BookID.Text.Trim() + "';";
                if (Operator.ExecuteSql(Sql) <= 0)
                {
                    MessageBox.Show("续借失败!", "读者续借");
                
                }
                BookInfoMag_Load(sender, e);
                return;
            }
            if (B_BookID.Text.Trim() != "" && B_ReaderID.Text.Trim() != "")
            {
                int i;
                string msg = "用户已借图书:";
                Sql1 = "select bookid,bookname from brinf where Readerid = '" + B_ReaderID.Text.Trim() + "'and isreturn ='否';";
                ds = Operator.SelectSql(Sql1);
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        msg += "1.图书编号:"+ds.Tables[0].Rows[i].ItemArray[0].ToString().Trim()+"\t";
                        msg+="书名:"+ds.Tables[0].Rows[i].ItemArray[1].ToString().Trim()+"\n";
                    }
                    msg += "按确定继续,按消取中止操作!";
                    DialogResult dlr = MessageBox.Show(msg,"读者借阅",MessageBoxButtons.OKCancel);
                    if (dlr == DialogResult.Cancel)
                    {
                        return;
                    }
                   
                }
                string Rid, Rname, Bid, Bname, Btime, LRtime, Rtime, Otime, BRSpeed, Money,Pid,id, IsR;
                Sql1 = "select * from Reader where readerid = '" + B_ReaderID.Text.Trim() + "';";
                ds = Operator.SelectSql(Sql1);

                Rid = B_ReaderID.Text.Trim();
                Bid = B_BookID.Text.Trim();
                Rname = ds.Tables[0].Rows[0].ItemArray[1].ToString().Trim();
                IsR = "否";

                ds = new DataSet();
                Sql1 = "select bookname,Published from bookinf where bookid = '" + B_BookID.Text.Trim() + "';";
                ds = Operator.SelectSql(Sql1);
                
                Bname = ds.Tables[0].Rows[0].ItemArray[0].ToString().Trim();
                Pid = ds.Tables[0].Rows[0].ItemArray[1].ToString().Trim();
                Btime = "getdate()";
                LRtime = Return_Time.Value.ToString().Substring(0, Return_Time.Value.ToString().IndexOf(" "));
                Rtime = "getdate()";
                Otime = "0";
                BRSpeed = Speed.Text.Trim();
                if (BRSpeed.Length == 0)
                {
                    BRSpeed = "0";
                }
                if (B_BookID.Text.Trim() == "")
                {
                    return;
                }
                Sql1 = "select Bookstate from bookinf where bookid = '" + B_BookID.Text.Trim() + "';";
                ds = Operator.SelectSql(Sql1);
                if (!(ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0].ItemArray[0].ToString().Trim() == "在库"))
                {
                    MessageBox.Show("输入的图书编号有误或图书已借出,请仔细核对!", "读者借阅");
                    B_BookID.Focus();
                    B_BookID.Text = "";
                    return;
                }
                Money = "0";
                id = Operator.select("select count(*) from brinf").ToString().Trim()+1;
               
                Sql2 = "'" + Rid + "','" + Rname + "','" + Bid + "','" + Bname + "'," + Btime + ",'" 
                    + LRtime + "'," + Rtime + ","+ Otime + "," + BRSpeed + "," + Money + "," + id 
                    + ",'" + IsR + "','" + Value.CurrentLoger1 + "'";
               
                Sql = string.Format(Sql,Sql2);
                Sql += "update booktable set bookstocks = bookstocks - 1 where Publishid = "+ Pid +";";
                Sql += "update bookinf set Bookstate = '借出' where bookid = '"+B_BookID.Text.Trim()+"';commit tran;";
                
                if (Operator.ExecuteSql(Sql) <= 0)
                {
                    MessageBox.Show("借阅失败!", "读者借阅");
                }
            }
            else
            {
                MessageBox.Show("请输入完整的阅信息!", "读者借阅");
                return;
            }
            BookInfoMag_Load(sender, e);
        }

           }
} --------------------编程问答-------------------- 数据库里两个字段

一个是该本的总量,另一个是库存量或者借出量 --------------------编程问答-------------------- 我会这样设计  到数据库里面设置一个字段 如:bookNum(该本书共有多少本)
当读者借一本就相应的到该字段减少一本  当bookNub=0时该书借完   只是建议  呵呵

代码写起来比较的简单
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,