www.pudn.com > tsglxt.zip > bookBorrow.aspx.cs, change:2012-08-31,size:10098b


using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Data.SqlClient; 
 
public partial class bookBorrow : System.Web.UI.Page 
{ 
 
    protected void Page_Load(object sender, EventArgs e) 
    { 
         
        if (Session["entryType"] != "reader") 
        { 
            if (Session["userName"] == null)             //判断管理员是否登录 
            { 
              
    
                Response.Redirect("../entry.aspx");            //返回到登录页面 
            } 
        } 
        else 
        { 
            Response.Write("<script>alert('您没有此权限');location='../index.aspx';</script>"); 
        } 
 
    } 
 
    protected void btnReaderSearch_Click(object sender, EventArgs e) 
    { 
        bindReaderInfo();     //调用自定义方法显示读者信息 
    } 
    //自定义方法显示读者信息, 
    public void bindReaderInfo() 
    { 
        string readerBarCode = txtReaderBarCode.Text;               //获取读者条形码 
        //创建SQL语句在读者信息表中查询符合读者条形码条件的记录 
        string readerSql = "select * from tb_readerInfo where readerBarCode='" + readerBarCode + "'"; 
        SqlDataReader sdr = dataOperate.getRow(readerSql);          //获取该读者详细信息 
        if (sdr.Read()) 
        {                                                 //读取一条记录 
            txtReaderName.Text = sdr["readerName"].ToString();          //显示读者姓名 
            txtReaderSex.Text = sdr["Sex"].ToString();                  //显示读者性别 
            txtCertificateType.Text = sdr["certificateType"].ToString();//显示证件类型 
            txtCertificate.Text = sdr["certificate"].ToString();        //显示证件号 
            string money = sdr["money"].ToString(); 
            txtMoney.Text = money.Substring(0, money.Length - 2); 
            //创建SQL语句在读者类型表中查询符合读者类型编号的记录 
            string readerTypeSql = "select * from tb_readerType where id=" + sdr["readerType"].ToString(); 
            SqlDataReader typeSdr = dataOperate.getRow(readerTypeSql);  //获取读者类型信息 
            typeSdr.Read();                                             //读取一条记录 
            txtReaderType.Text = typeSdr["type"].ToString();            //显示读者类型 
            int borrowNum = Convert.ToInt32(typeSdr["num"]);            //获取可借图书总数 
            //创建SQL语句在图书借阅表中查询符合读者条形码条件的读者借了几本图书(图书未还的) 
            string selSql = "select count(*) from tb_bookBorrow where readerBarCode='" + readerBarCode + "' "; 
            int alreadyNum = dataOperate.seleSQL(selSql);               //获取图书已借数 
            txtNum.Text = Convert.ToString(borrowNum - alreadyNum);     //显示可以借阅数 
            btnBookSearch.Enabled = true; 
        } 
        else 
            RegisterStartupScript("", "<script>alert('读者条形码输入错误!')</script>"); 
    } 
    protected void btnBookSearch_Click(object sender, EventArgs e) 
    { 
 
        if (Convert.ToInt32(txtNum.Text.Trim()) > 0)                //判断读者是否还可以借书         
        { 
 
 
            if (txtBookBarCode.Text.Trim() != "")                   //判断图书条形码是否为空 
            { 
                string bookBarCode = txtBookBarCode.Text;           //获取图书条形码 
                //创建SQL语句在图书信息表中查询符合图书条形码条件的记录 
                string sql = "select * from tb_bookInfo where bookBarCode='" + bookBarCode + "'"; 
                DataSet ds = dataOperate.getDataset(sql); 
                 
                if (ds.Tables[0].Rows.Count > 0) 
                { 
                    gvBookBorrow.DataSource = ds;  //获取数据源 
                    gvBookBorrow.DataKeyNames = new string[] { "bookBarCode" }; //设置主键 
                    gvBookBorrow.DataBind();                            //绑定GridView控件 
                } 
                else 
                    RegisterStartupScript("", "<script>alert('图书条形码错误!')</script>"); 
            } 
            else 
            { 
                RegisterStartupScript("", "<script>alert('图书条形码不能为空')</script>"); 
            } 
        } 
        else 
        { 
            RegisterStartupScript("", "<script>alert('借阅数量已满!不可以再借阅')</script>"); 
        } 
 
 
 
    } 
    protected void gvBookBorrow_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
        if (e.Row.RowType == DataControlRowType.DataRow) 
        {   //设置图书类型 
            string bookType = e.Row.Cells[1].Text.ToString();               //获取图书类型编号 
            //创建SQL语句在图书类型表中查询符合图书类型编号条件的记录 
            string typeSql = "select * from tb_bookType where TypeID=" + bookType; 
            SqlDataReader typeSdr = dataOperate.getRow(typeSql); 
            typeSdr.Read();                                                 //读取一条记录 
            e.Row.Cells[1].Text = typeSdr["typeName"].ToString();           //显示图书类型 
            e.Row.Cells[5].Text = typeSdr["borrowDay"].ToString();          //显示图书可借天数 
            e.Row.Cells[6].Text = typeSdr["hire"].ToString() + "元 / 期"; 
            e.Row.Cells[7].Text = typeSdr["lagMoney"].ToString() + "元 / 天"; 
             
        } 
    } 
    protected void gvBookBorrow_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) 
    { 
 
        //获取选中图书条形码 
        string bookBarCode = gvBookBorrow.DataKeys[e.NewSelectedIndex].Value.ToString(); 
        //创建SQL语句使用内联接连接条件为图书类型编号,查询条件为符合图书条形码的记录 
        string sql = "select * from tb_bookInfo as a inner join tb_bookType as b on a.bookType=typeID where a.bookBarCode='" + bookBarCode + "'"; 
        SqlDataReader sdr = dataOperate.getRow(sql);                                    //获取图书信息 
        //读取一条记录 
        sdr.Read(); 
        //判断图书是否还有库存 
        if (Convert.ToInt32(sdr["stock"]) > 0) 
        { 
            float  readerMoney =Convert.ToSingle( txtMoney.Text); 
            float bookHire = Convert.ToSingle(sdr["hire"].ToString()); 
            if (readerMoney>bookHire) 
            { 
                string sqlBookBorrow = "select count(*) from tb_bookBorrow where  bookBarcode='" + txtBookBarCode.Text + "' and readerBarCode='" + txtReaderBarCode.Text + "' "; 
                if (dataOperate.seleSQL(sqlBookBorrow) == 0) 
                { 
                    int borrowDay = Convert.ToInt32(sdr["borrowDay"]);                              //获取借阅天数 
                    string bookName = sdr["bookName"].ToString();                                   //获取图书名称 
                    string borrowTime = DateTime.Now.Date.ToShortDateString();                     //获取借阅日期 
                    string readerBarCode = txtReaderBarCode.Text;                                   //获取读者条形码 
                    string returnTime = DateTime.Now.Date.AddDays(borrowDay).ToShortDateString();   //获取应还日期 
                    string readerName = txtReaderName.Text;                                         //获取读者姓名 
 
 
 
                    //设置SQL语句数组。 
                    string[] sqlT = new string[3]; 
                    //设置SQL语句,将图书借阅信息插入到图书借阅表中 
                    sqlT[0] = "insert tb_bookBorrow values('" + bookBarCode + "','" + bookName + "','" + borrowTime + "','" + returnTime + "','" + readerBarCode + "','" + readerName + "')"; 
                    //设置SQL语句,更新图书的借阅次数和图书的库存信息 
                    sqlT[1] = "update tb_bookInfo set borrowSum=borrowSum+1,stock=stock-1 where bookBarCode='" + bookBarCode + "'"; 
                    //设置SQL语句,更新读者的金额 
                    sqlT[2] = "update tb_readerInfo set money=money-" + bookHire + " where readerBarCode='" + readerBarCode + "'"; 
                    //设置SQL语句, 
                    //sqlT[3] = "update tb_bookInfo  where bookBarCode='" + bookBarCode + "'"; 
 
                    //调用公共类中的execTransaction方法执行事务 
                    if (dataOperate.execTransaction(sqlT)) 
                    {                         
                        bindReaderInfo();               //重新绑定读者信息 
                        gvBookBorrow.DataSource = null; //将数据源设置为空 
                        gvBookBorrow.DataBind(); 
                        txtBookBarCode.Text = "";       //将图书条形码文本框清空 
                        RegisterStartupScript("", "<script>alert('借阅成功!')</script>"); 
                    } 
                    else 
                    { 
                        RegisterStartupScript("", "<script>alert('借阅失败!')</script>"); 
                    } 
                } 
                else 
                { 
                    RegisterStartupScript("", "<script>alert('该读者已经借阅此图书不可以在借阅!')</script>"); 
                } 
                
            } 
            else 
            { 
                RegisterStartupScript("", "<script>alert('读者金额不足不可以借阅图书!')</script>"); 
            } 
 
        } 
        else 
        { 
            RegisterStartupScript("", "<script>alert('图书已没有库存不可以借阅!')</script>"); 
        } 
 
 
    } 
}