递归的例子1 计算1到100相加的值
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Response.Write(checks(100));
}
}
public static int checks(int i)
{
if (i == 0)
{
return 0;
}
return checks(i-1)+i;
}
}
递归的例子2
Dal dal = new Dal();
protected void Page_Load(object sender, EventArgs e)
{
string road ="";
getCategoryRoad(603, ref road); //主要用来存储数据
Response.Write(road);
}
public void getCategoryRoad(int category_id,ref string strRoad)
{
DataTable dt = dal.GetDatable(category_id);
if (dt.Rows.Count>0)
{
int category_father_id = Convert.ToInt32(dt.Rows[0]["category_father_id"]);
strRoad += dt.Rows[0]["category_father_id"].ToString()+",";
getCategoryRoad(category_father_id, ref strRoad);
}
else
{
return;
}
}
public DataTable GetDatable(int id)//type=2 father_id type=1 id
{
int type = 1;
_accessMySql = DBFactory.getDBAccess(DBType.MySql, connectionString);
this.connStr = connectionString;
StringBuilder sbSql = new StringBuilder();
sbSql.Append("select category_id,category_father_id,category_name from product_category where 1=1 ");
if (1 == type)
{
sbSql.AppendFormat(" and category_id = {0}", id);
}
else
{
sbSql.AppendFormat(" and category_father_id = {0}", id);
}
DataTable dt = _accessMySql.getDataTable(sbSql.ToString());
return dt;
}