C# web 链接数据库问题二三 (二)

1 篇文章 0 订阅
1 篇文章 0 订阅

在数据库链接后,后续页面未遇到较大困难,备份留用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace bank_management_system
{
    public partial class draw : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            drawValue.Focus();
        }

        protected void submitbtn_Click(object sender, EventArgs e)
        {
            string Temp;
            Temp = Request.Cookies["name"].Value.ToString();

            double  drawvalue = Convert.ToDouble(drawValue.Text);
            string Scon = @"Server=(local);uid=sa;Pwd=123456;DataBase=bankManagement";
            SqlConnection con = new SqlConnection(Scon);
            con.Open();
            string sqlstring = "update client set balance=balance-'" + drawvalue + "' where account='" + Temp + "'";

            string sqlcheck = "select balance from client where account='"+Temp+"'"; // 查询余额
            SqlCommand checkCmd = new SqlCommand(sqlcheck, con);
            
            SqlDataReader ds = checkCmd.ExecuteReader();
            ds.Read();
          
            string TempStr=ds["balance"].ToString();
            double i = Convert.ToDouble(TempStr);
          //  Response.Write("<script>alert('" + TempStr + "')</script>");
            //Response.Write("<script>alert('当前余额为"+i+"')</script>");
            ds.Close();
            if (drawvalue > i)//余额不足    
            {
                Response.Write("<script>alert('余额不足,请重新输入取款金额!')</script>");
                Response.Write("<script>location.href='draw.aspx';</script>");
            }
            else
            {
                SqlCommand cmd = new SqlCommand(sqlstring, con);
                cmd.ExecuteNonQuery();
                {
                    string sqldetails = "insert into details (account,Opamount,Toaccount,Mytime,operationName)values('" + Temp + "','" + drawvalue + "','',GETDATE(),'取出')";
                    SqlCommand conn = new SqlCommand(sqldetails, con);
                    conn.ExecuteNonQuery();
                }
                Response.Write("<script>alert('取款成功!')</script>");
                Response.Write("<script>location.href='content.aspx';</script>");
            }
            con.Close();
        }

        protected void returnbtn_Click(object sender, EventArgs e)
        {
            Response.Write("<script>location.href='content.aspx';</script>");
        }
    }
}
上述为取款
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace bank_management_system
{
    public partial class deposit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            despositValue.Focus();
        }

        protected void returnbtn_Click(object sender, EventArgs e)
        {
            Response.Write("<script>location.href='content.aspx';</script>");
        }

        protected void submitbtn_Click(object sender, EventArgs e)
        {
            string Temp;
            Temp = Request.Cookies["name"].Value.ToString();

            int depvalue = Convert.ToInt32(despositValue.Text);
            string Scon = @"Server=(local);uid=sa;Pwd=123456;DataBase=bankManagement";
            SqlConnection con = new SqlConnection(Scon);
            con.Open();
            string sqlstring = "update client set balance=balance+'" + depvalue + "' where account='" + Temp + "'";
            SqlCommand cmd = new SqlCommand(sqlstring, con);
            cmd.ExecuteNonQuery();
            {
                string sqldetails = "insert into details (account,Opamount,Toaccount,Mytime,operationName)values('"+Temp+"','"+depvalue+"','',GETDATE(),'存入')";
                SqlCommand conn = new SqlCommand(sqldetails, con);
                conn.ExecuteNonQuery();
            }
            Response.Write("<script>alert('存款成功!')</script>");
            Response.Write("<script>location.href='content.aspx';</script>");
            con.Close();
        }
    }
}

以上为取款。


虽然简单,但不失为入门参考,权当抛砖引玉。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值