首页 > 代码库 > 方法:C#用session做登陆
方法:C#用session做登陆
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<HTML>
<HEAD>
<TITLE> 登陆 的例子 </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<Script Language="C#" runat="server">
void Add_Click (Object sender,EventArgs e)
{
string str_id=Tel1.Text;
string str_name=Tel2.Text;
//Session["id"]=str_name;
//Session["UserName"]=str_name;
SqlCommand myCommand;
string myselect;
string myConnString=@"server=localhost;uid=sa;pwd= ;database=test ";
SqlConnection myConnection =new SqlConnection(myConnString);
myConnection.Open();
myselect="Select * from test where id = ‘"+Tel1.Text+"‘ and name=‘"+Tel2.Text+"‘";
myCommand=new SqlCommand(myselect,myConnection);
SqlDataReader reader=myCommand.ExecuteReader();
//SqlDataReader reader=myCommand.ExecuteReader();
if(Tel1.Text=="" || Tel2.Text=="")
{
Label1.Text="学号姓名不能为空";
}
else if( Session["UserName"]!=null || Session["id"]!=null )
{
Label1.Text="你已经登陆";
}
else if(!reader.Read())
{
Label1.Text="数据库没有信息!";
reader.Close();
}
else
{
Label1.Text="登陆成功";
// Label.Text=Session["Username"].ToString();
//reader.Close();
Tel1.Text="";
Tel2.Text=" ";
Session["UserName"]=str_name;;
Session["id"]=str_id;
//Response.Redirect("first3.aspx");
}
myConnection.Close();
}
</Script>
</HEAD>
<BODY>
<form runat="server">
学号: <asp:TextBox id="Tel1" size="20" runat="server"/>
<p>
姓名: <asp:TextBox id="Tel2" size="20" runat="server"/>
<p>
<asp:Button id="Add" onClick="Add_Click" runat="server" Text="登陆" ></asp:Button>
<p>
<asp:Label id="Label1" runat="server"/>
</form>
</BODY>
</HTML>
方法:C#用session做登陆