11-13asp中实现存储过程的增删改查!

注册:

 

  protected void btnlogin_Click(object sender, EventArgs e)
        {
            string username = txtusername.Text.Trim();
            string password = txtpassword.Text.Trim();
            string email = txtemail.Text.Trim();
            using (SqlConnection conn = new SqlConnection(MyTestconstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    string sp_name = "usp_regiter2";
                    cmd.CommandText = sp_name;
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter prm1 = new SqlParameter("@username", username);
                    SqlParameter prm2 = new SqlParameter("@password", password);
                    SqlParameter prm3 = new SqlParameter ("@email",email );
                    cmd.Parameters.Add(prm1);
                    cmd.Parameters.Add(prm2);
                    cmd.Parameters.Add(prm3);
                    int r = cmd.ExecuteNonQuery();
                    Label1.Text = r.ToString();
                }
            }
        }

删:

 protected void Button2_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(MyTestconstr))
            {
                conn.Open ();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    string sp_name = "usp_student_delete";
                    cmd.CommandText = sp_name;
                    cmd.CommandType = CommandType.StoredProcedure;

                    SqlParameter prm = new SqlParameter("@sid", 3);
                    cmd.Parameters.Add(prm);
                    int r=cmd.ExecuteNonQuery();
                    if (r > 0)
                    {
                        Label1.Text = "删除成功!";
                    }
                    else
                    {
                        Label1.Text = "删除失败!";
                    }
               
               
                }
           
           
            }

        }

改:

 protected void Button3_Click(object sender, EventArgs e)
        {
            string schoolsconstr = ConfigurationManager.ConnectionStrings["schoolsconstr"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(schoolsconstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    string sp_name = "usp_Class_update";
                    cmd.CommandText = sp_name;
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter prm1 = new SqlParameter( "@clsname",txtusername.Text );
                    SqlParameter prm2 = new SqlParameter( "@disc ",txtpassword .Text );
                    SqlParameter prm3 = new SqlParameter("@clsid  ", txtdelete .Text );
                    cmd.Parameters.Add(prm1);
                    cmd.Parameters.Add(prm2);
                    cmd.Parameters.Add(prm3);
                    int r = cmd.ExecuteNonQuery();
                    if (r > 0)
                    {
                        Label1.Text = "更新成功!";
                    }
                    else
                    {
                        Label1.Text = "更新失败!";
                    }


                }


            }

登陆:方法一

  protected void btnlogin_Click(object sender, EventArgs e)
        {
            string username = txtusername.Text.Trim();
            string password = txtpassword.Text.Trim();
            string  MyTestconstr= ConfigurationManager.ConnectionStrings["MyTestconstr"].ConnectionString;
           using (SqlConnection  conn=new SqlConnection (MyTestconstr))
            {
                conn.Open();
                 using(SqlCommand  cmd=conn.CreateCommand ())
                 {
                     string sp_name="usp_login";
                     cmd.CommandText = sp_name;
                     cmd.CommandType = CommandType.StoredProcedure;
               
                     SqlParameter prm1 = new SqlParameter("@uname", username);
                     SqlParameter prm2 = new SqlParameter("@password",password );
                     SqlParameter prm3 = new SqlParameter("@result",System.Data .SqlDbType .Bit );
                     prm3.Direction = ParameterDirection.Output;-------------------注意点
                     cmd.Parameters .Add (prm1 );
                     cmd.Parameters .Add (prm2 );
                     cmd.Parameters .Add (prm3 );

                     int r = cmd.ExecuteNonQuery();
                     bool b=Convert .ToBoolean (prm3.Value );
                     if (b)
                     {
                      Label1.Text ="登陆成功";
                    
                     }
                     else
                     {
                         Label1 .Text ="登陆失败";
                    
                     }
                     


              
                 }
             
           
           
           
            }

方法二:

protected void btnlogin_Click(object sender, EventArgs e)
        {
           string username = txtusername.Text.Trim();
            string password = txtpassword.Text.Trim();
            string  MyTestconstr= ConfigurationManager.ConnectionStrings["MyTestconstr"].ConnectionString;
           using (SqlConnection  conn=new SqlConnection (MyTestconstr))
            {
                conn.Open();
                 using(SqlCommand  cmd=conn.CreateCommand ())
                 {
                     string sp_name = "usp_chkLogin";
                     cmd.CommandText = sp_name;
                     cmd.CommandType = CommandType.StoredProcedure;
               
                     SqlParameter prm1 = new SqlParameter("@username", username);
                     SqlParameter prm2 = new SqlParameter("@password",password );
                     cmd.Parameters .Add (prm1 );
                     cmd.Parameters .Add (prm2 );
                     object obj = cmd.ExecuteScalar();
                     int b = Convert.ToInt32(obj);
                     if (b>0)
                     {
                      Label1.Text ="登陆成功";
                    
                     }
                     else
                     {
                         Label1 .Text ="登陆失败";
                    
                     }
                 }
            }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值