存储过程——数据库优化方法(二)

use Student
create table UserLogin
(
    UserName varchar(50) not null,
    UserPassword varchar(50) not null
)
alter procedure sp_Login
@uid varchar(50),
@pwd varchar(50),
@result bit output
as
begin
  declare @count int
  set @count=(select count(*) from UserLogin where UserName=@uid and UserPassword=@pwd)
  if @count>1
    begin
     set @result=1
    end
  else
    begin
      set @result=0
    end
end
declare @r bit
exec sp_Login 'ggg','ggg',@result=@r output
print @r
private void button1_Click(object sender, EventArgs e)
        {
            string pwd = this.textBox1.Text.Trim();
            string uid = this.textBox2.Text.Trim();
            string constr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_Login",con);
            SqlParameter[] sp = new SqlParameter[]{
              new SqlParameter("@uid",pwd),
              new SqlParameter("pwd",uid),
              new SqlParameter("@result",SqlDbType.Bit)
            };
            sp[2].Direction = ParameterDirection.Output;
            cmd.Parameters.AddRange(sp);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.ExecuteScalar();
            bool b = Convert.ToBoolean(sp[2].Value);
            if (b)
            {
                MessageBox.Show("登录成功");
            }
            else
            {
                MessageBox.Show("登录失败");
            }

            
        }
原文地址:https://www.cnblogs.com/772933011qq/p/4534560.html