BookBusiness.cs
上传用户:gooyliu
上传日期:2018-09-29
资源大小:5816k
文件大小:3k
源码类别:

.net编程

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Configuration;
  7. namespace DataAccessLayer
  8. {
  9.     public class BookBusiness
  10.     {
  11.         DataBase db = new DataBase();
  12.         /// <summary>
  13.         /// 判断是否存在给出的ID的书
  14.         /// </summary>
  15.         /// <param name="bookID"></param>
  16.         /// <returns>返回0表示不存在该书,返回-1表示该书当前不可借阅,返回1表示该书当前可借阅</returns>   
  17.         public int CheckBookByID(string bookID)
  18.         {
  19.             string cmdStr = "SELECT * FROM Book WHERE bookID='" + bookID + "'";
  20.             string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();
  21.             SqlConnection conn = new SqlConnection(connStr);
  22.             SqlCommand cmd = new SqlCommand(cmdStr, conn);
  23.             SqlDataAdapter ada = new SqlDataAdapter(cmd);
  24.             DataSet ds = new DataSet();
  25.             ada.Fill(ds);
  26.             if (ds.Tables[0].Rows.Count == 0)
  27.                 return 0;
  28.             else if (Convert.ToInt16(ds.Tables[0].Rows[0]["status"]) != 1)
  29.                 return -1;
  30.             else return 1;
  31.         }
  32.         /// <summary>
  33.         /// 
  34.         /// </summary>
  35.         /// <param name="readerID"></param>
  36.         /// <param name="bookID"></param>
  37.         /// <returns>1:借阅成功  0:达到最大借阅册数5本</returns>
  38.         public static int LendBook(string readerID, string bookID)
  39.        {
  40.            DataBase db = new DataBase();
  41.            string getBorrowCount = "SELECT COUNT(*) FROM BorrowRec WHERE readerID='" + readerID + "' AND borrowStatus='True'";
  42.            int borrowCount=db.GetRecordCount(getBorrowCount);
  43.            if (borrowCount == 5)
  44.                return 0;
  45.            else
  46.            {
  47.                DateTime beginDate = DateTime.Now;
  48.                string status = "true";
  49.                string insertStr = "INSERT INTO BorrowRec(readerID,bookID,beginDate,borrowStatus)" + "  VALUES('" + readerID + "','" + bookID + "','" + beginDate + "','" + status + "')";
  50.                string upBookStatus = "UPDATE Book  SET status=0 WHERE bookID='" + bookID + "'";
  51.                int val = db.ExecuteNonQuery(insertStr);
  52.                if (val == 1)
  53.                {
  54.                    db.ExecuteNonQuery(upBookStatus);
  55.                }
  56.                return val;
  57.            } 
  58.         }
  59.         public int ReturnBook(string bookID)
  60.         {
  61.             string endDate = DateTime.Now.ToString ();
  62.             string cmdText = "update Book set status=1 where bookID='"+bookID+"';Update borrowRec SET borrowStatus='False', endDate='"+endDate +"'  WHERE  borrowRecID IN(SELECT borrowRecID FROM borrowRec WHERE bookID='"+@bookID+"'  AND borrowStatus='True')";
  63.            // SqlParameter param = new SqlParameter("@bookID", SqlDbType.NVarChar );
  64.            // param.Value = bookID;
  65.             int result = db.ExecuteNonQuery(cmdText);
  66.             return result;
  67.         }
  68.     }
  69. }