<%@ Page Language="C#" ContentType="text/html" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
private void Page_Load(object sender,EventArgs e)
{
string Rname=Request["n"];
string Rpswd=Request["p"];
if((Rname!=null)&&(Rpswd!=null))
{
username.Text=Rname;
userpswd.Text=Rpswd;
Login_in(sender,e);
}
}
private void Login_in(object sender,EventArgs e)
{
string TempName=username.Text;
string TempPswd=userpswd.Text;
if(TempName==null)
{
Response.Write("<script language='javascript'>alert('请输入用户名和密码');window.history.back();<"+"/script>");
}
else
{
//设定数据库连接
string ConnectionStr="server=127.0.0.1;uid=FinanceUser;pwd=FinanceData;database=Finance";
//string ConnectionStr=ConfigurationSettings.AppSettings["connStr"].ToString();
SqlConnection SconnStr=new SqlConnection(ConnectionStr);
SqlCommand Scmd=new SqlCommand("select top 1 * from User() where Name='"+TempName+"'order by ID",SconnStr);
SqlDataReader odr=null;
if(SconnStr.State.ToString()=="Closed")SconnStr.Open();
try
{
//执行命令 获取数据流
odr=Scmd.ExecuteReader();
if(odr.Read())
{
string UserName=odr["Name"].ToString();
string UserPswd=odr["Pswd"].ToString();
string UserLevel=odr["UserLever"].ToString();
if((UserName==TempName)&&(UserPswd==TempPswd))
{
if((UserLevel!="")&&(int.Parse(UserLevel)>0))
{
Session["UserInfo"]=(UserLevel+"0").Substring(0,1)+UserName;
Response.Write("<script language='javascript'>top.location.href='/';<"+"/script>");
}
else
{
Response.Write("<script language='javascript'>alert('你还未通过验证');top.location.href='/';<"+"/script>");
}
}
else
{
Response.Write("<script language='javascript'>alert('账号密码有误'); window.history.bock();<='/';<"+"/script>");
}
}
else
{
Response.Write("<script language='javascript'>alert('没有该账号'); window.history.bock();<"+"/script>");
}
odr.Close();
}
catch(Exception ex)
{
if(ex!=null)Response.Write(ex.ToString());
}
SconnStr.Close();
}
Response.End();
}
// Insert page code here
//
void LoginBtb_Click(object sender, EventArgs e) {
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:Label id="Label1" runat="server" text="用户名"></asp:Label>
<asp:TextBox id="username" runat="server"></asp:TextBox>
</p>
<p>
<asp:Label id="Label2" runat="server" text="密 码"></asp:Label>
<asp:TextBox id="userpswd" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button id="LoginBtb" onclick="Login_in" runat="server" Text="登录"></asp:Button>
</p>
<!-- Insert content here -->
</form>
</body>
</html>
这是代码。
晕
在asp.net web matrix中复制不了,只能在dreamweaver中复制!
我建数据库时就是在sql server 2000中直接建立的数据库,并没有设置登录名和密码,但在程序中把uid=FinanceUser;pwd=FinanceData;去掉也不行