asp.net调用带有输入和输出参数的存储过程
2020-11-27 17:12
YPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
存储过程检测用户名是否重户
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页面
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("添加成功!");
}
}
}