删除存储过程已经写好
CREATE PROCEDURE SP_DelClass  (@ID VARCHAR(50))AS
Delete  FROM Cuser WHERE id=@ID
GO
在C#里怎么去调用 并执行这段存储过程

解决方案 »

  1.   

    写了段代码 但没有执行 不知道什么原因
            public override bool DeleteUser(int id)
            {
                bool ds = false;
                using (SqlConnection conn = GetConnection())
                {
                    
                    SqlCommand cmd = new SqlCommand("SP_DelClass", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@ID", SqlDbType.VarChar, 50).Value = id;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.DeleteCommand = cmd;
                }
                return ds;
                //throw new Exception("The method or operation is not implemented.");
            }
      

  2.   

    public override int DeleteUser(int id)
            {
                int i = 0;
                using (SqlConnection conn = GetConnection())
                {
                    
                    SqlCommand cmd = new SqlCommand("SP_DelClass", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter mypara = new SqlParameter("@ID",id);
             cmd.Parameters.Add(mypara);
                    i = cmd.ExecuteNonQuery();
                }
                 return i; //返回受影响的行数
            }
      

  3.   

    我想用 返回值为bool 值, 应该怎么写
      

  4.   

    根据返回行数再判断咯 return i>0;
      

  5.   

    public override bool DeleteUser(int id)
            {
                using (SqlConnection conn = GetConnection())
                {
                    bool ds = false;
                    SqlCommand cmd = new SqlCommand("SP_DelClass", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter mypara = new SqlParameter("@ID",id);
                    cmd.Parameters.Add(mypara);
                    try{
                         cmd.ExecuteNonQuery();
                         ds = true;
                       }
                   catch
                      { 
                         ds = fasle;
                       }
                }
                 return ds; //返回bool值
            }