最近在做一个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("<script>location.href='content.aspx';</script>");
//Response.Write("<script>alert('ss')</script>");
}
}
}
Response.Write("<script>alert('密码错误!')</script>");
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("<script>alert('Yeah!')</script>");
}*/
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("<script>alert('Yeah!')</script>");
Label1.Text = ds["balance"].ToString();
//Label1.Text = ds.GetString(0);
}
con.Close();
第二个页面的实现
后续的存取款、转账还未完成,留待下次记录。