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


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 bookRenewal : 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>"); 
        } 
 
    } 
 
    /// <summary> 
    /// 查询读者信息的按钮事件,查询读者的详细信息并显示在页面中 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    protected void btnReaderSearch_Click(object sender, EventArgs e) 
    { 
        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 readerMoney = sdr["money"].ToString(); 
            txtMoney.Text = readerMoney.Substring(0, readerMoney.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);         //显示可以借阅数 
            //创建SQL语句在图书借阅表中查询符合读者条形码条件未归还图书的记录 
            string sql = "select * from tb_bookBorrow as a join view_bookInfo as b on a.bookBarCode=b.bookBarCode where  a.readerBarCode='" + txtReaderBarCode.Text + "'"; 
            bindGridView(sql);                                              //调用自定义方法显示已借阅未归还图书信息 
        } 
        else 
            RegisterStartupScript("", "<script>alert('读者条形码输入错误!')</script>"); 
    } 
 
    public void bindGridView(string sql) 
    { 
 
        gvBookRenewal.DataSource = dataOperate.getRow(sql);              //获取数据源 
        gvBookRenewal.DataKeyNames = new string[] { "bookBarCode" };    //设置主键 
        gvBookRenewal.DataBind();                                       //绑定GridView控件 
 
    } 
    protected void btnBookSearch_Click(object sender, EventArgs e) 
    { 
        if (txtBookBarCode.Text.Trim() != "")                           //判断图书条形码是否为空 
        { 
            string bookBarCode = txtBookBarCode.Text;                   //获取图书条形码 
            //创建SQL语句在图书借阅表中查询符合读者条形码条件和图书条形码条件未归还图书的记录 
            string sqlBorrow = "select count(*) from tb_bookBorrow where bookBarCode='" + bookBarCode + "'and readerBarCode='" + txtReaderBarCode.Text + "'"; 
            if (dataOperate.seleSQL(sqlBorrow) > 0) 
            { 
                string sql = "select * from tb_bookBorrow as a join view_bookInfo as b on a.bookBarCode=b.bookBarCode where  a.readerBarCode='" + txtReaderBarCode.Text + "'and a.bookBarCode='" + bookBarCode + "'"; 
                bindGridView(sql);                                          //调用自定义方法显示已借阅未归还图书信息                         
            } 
            else 
            { 
                RegisterStartupScript("", "<script>alert('该图书没有借阅过,请到借阅功能中操作!')</script>"); 
                gvBookRenewal.DataBind(); 
            } 
 
        } 
        else 
        { 
            RegisterStartupScript("", "<script>alert('图书条形码不能为空')</script>"); 
        } 
    } 
 
 
    protected void gvBookRenewal_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) 
    { 
        //获取当前图书条形码 
        string bookBarCode = gvBookRenewal.DataKeys[e.NewSelectedIndex].Value.ToString(); 
        DateTime returnDate = Convert.ToDateTime(gvBookRenewal.Rows[e.NewSelectedIndex].Cells[4].Text); 
        DateTime todayDate = DateTime.Now.Date; 
        TimeSpan ts = todayDate - returnDate; 
        int daysDate = ts.Days; 
        string strLagMoney = gvBookRenewal.Rows[e.NewSelectedIndex].Cells[7].Text; 
        int lagMoney = Convert.ToInt32(strLagMoney.Substring(0, strLagMoney.Length - 5)); 
        string strHire = gvBookRenewal.Rows[e.NewSelectedIndex].Cells[6].Text; 
        int hire = Convert.ToInt32(strHire.Substring(0, strHire.Length - 5)); 
        int borrowDay = Convert.ToInt32(gvBookRenewal.Rows[e.NewSelectedIndex].Cells[5].Text); 
        string hint = ""; 
        DateTime isHalf = returnDate.AddDays(-borrowDay / 2); 
 
        if (isHalf < todayDate) 
        { 
            string[] sqlT = new string[3]; 
 
            if (daysDate > 0) 
            { 
                sqlT[0] = "update tb_readerInfo set money=money- " + hire + "- " + lagMoney * daysDate + " where readerBarCode = '" + txtReaderBarCode.Text + "' "; 
                hint = "您的图书归还期已过" + daysDate + "天,将抠除" + lagMoney * daysDate + "元金额"; 
            } 
            else 
            { 
                sqlT[0] = "update tb_readerInfo set money=money- " + hire + " where readerBarCode = '" + txtReaderBarCode.Text + "' "; 
            } 
 
            sqlT[1] = "update tb_bookInfo set borrowSum=borrowSum+1 where bookBarCode='" + bookBarCode + "'"; 
            sqlT[2] = "update tb_bookBorrow set borrowTime='" + todayDate + "', returnTime='" + todayDate.AddDays(borrowDay) + "' where readerBarCode='" + txtReaderBarCode.Text + "' and bookBarCode='" + bookBarCode + "' "; 
 
            if (dataOperate.execTransaction(sqlT)) 
            { 
                string readerSql = "select * from tb_readerInfo where readerBarCode='" + txtReaderBarCode.Text + "'"; 
                SqlDataReader sdr = dataOperate.getRow(readerSql); 
                sdr.Read(); 
                string readerMoney = sdr["money"].ToString(); 
                txtMoney.Text = readerMoney.Substring(0, readerMoney.Length - 2);   //显示余额 
                string sql = "select * from tb_bookBorrow as a join view_bookInfo as b on a.bookBarCode=b.bookBarCode where  a.readerBarCode='" + txtReaderBarCode.Text + "'"; 
                bindGridView(sql);                                              //调用自定义方法显示已借阅未归还图书信息 
                RegisterStartupScript("", "<script>alert('续借成功!" + hint + "')</script>"); 
            } 
            else 
            { 
                RegisterStartupScript("", "<script>alert('续借失败!')</script>"); 
            } 
 
        } 
        else 
        { 
            RegisterStartupScript("", "<script>alert('借阅天数未过半不可以续借!')</script>"); 
        } 
 
 
 
 
 
 
 
    } 
}