<%@ Page Language="C#" AutoEventWireup="true" CodeFile="admin.aspx.cs" Inherits="admin" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center; padding-top: 150px">
<table>
<tr>
<td>
用户名:
</td>
<td>
<asp:TextBox ID="userName" runat="server" Width="96px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
密 码:
</td>
<td>
<input id="password" style="width: 96px" type="password" runat="server"/>
</td>
</tr>
<tr>
<td>
<asp:Button ID="submit" runat="server" Text="登 陆" OnClick="submit_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//添加数据库引用
using System.Data.SqlClient;
public partial class admin : System.Web.UI.Page
{
//定义全局变量
protected void Page_Load(object sender, EventArgs e)
{
//页面初始化,一般写在postback中
}
//用户自定义函数,此处为登陆验证函数
protected void submit_Click(object sender, EventArgs e)
{
//定义一个连接字符串
string connString = "Data Source=localhost;Initial Catalog=users;Persist Security Info=True;User ID=sa;password=123";
//创建一个连接数据库对象
SqlConnection con = new SqlConnection(connString);
//定义sql语句
string sqlstring = "select * from adminUser where username='" + userName.Text.Trim() + "' and password='" + password.Value.Trim()+ "'";
//定义查询数据库对象
SqlCommand com = new SqlCommand(sqlstring,con);
//定义SqlDataReader
SqlDataReader sdr = new SqlDataReader();
try
{
//打开连接
con.Open();
//数据填充
sdr = com.ExecuteReader();
//判断
if (sdr.HasRows)
{
//循环读取数据
while (sdr.Read())
{
//转向
Response.Redirect("error.aspx?type=suc&name=" + sdr.GetValue(0));
}
}
else
{
Response.Redirect("error.aspx?type=failure");
}
}
catch (SqlException ex)
{
Response.Write(ex);
}
finally
{
con.Close();
}
}
}