zoukankan      html  css  js  c++  java
  • asp.net 调用存储过程(带有输入,输出参数 范例)二

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

    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

    前台代码省略。

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

        }
    }

  • 相关阅读:
    linux时间同步
    阿里云自定义监控tomcat进程数
    Kafka监控
    阿里云ECS主机自定义进程监控
    kafka常用运维命令
    命令--cut
    Linux查看用户及分组
    Bind搭建DNS服务
    使用Bind搭建DNS服务
    DNS开源服务器BIND最小配置详解
  • 原文地址:https://www.cnblogs.com/kevinhome/p/3367845.html
Copyright © 2011-2022 走看看