c#简单的用户登录代码
2014-09-10 00:00:00 by 【6yang】,
379
visits,
收藏 |
返回
index.aspx 代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="test.index" title="test"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
.auto-style1 {
width: 96%;
height: 71px;
}
.auto-style2 {
width: 307px;
}
</style>
</head>
<body style="width: 274px">
<form id="form1" runat="server">
<div>
用户登录</div>
<table class="auto-style1">
<tr>
<td>
ID:</td>
<td class="auto-style2">
<asp:TextBox ID="txt_username" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>PWD:</td>
<td class="auto-style2">
<asp:TextBox ID="txt_pwd" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" OnClick="lnklogin_Click" Text="submit" />
<br />
<asp:Label ID="lb_msg" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
<asp:BoundField DataField="user" HeaderText="user" SortExpression="user" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
index.aspx.cs 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
using System.Configuration;
namespace test
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = @"data source=.SQLEXPRESS;initial catalog=test;persist security info=True;user id=sa;password=123456"; //Pooling=False;
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
Response.Write(conn.State);
SqlCommand cmd = new SqlCommand("select * from admin", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "admin");
this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.DataBind();
}
protected void lnklogin_Click(object sender, EventArgs e)
{
if (this.txt_username.Text == "admin" && this.txt_pwd.Text == "123456")
{
this.lb_msg.Text = "成功登录";
} else {
this.lb_msg.Text = "对不起,你没有权限!";
}
}
}
}
share