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

最近在做一个C# webform银行管理项目,由于刚接触,对其中数据库的了解还不太熟悉,特在此记录。


首先要加入这两个命名空间

using System.Data;
using System.Data.SqlClient;

在登录界面查了好多资料才完成,网上的大部分方法都不能实现,不知是什么原因,留待以后处理;

  string name = UserName.Text.Trim();
  string word = UserWord.Text.Trim();
  string Scon = @"Server=(local);uid=sa;Pwd=123456;DataBase=bankManagement";
  SqlConnection con = new SqlConnection(Scon);
  string sqlcheck = "select account from client";
  con.Open();
  SqlCommand check = new SqlCommand(sqlcheck, con);
  string sqlstring1 = "select* from client";
  SqlCommand cmd = new SqlCommand(sqlstring1, con);
  SqlDataReader sqlRead = cmd.ExecuteReader();
  while (sqlRead.Read())
                {
                    if (UserName.Text == sqlRead["account"].ToString())
                    {
                        if (UserWord.Text == sqlRead["password"].ToString())
                        {
                            Response.Write("");
                            //Response.Write("");
                        }
                    }
                }
  Response.Write("");
  UserWord.Focus();
  con.Close();

然后用cookie传值的方式实现了两个页面的传值,正确地在第二个页面显示余额项

            HttpCookie cookie_name = new HttpCookie("name");
            cookie_name.Value =UserName.Text;
            Response.AppendCookie(cookie_name);
            Server.Transfer("content.aspx");
第一个页面的代码

            string Temp;
            Temp = Request.Cookies["name"].Value.ToString();
         /*   if (Temp == "admin")
            {
                Response.Write("");
            }*/
            string Scon = @"Server=(local);uid=sa;Pwd=123456;DataBase=bankManagement";
            SqlConnection con = new SqlConnection(Scon);
            con.Open();
            string sqlstring = "select balance from client where account='"+Temp+"'";
            SqlCommand cmd = new SqlCommand(sqlstring, con);
            SqlDataReader ds = cmd.ExecuteReader();
            while (ds.Read())
            {
               // Response.Write("");
                Label1.Text = ds["balance"].ToString();
                //Label1.Text = ds.GetString(0);
            }
            con.Close();
第二个页面的实现


后续的存取款、转账还未完成,留待下次记录。

你可能感兴趣的:(C#,数据库,webform)