using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Data;
namespace DBUnitly
{
public class DBHelp
{
public static string _connectionString = ConfigurationManager.AppSettings["connectionString"];//连接数据库
/// <summary>
/// 执行sql语句 返回影响的行数
/// </summary>
/// <param name="SQLString"></param>
/// <returns></returns>
public static int ExecuteSql(string SQLString)
{
using(SqlConnection conn=new SqlConnection(_connectionString))
{
using(SqlCommand cmd=new SqlCommand(SQLString,conn))
{
try
{
conn.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}catch(System.Data.SqlClient.SqlException e)
{
conn.Close();
throw e;
}
}
}
}
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
}
}
DAL层:
_menuManager类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace DAL
{
public class _menuManager
{
/// <summary>
/// 分页获取数据列表
/// </summary>
public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT * FROM ( ");
strSql.Append(" SELECT ROW_NUMBER() OVER (");
if (!string.IsNullOrEmpty(orderby.Trim()))
{
strSql.Append("order by T." + orderby);
}
else
{
strSql.Append("order by T.menu_id desc");
}
strSql.Append(")AS Row, T.* from _menu T ");
if (!string.IsNullOrEmpty(strWhere.Trim()))
{
strSql.Append(" WHERE " + strWhere);
}
strSql.Append(" ) TT");
strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
return DBUnitly.DBHelp.Query(strSql.ToString());
}
/// <summary>
/// 获得数据列表
/// </summary>
public DataSet GetList(string strWhere)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select menu_id,menu_name");
strSql.Append(" FROM _menu ");
if (strWhere.Trim() != "")
{
strSql.Append(" where menu_quanxian in (" + strWhere+")");
}
return DBUnitly.DBHelp.Query(strSql.ToString());
}
}
}
_menuChildrenManager类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace DAL
{
public class _menuChildrenManager
{
/// <summary>
/// 获得数据列表
/// </summary>
/// <param name="strWhere"></param>
/// <returns></returns>
public DataSet GetAllList(string strWhere)
{
StringBuilder sb = new StringBuilder();
sb.Append("select mc_name ");
sb.Append(" from _menuChildren ");
sb.Append(" where menu_type in ");
if(strWhere.Trim()!="")
{
sb.Append("("+ Convert.ToInt32(strWhere)+")");
}
return DBUnitly.DBHelp.Query(sb.ToString());
// select mc_name from _menuChildren where menu_type in( select menu_id from _menu );
}
}
}
BLL层:
_menuService类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace BLL
{
public class _menuService
{
/// <summary>
/// 分页获得数据列表
/// </summary>
/// <param name="strWhere"></param>
/// <param name="orderby"></param>
/// <param name="startIndex"></param>
/// <param name="endIndex"></param>
/// <returns></returns>
public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
{
return new DAL._menuManager().GetListByPage(strWhere, orderby, startIndex, endIndex);
}
/// <summary>
/// 获得数据列表
/// </summary>
/// <param name="strWhere"></param>
/// <returns></returns>
public DataSet GetList(string strWhere)
{
return new DAL._menuManager().GetList(strWhere);
}
}
}
_menuChildrenService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace BLL
{
public class _menuChildrenService
{
/// <summary>
/// 获得数据列表
/// </summary>
/// <param name="strWhere"></param>
/// <returns></returns>
public DataSet GetAllList(string strWhere)
{
return new DAL._menuChildrenManager().GetAllList(strWhere);
}
}
}
Test层:
MenuTest.aspx
<body>
<form id="form1" runat="server">
<div>
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal"
DynamicEnableDefaultPopOutImage="False" DynamicVerticalOffset="10"
Height="24px" Width="126px">
<StaticSelectedStyle HorizontalPadding="0px" />
<StaticMenuItemStyle HorizontalPadding="5px" ForeColor="#3C3C3C" />
<DynamicMenuStyle HorizontalPadding="0px" />
<DynamicMenuItemStyle HorizontalPadding="0px" ItemSpacing="3px" ForeColor="#D96C01" />
<StaticMenuStyle HorizontalPadding="0px" />
<DynamicHoverStyle ForeColor="#3D68A3" />
<StaticHoverStyle ForeColor="#D96C01" />
</asp:Menu>
</div>
</form>
</body>
.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class MenuTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new BLL._menuService().GetList("0").Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
MenuItem menuNode = new MenuItem();
menuNode.Text = dt.Rows[i]["menu_name"].ToString();//一级菜单栏
menuNode.Value = dt.Rows[i]["menu_id"].ToString();
DataTable dt1 = new BLL._menuChildrenService().GetAllList(menuNode.Value).Tables[0];
for (int j = 0; j < dt1.Rows.Count; j++)
{
MenuItem menuChildNode = new MenuItem();
menuChildNode.Text = dt1.Rows[j]["mc_name"].ToString();//二级菜单
menuChildNode.Enabled = true;
menuNode.ChildItems.Add(menuChildNode);//将子菜单放在父菜单下
}
menuNode.Enabled = true;
Menu1.Items.Add(menuNode);
}
}
}
}