完整代码:
a1.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="a1.aspx.cs" Inherits="a1" %>
<!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>
<asp:Label ID="Label1" runat="server" Height="24px" Text="用户名:" Width="110px"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<br />
<asp:Label ID="Label2" runat="server" Height="21px" Text="密码:" Width="108px"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password" Width="151px"></asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" Height="27px" OnClick="Button1_Click" Text="Button"
Width="96px" /></div>
</form>
</body>
</html>
a1.aspx.cs:
using System;
using System.Data;
using System.Web;
using System.Data.SqlClient;
public partial class a1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string str = "select xh,mm from xs where xh='" + TextBox1.Text + "'";
SqlConnection con = new SqlConnection(@"Server=;Integrated Security=True;" + "Database=");
//SERVER添加服务器名,DataBase添加数据库名,
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
con.Open();
cmd.CommandText = str;
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.Read())
{
if (dr["mm"].ToString() == TextBox2.Text)
Response.Write(@"<script>alert('欢迎登录', ' + TextBox1.Text + ')</script>");
else
Response.Write(@"<script>alert('密码错误')</script>");
}
else
{
Response.Write(@"<script>alert('没有记录')</script>");
}
}
}
读取,连接数据不难,不过要注意命名空间的引用。。