简单的登录注册界面测试(asp.net)

 asp.net编写登录注册代码  
win7 
asp.net+ms sql 2005+vs2010(开发平台)


数据库名userinfo
username  varchar(50) 

userpassword varchar(50)

连接数据库,写在类db.cs里面,其代码如下: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

/// 
///db 的摘要说明
/// 
public class db
{
	public db()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}
    public static SqlConnection CreateConnection()
    {
        SqlConnection con = new SqlConnection("server=localhost\\SQL2005;database=userinfo;uid=sa;pwd=1111qq;");
        return con;

    }
}

login.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>





    登录界面


    


 


login.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.SqlClient;
using System.Data;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
        protected void allow_Click(object sender, EventArgs e)
   {
       if((labuser.Text=="")||(labpwd.Text==""))
       {
           Label3.Text="用户名与密码不能为空!";

       }
       else
       {
           SqlConnection con = db.CreateConnection();
           con.Open();
           string strSql = "select password  from userinfo where username='"+labuser.Text+"'";
           SqlCommand cmd = new SqlCommand(strSql, con);
           DataSet ds =new DataSet();
           SqlDataAdapter da =new SqlDataAdapter(strSql,con);
           da.Fill(ds,"mytable");
           try
           {
               if(labpwd.Text==ds.Tables[0].Rows[0].ItemArray[0].ToString().Trim())
               {
                   string curuser=labuser.Text;
                    Label3.Text="登录成功,欢迎你!";
               }
               else
               {
                   Label3.Text="用户名或者密码错误!";
               }
           }
           catch
           {
               Label3.Text="Sorry!你输入的用户名不存在!";
               
           }
           con.Close();
       }
       
   }
   protected void abov_Click(object sender, EventArgs e)
   {
       if((labuser.Text=="")||(labpwd.Text==""))
       {
           Label3.Text="用户名为或密码不能为空";
       }
       else
       {
           try
           {
               SqlConnection con = db.CreateConnection();
               con.Open();
               string strsql = "insert into userinfo values('" + labuser.Text + "','" + labpwd.Text + "')";
               SqlCommand cmd=new SqlCommand(strsql,con);
               cmd.ExecuteNonQuery();
               con.Close();
               labuser.Text="";
               labpwd.Text="";
               Label3.Text="注册成功!欢迎登录!";
           }
           catch
           {
               Label3.Text="用户名已存在!";
           }
       }
   }
}

    

简单的登录注册界面测试(asp.net)_第1张图片

你可能感兴趣的:(asp.net)