asp.net 調用存儲過程(帶有輸入,輸出參數 範例)二

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

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
GOstring

 

前臺代碼省略。it

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

using System.Data.SqlClient;class

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

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

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

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

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

    }}

相關文章
相關標籤/搜索