C# web 鏈接數據庫問題二三 (二)

在數據庫鏈接後,後續頁面未遇到較大困難,備份留用。

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();
        }
    }
}

以上爲取款。


雖然簡單,但不失爲入門參考,權當拋磚引玉。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章