摘自:http://hi.baidu.com/281305450/blog/item/e70cc344f714128cb3b7dc64.html

存储过程检测用户名是否重复

create proc checklogin
@username nchar(20),
@pwd nchar(20),
@hasrow int output
as
select @hasrow=count(*) from users where username=@username and pwd=@pwd
GO

存储过程写入数据

create proc adduser
@username nchar(20),
@pwd nchar(20)
as
begin
insert into users (username,pwd)values(@username,@pwd)
end
GO


aspx页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true" %>

<!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>
   
    &nbsp;用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
            ControlToValidate="TextBox1" ErrorMessage="*"></asp:RequiredFieldValidator>
        <br />
        密码:<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
            ControlToValidate="TextBox2" EnableTheming="True" ErrorMessage="*"></asp:RequiredFieldValidator>
        <br />
        确认密码:<asp:TextBox ID="TextBox3" runat="server" TextMode="Password"></asp:TextBox>
        <asp:CompareValidator ID="CompareValidator1" runat="server"
            ControlToCompare="TextBox3" ControlToValidate="TextBox2"
            CultureInvariantValues="True" ErrorMessage="两次密码不一样"></asp:CompareValidator>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
   
    </div>
    </form>
</body>
</html>

CS页面

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      
        string connstr = "Data Source=.;Database=test;UID=sa;PWD=sa;";
        SqlConnection conn = new SqlConnection(connstr);
        SqlCommand command = new SqlCommand("checkuser",conn);
        command.CommandType = CommandType.StoredProcedure;

        SqlParameter parameter = new SqlParameter("@username", SqlDbType.NChar, 20);
        parameter.Value = this.TextBox1.Text;
        command.Parameters.Add(parameter);

        SqlParameter parameter1 = new SqlParameter("@result",SqlDbType.Int,4);
        parameter1.Direction = ParameterDirection.Output;
        command.Parameters.Add(parameter1);

        conn.Open();
        command.ExecuteNonQuery();
        conn.Close();
        int result =Convert.ToInt32( parameter1.Value);

       
        if (result == 1)
        {
            Response.Write("此用户已经存在");
        }
        else
        {
            //Response.Write("可以注册");
            SqlCommand command1 = new SqlCommand("adduser", conn);
            command1.CommandType = CommandType.StoredProcedure;
            SqlParameter para_username = new SqlParameter("@username", SqlDbType.NChar);
            para_username.Value = this.TextBox1.Text;
            command1.Parameters.Add(para_username);

            SqlParameter para_pwd = new SqlParameter("@pwd", SqlDbType.NChar);
            para_pwd.Value = this.TextBox2.Text;
            command1.Parameters.Add(para_pwd);
            conn.Open();
            command1.ExecuteNonQuery();
            conn.Close();
            Response.Write("添加成功!");
        }

    }
}

posted on 2010-11-12 14:14  露水丛生  阅读(1164)  评论(0编辑  收藏  举报