登录界面:
Userloagin.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Userloagin.aspx.cs" Inherits="UserLogin.Userloagin" %>
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(@"");
}
if (password.Text == "")
{
//MessageBox.Show("密码不能为空!");
Response.Write(@"");
}
//连接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(@"");
//Response.Write(@"");
//Page.ClientScript.RegisterStartupScript(GetType(), "error", "");
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" %>
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();
}
}
}