一﹑存儲過程
create procedure User_Login
@user_id varchar(20),
@password varchar(50),
@Return_Status char(1) output,
@user_name varchar(20) output,
@dept varchar(20) output,
@duty varchar(20) output
AS
declare @dept_id varchar(4)
declare @duty_id varchar(4)
select @dept=''
select @duty=''
select @user_name=''
select @return_status='0' --不知明錯誤
if exists(select user_id from user_list where user_id=@user_id)
begin
if exists(select user_id from user_list where user_id=@user_id and password=@password)
begin
select @user_name=user_name,@dept_id=dept_id,@duty_id=duty_id from user_list where user_id=@user_id and password=@password
select @dept=sname from dept_list where dept_id=@dept_id
select @duty=sname from duty_list where duty_id=@duty_id
select @return_status='3' --正確登錄
end
else
select @return_status='2' --密碼錯誤
end
else
select @return_status='1' --用戶名不存在
GO
二﹑login.aspx(C#)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;
namespace hterp
{
/// <summary>
/// Summary description for _Default.
/// </summary>
public class _Default : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Msg;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected SqlConnection Scn;
private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack)
{
Msg.Visible=false;
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support -do
not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
Common_Data conn=new Common_Data();
SqlCommand myComm=new SqlCommand("User_Login",conn.Common_Conn());
myComm.CommandType=CommandType.StoredProcedure;
myComm.Parameters.Add("@user_id",SqlDbType.NChar,20);
myComm.Parameters["@user_id"].Value=TextBox1.Text;
myComm.Parameters.Add("@password",SqlDbType.NChar,20);
myComm.Parameters["@password"].Value=TextBox2.Text;
myComm.Parameters.Add("@Return_Status",SqlDbType.Char,1);
myComm.Parameters.Add("@user_Name",SqlDbType.NChar,20);
myComm.Parameters.Add("@Dept",SqlDbType.NChar,20);
myComm.Parameters.Add("@duty",SqlDbType.NChar,20);
myComm.Parameters["@return_Status"].Direction=ParameterDirection.Output;
myComm.Parameters["@User_Name"].Direction=ParameterDirection.Output;
myComm.Parameters["@Dept"].Direction=ParameterDirection.Output;
myComm.Parameters["@duty"].Direction=ParameterDirection.Output;
try
{
myComm.Connection.Open();
SqlDataReader dr=myComm.ExecuteReader();
if (myComm.Parameters["@Return_Status"].Value.ToString()=="0") {
Msg.Visible=true;
Msg.Text="不知名錯誤﹐請與系統管理員聯系";
Response.Write(Functions.ShowErrMsg(Msg.Text));
}
if (myComm.Parameters["@Return_Status"].Value.ToString()=="1") {
Msg.Visible=true;
Msg.Text="用戶名不存在﹗";
Response.Write(Functions.ShowErrMsg(Msg.Text));
}
if (myComm.Parameters["@Return_Status"].Value.ToString()=="2") {
Msg.Visible=true;
Msg.Text="密碼錯誤﹗";
Response.Write(Functions.ShowErrMsg(Msg.Text));
}
if (myComm.Parameters["@Return_Status"].Value.ToString()=="3") {
Msg.Visible=false;
HttpCookie User_Cookie=new HttpCookie("User_Name");
User_Cookie.Value=myComm.Parameters["@user_Name"].Value.ToString();
Response.Cookies.Add(User_Cookie);
HttpCookie Dept_Cookie=new HttpCookie("Dept");
Dept_Cookie.Value=myComm.Parameters["@Dept"].Value.ToString();
Response.Cookies.Add(Dept_Cookie);
HttpCookie Duty_Cookie=new HttpCookie("Duty");
Duty_Cookie.Value=myComm.Parameters["@duty"].Value.ToString();
Response.Cookies.Add(Duty_Cookie);
//Response.Write("<script>window.open('http://192.168.0.200/hterp/default.aspx','new','fullscreen=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,width=790,height=538,resizable=0,top=0,left=0');
</script>");
Response.Redirect("http://192.168.0.200/hterp/default.aspx");
}
}
finally {
myComm.Dispose();
conn.Common_Conn().Close();
}
}
}
}
這是我本人為公司管理軟件寫的﹐沒有問題
試試吧