asp.net調用帶有輸入和輸出參數的存儲過程

存儲過程檢測用戶名是否重戶html

create proc checklogin
@username nchar(20),
@pwd nchar(20),
@hasrow int output
as
select @hasrow=count(*) from users where and
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" %>ui

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">this

<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;orm

using System.Data.SqlClient;server

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

    }
    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;htm

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

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

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

       
        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("添加成功!");
        }

    }}

相關文章
相關標籤/搜索