您的位置:首页 > 编程语言 > C#

.net C#实现登录界面并进行跳转

2017-05-30 20:49 423 查看
登录界面:
Userloagin.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Userloagin.aspx.cs" Inherits="UserLogin.Userloagin" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center;margin-bottom: 40px;">

登录画面<br />
<br />
用户ID:<asp:TextBox ID="id" runat="server" Height="20px" Width="180px"></asp:TextBox>
<br />
<br />
密   码:<asp:TextBox ID="password" runat="server" Height="20px" Width="180px"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button" runat="server" Height="24px" OnClick="Button_Click" Text="登录" Width="75px" />
<br />
<br />
<asp:Label ID="Label" runat="server" ForeColor="Red"></asp:Label>
<br />

</div>
</form>
</body>
</html>
Userloagin.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//添加以下参照
using System.Windows.Forms;
//using Oracle.DataAccess.Client;//oracle自带的
using System.Data.OracleClient;
using System.Data.SqlClient;//using Oracle.DataAccess.Client;
using System.Data;//注意

namespace UserLogin
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void Button_Click(object sender, EventArgs e)
{
if (id.Text == "")
{
//MessageBox.Show("用户ID必须存在入内!");//using System.Windows.Forms;
Response.Write(@"<script>alert('用户名不能为空!');</script>");
}
if (password.Text == "")
{
//MessageBox.Show("密码不能为空!");
Response.Write(@"<script>alert('用户密码不能为空!');</script>");
}
//连接Oracle
string connString = "User ID=system;Password=Lg_12358134711;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 10.167.23.31)(PORT = 1521))) (CONNECT_DATA = (SID = oracletest)))";
OracleConnection conn = new OracleConnection(connString);
conn.Open();

//方法一
//DataSet 是用来存储数据库数据的。数据库 DataSet,表 DataTable,列 DataColumn,行 DataRow
//DataSet 和 物理数据库是两个客体,要使这两个客体保持一致,就需要使用 DataAdapter 类来同步两个客体。
//一般来说操作流程是:使用 DataAdapter 把物理数据库中的数据填充到 DataSet 中的 DataTable 里。当我们对内存中的 DataSet 做了增、删、改操作后,可以使用 DataAdapter 同步回数据库。
//http://blog.163.com/jianzhong_w/blog/static/3833113620077245104435/

//创建DataSet  SqlDataAdapter对象
//string strSql = "select * from USER_MST";
//OracleDataAdapter odr = new OracleDataAdapter(strSql,conn);
//try
//{
//    DataSet ds = new DataSet();
//    odr.Fill(ds, "USER_MST");
//    for (int i = 0; i < ds.Tables["USER_MST"].Rows.Count; i++)
//    {
//        string User = id.Text;
//        string Psw = id.Text.Trim();
//        string db_Uid = ds.Tables["USER_MST"].Rows[i]["USERID"].ToString();
//        string db_Psw = ds.Tables["USER_MST"].Rows[i]["PASSWORD"].ToString();
//        string username = ds.Tables["USER_MST"].Rows[i]["USERNAME"].ToString();
//        if (id.Text == db_Uid && password.Text == db_Psw)
//        {
//            Label.Text = "登陆成功";
//            Session["username"] = username;
//            Session["userid"] = db_Uid;
//            Server.Transfer("menu.aspx");
//        }
//    }
//    Label.Text = "登陆失败!用户ID或密码错误!";
//}
//catch (OracleException ex)
//{
//    Console.WriteLine(ex.Message);
//}
//finally
//{
//    conn.Close();
//}

//string strOrcSelect = "select USERNAME from USER_MST where USERID=@USERID and PASSWORD=@PASSWORD";
//OracleCommand comm = new OracleCommand(strOrcSelect, conn);
//comm.Parameters.Add("USERID", id.Text);
//comm.Parameters.Add("PASSWORD", password.Text);
//OracleDataReader sdr = comm.ExecuteReader();
//while (sdr.Read())
//{
//    Label.Text = "登陆成功";
//    Session["USERID"] = id.Text;
//    Session["PASSWORD"] = password.Text;
//    Response.Redirect("menu");
//}
// Label.Text = "登陆失败!用户ID或密码错误!";

try
{
string userid = id.Text;
string psd = password.Text;

//Oracle
//string strOrcSelect = "select * from USER_MST where USERID=" + userid + " and PASSWORD=" + psd;//select * from USER_MST where USERID= 12 and PASSWORD = 123
//string strOrcSelect = "select * from USER_MST where USERID='userid' and PASSWORD='psd'";//select * from USER_MST where USERID='userid' and PASSWORD='psd';
//上述表示错误,应为下面的两种形式
//形式一:
//string strOrcSelect = "select * from [USER_MST] where id='" + userid + "' and password='" + psd + "'";
//形式二:
//string strOrcSelect = string.Format("select count(*) from USER_MST where USERID= '{0}'and PASSWORD = '{1}'", this.id.Text.Trim(), this.password.Text.Trim());

//string strOrcSelect = string.Format("select count(*) from USER_MST where USERID= '{0}'and PASSWORD = '{1}'", this.id.Text.Trim(), this.password.Text.Trim());
//OracleCommand comm = new OracleCommand(strOrcSelect, conn);
//if (comm.ExecuteScalar() == null)//可能会失败,需要进行判断
//{
//    Label.Text = "登陆失败!用户ID或密码错误!";
//}
//else
//{
//    int i = int.Parse(comm.ExecuteOracleScalar().ToString());
//}

//ExecuteScalar():从数据库中检索单个值。执行查询,并返回查询所返回的结果集中第一行的第一列或空引用(如果结果集为空).忽略其他列或行.
//ExecuteScalar()所求的数据集为空或不存在两种情况:
//object obj = comm.ExecuteScalar();
//情况一:if (obj == System.DBNull.Value) { MessageBox.Show("Result Is Null 数据集的行数为一");}
//情况二:if (obj == null )  {MessageBox.Show("数据集的行数为零");}

//OracleCommand cmd = conn.CreateCommand(); //创建命令对象
//cmd.CommandText = "select USERNAME from USER_MST where id='" + userid + "'";  //提供oracle命令
//不可以使用:string strOrcSelect = string.Format("select count(*)  from USER_MST where USERID = '{0}'and PASSWORD = '{1}'", this.id.Text.Trim(), this.password.Text.Trim());
//ExecuteReader()和ExecuteScalar()的用法

string strOrcSelect = "select USERNAME from USER_MST where USERID='" + userid + "'and PASSWORD='" + psd + "'";//注意使用'" + userid + "','" + psd + "'中单引号可以加也可以不加
//string strOrcSelect = string.Format("select USERNAME from USER_MST where USERID = '{0}' and PASSWORD = '{1}'", this.id.Text.Trim(), this.password.Text.Trim());

OracleCommand cmd = new OracleCommand(strOrcSelect, conn);

if (cmd.ExecuteScalar() == null || cmd.ExecuteScalar()== System.DBNull.Value)//comm.ExecuteScalar()需要进行判断,数据库查找结果为空时;注意大写NULL是错误的
{
//MessageBox.Show("登陆失败!用户ID或密码错误!");
//Response.Write(@"<script>alert('登陆失败!用户ID或密码错误!')</script>");
//Response.Write(@"<script language='javascritp'>alert('登陆失败!用户ID或密码错误!');location.href='Userloagin.aspx'</script>");
//Page.ClientScript.RegisterStartupScript(GetType(), "error", "<script>alert('用户ID或密码错误!');</script>");
Label.Text = "登陆失败!用户ID或密码错误!";
}
else
{
int i = int.Parse(cmd.ExecuteScalar().ToString());//comm.ExecuteScalar()大于0
Label.Text = "登录成功";
//利用Session进行保存userid,username

//方法二:
//OracleDataReader读取多个结果集,OracleDataReader中的NextResult()来接收多个结果集
//DataReader对象提供只读单向数据的快速传递。
//单向:您只能依次读取下一条数据;只读:DataReader中的数据是只读的,不能修改;相对地,DataSet中的数据可以任意读取和修改
//只可使用:select USERNAME  from USER_MST where USERID = userid and PASSWORD = psd;

//OracleDataReader red = cmd.ExecuteReader(); //执行命令返回结果指派给DataReader对象
//while (red.Read())//循环列
//{
//    string username = red["USERNAME"].ToString();
//    Session["username"] = username;
//    Session["userid"] = userid;
//    Server.Transfer("menu.aspx");
//}

//方法三
//只可使用:select USERNAME  from USER_MST where USERID = userid and PASSWORD = psd;

string username = cmd.ExecuteScalar().ToString();
Session["username"] = username;
Console.Write(Session["username"].ToString());
Session["userid"] = userid;
Response.Redirect("menu.aspx"); //Server.Transfer("menu.aspx");
}
}
catch (OracleException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
}
}
}


登陆成功后跳转到系统主界面:
menu.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="menu.aspx.cs" Inherits="UserLogin.main" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center">

欢迎进入该系统</div>
<p>
用户ID:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</p>
<p>
用户名:<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</p>
</form>
<p>
</p>
</body>
</html>
menu.aspx

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

namespace UserLogin
{
public partial class main : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["userid"].ToString();
Label2.Text = Session["username"].ToString();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: