你的位置:首页 > ASP.net教程

[ASP.net教程]最近写的小型CMS免费开源出来


mvc太过于臃肿,所以这里采用webform形式,razor语法实现前后台(好像很少人这么写过),写这个CMS的目的主要是解决各类网站系统中不同的表单数据的输出,所以采用了部分固定字段+自定义字段(json),目前只写了后台,后台模板采用的是bootstrap模板(AdminLTE-2.3.0),主要有四块功能,模型(主要是定义数据表单)、栏目(网站里的栏目管理)、内容(具体的网站内容数据),用户(后台管理员管理)。系统支持多种数据库,为了部署方便,源码里只有access。

 

系统刚开发目前还是有很多问题,发布出来是想交流学习,大家共同进步,欢迎大家加QQ群:7469493。

 

DBHelper.cs 数据库操作基础类

using System;using System.Collections.Generic;using System.Configuration;using System.Data;using System.Data.Common;using System.Dynamic;namespace ST.Framework.Database{  public class DBHelper  {    private DbConnection MyConnection;    private DbCommand MyCommand;    private DbProviderFactory MyFactory;    /// <summary>    /// 数据库类型    /// </summary>    public string ProviderName    {      get;      set;    }    /// <summary>    /// 数据库连接字符串    /// </summary>    public string ConnectionString    {      get;      set;    }    #region DBHelper    public DBHelper()      : this("DataLink")    {    }    public DBHelper(string name)      : this(ConnectionName(name).ProviderName, ConnectionName(name).ConnectionString)    {    }    public DBHelper(string providerName, string connectionString)    {      this.ProviderName = providerName;      this.ConnectionString = ConnectionPath(connectionString);      MyFactory = DbProviderFactories.GetFactory(ProviderName);      MyConnection = MyFactory.CreateConnection();      MyConnection.ConnectionString = ConnectionString;      MyConnection.Open();      MyCommand = MyConnection.CreateCommand();    }    #endregion    #region Connection    /// <summary>    /// 数据连接实体    /// </summary>    public static ConnectionStringSettings ConnectionName(string name)    {      return ConfigurationManager.ConnectionStrings[name];    }    /// <summary>    /// 处理数据库连接路径    /// </summary>    public string ConnectionPath(string connectionString)    {      return String.Format(connectionString, System.Web.HttpContext.Current.Server.MapPath("~/"));    }    #endregion    #region ExecuteNonQuery    /// <summary>    /// ExecuteNonQuery    /// </summary>    public int ExecuteNonQuery(string sql)    {      try      {        MyCommand.CommandText = sql;        return MyCommand.ExecuteNonQuery();      }      catch (Exception e)      {        this.Dispose();        throw e;      }    }    /// <summary>    /// ExecuteNonQuery    /// </summary>    public int ExecuteNonQuery(List<String> list)    {      try      {        int count = 0;        for (int i = 0; i < list.Count; i++)        {          MyCommand.CommandText = list[i];          count += MyCommand.ExecuteNonQuery();        }        return count;      }      catch (Exception e)      {        this.Dispose();        throw e;      }    }    #endregion    #region ExecuteScalar    /// <summary>    /// ExecuteScalar    /// </summary>    public object ExecuteScalar(string sql)    {      try      {        MyCommand.CommandText = sql;        return MyCommand.ExecuteScalar();      }      catch (Exception e)      {        this.Dispose();        throw e;      }    }    #endregion    #region ExecuteDataSet    /// <summary>    /// ExecuteDataSet    /// </summary>    public DataSet ExecuteDataSet(string sql)    {      try      {        MyCommand.CommandText = sql;        DbDataAdapter dataAdapter = MyFactory.CreateDataAdapter();        dataAdapter.SelectCommand = MyCommand;        DataSet dataSet = new DataSet();        dataAdapter.Fill(dataSet);        return dataSet;      }      catch (Exception e)      {        this.Dispose();        throw e;      }    }    #endregion    #region ExecuteReader    /// <summary>    /// ExecuteReader    /// </summary>    public List<dynamic> ExecuteReader(string sql)    {      try      {        var list = new List<dynamic>();        MyCommand.CommandText = sql;        using (DbDataReader dr = MyCommand.ExecuteReader())        {          while (dr.Read())          {            IDictionary<string, object> d = new System.Dynamic.ExpandoObject();            for (int i = 0; i < dr.FieldCount; i++)            {              d.Add(dr.GetName(i), dr.GetValue(i));            }            list.Add(d);          }        }        return list;      }      catch (Exception e)      {        this.Dispose();        throw e;      }    }    #endregion    #region Utils    /// <summary>    /// 参数组    /// </summary>    public void ParameterAdd(string parameterName, object value)    {      DbParameter MyDbParameter = MyCommand.CreateParameter();      MyDbParameter.ParameterName = parameterName;      MyDbParameter.Value = value;      MyDbParameter.DbType = DbType.String;      MyCommand.Parameters.Add(MyDbParameter);    }    /// <summary>    /// 释放所有资源    /// </summary>    public void Close()    {      this.Dispose();    }    /// <summary>    /// 释放所有资源    /// </summary>    public void Dispose()    {      if (MyCommand.Parameters != null)        MyCommand.Parameters.Clear();      if (MyCommand != null)        MyCommand.Dispose();      //if (MyConnection.State == ConnectionState.Open)      if (MyConnection != null)      {        MyConnection.Close();        MyConnection = null;      }    }    #endregion  }}

  

DBShortcut.cs 数据库操作快捷类

using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Linq;namespace ST.Framework.Database{  public class DBShortcut  {    #region 提取数据    /// <summary>    /// 筛选    /// </summary>    public static DataSet Select(Dictionary<string, object> d)    {      string table = d["table"].ToString();      int top = d.ContainsKey("top") ? Convert.ToInt32(d["top"]) : 0;      string field = d.ContainsKey("field") ? d["field"].ToString() : String.Empty;      string where = d.ContainsKey("where") ? d["where"].ToString() : String.Empty;      List<string> list = d.ContainsKey("list") ? (List<string>)d["list"] : null;      string order = d.ContainsKey("order") ? d["order"].ToString() : String.Empty;      return Select(table, top, field, where, order, list);    }    /// <summary>    /// 筛选    /// </summary>    public static DataSet Select(Dictionary<string, object> d, out Dictionary<string, object> page)    {      string table = d["table"].ToString();      int top = d.ContainsKey("top") ? Convert.ToInt32(d["top"]) : 0;      string field = d.ContainsKey("field") ? d["field"].ToString() : String.Empty;      string where = d.ContainsKey("where") ? d["where"].ToString() : String.Empty;      List<string> list = d.ContainsKey("list") ? (List<string>)d["list"] : null;      string order = d.ContainsKey("order") ? d["order"].ToString() : String.Empty;      page = new Dictionary<string, object>();      page.Clear();      if (d.ContainsKey("pagesize"))      {        int count = Count(table);        int pagesize = Convert.ToInt32(d["pagesize"]);        int pagecount = (count + pagesize - 1) / pagesize;        var dpageindex = d["pageindex"];        int pageindex = dpageindex == null ? 1 : Convert.ToInt32(dpageindex);        string pageformat = d.ContainsKey("pageformat") ? d["pageformat"].ToString() : "<a href=\"?page={0}\">{1}</a>";        string pagefirstformat = d.ContainsKey("pagefirstformat") ? d["pagefirstformat"].ToString() : pageformat;        string pageprevformat = d.ContainsKey("pageprevformat") ? d["pageprevformat"].ToString() : pageformat;        string pagenextformat = d.ContainsKey("pagenextformat") ? d["pagenextformat"].ToString() : pageformat;        string pagelastformat = d.ContainsKey("pagelastformat") ? d["pagelastformat"].ToString() : pageformat;        StringBuilder pagenum = new StringBuilder();        int j = (pageindex - 5 < 1) ? (1) : (pageindex - 5);        int k = (pageindex + 5 > pagecount) ? (pagecount) : (pageindex + 5);        for (int i = j; i < k + 1; i++)        {          if (pageindex == i)            pagenum.AppendFormat("<span>{0}</span>", i);          else            pagenum.AppendFormat(pageformat, i, i);        }        string pagefirst = pageindex == 1 ? String.Empty : String.Format(pagefirstformat, "1", "首页");        string pageprev = pageindex == 1 ? String.Empty : String.Format(pageprevformat, pageindex - 1, "上一页");        string pagenext = (pageindex == pagecount || count == 0) ? String.Empty : String.Format(pagenextformat, pageindex + 1, "下一页");        string pagelast = (pageindex == pagecount || count == 0) ? String.Empty : String.Format(pagelastformat, pagecount, "尾页");        page.Add("count", count);        page.Add("pagesize", pagesize);        page.Add("pagecount", pagecount);        page.Add("pageindex", pageindex);        page.Add("pagenum", pagenum);        page.Add("pagefirst", pagefirst);        page.Add("pageprev", pageprev);        page.Add("pagenext", pagenext);        page.Add("pagelast", pagelast);        return Sql(DBSql.Page(table, pagesize, pageindex, "id", where, order));      }      return Select(table, top, field, where, order, list);    }    /// <summary>    /// 筛选    /// </summary>    public static DataSet Select(string table)    {      if (table.Contains(" "))        return Sql(table);      else        return Select(table, 0, String.Empty, String.Empty, String.Empty, null);    }    /// <summary>    /// 筛选    /// </summary>    public static DataSet Select(string table, int top, string field, string where, string order, List<string> list = null)    {      DBHelper db = new DBHelper();      if (list != null)      {        for (int i = 0; i < list.Count; i++)        {          db.ParameterAdd("@" + i, list[i]);        }      }      DataSet ds = db.ExecuteDataSet(DBSql.Select(table, top, field, where, order));      db.Dispose();      return ds;    }    #endregion    #region 提取数据 动态类型    /// <summary>    /// 筛选    /// </summary>    public static dynamic Query(Dictionary<string, object> d)    {      string table = d["table"].ToString();      int top = d.ContainsKey("top") ? Convert.ToInt32(d["top"]) : 0;      string field = d.ContainsKey("field") ? d["field"].ToString() : String.Empty;      string where = d.ContainsKey("where") ? d["where"].ToString() : String.Empty;      List<string> list = d.ContainsKey("list") ? (List<string>)d["list"] : null;      string order = d.ContainsKey("order") ? d["order"].ToString() : String.Empty;      return Query(table, top, field, where, order, list);    }        /// <summary>    /// 筛选    /// </summary>    public static dynamic Query(Dictionary<string, object> d, out dynamic dy)    {      string table = d["table"].ToString();      int top = d.ContainsKey("top") ? Convert.ToInt32(d["top"]) : 0;      string field = d.ContainsKey("field") ? d["field"].ToString() : String.Empty;      string where = d.ContainsKey("where") ? d["where"].ToString() : String.Empty;      List<string> list = d.ContainsKey("list") ? (List<string>)d["list"] : null;      string order = d.ContainsKey("order") ? d["order"].ToString() : String.Empty;      dy = new System.Dynamic.ExpandoObject();      if (d.ContainsKey("pagesize"))      {        int count = Count(table);        int pagesize = Convert.ToInt32(d["pagesize"]);        int pagecount = (count + pagesize - 1) / pagesize;        var dpageindex = d["pageindex"];        int pageindex = dpageindex == null ? 1 : Convert.ToInt32(dpageindex);        string pageformat = d.ContainsKey("pageformat") ? d["pageformat"].ToString() : "<a href=\"?page={0}\">{1}</a>";        string pagefirstformat = d.ContainsKey("pagefirstformat") ? d["pagefirstformat"].ToString() : pageformat;        string pageprevformat = d.ContainsKey("pageprevformat") ? d["pageprevformat"].ToString() : pageformat;        string pagenextformat = d.ContainsKey("pagenextformat") ? d["pagenextformat"].ToString() : pageformat;        string pagelastformat = d.ContainsKey("pagelastformat") ? d["pagelastformat"].ToString() : pageformat;        StringBuilder pagenum = new StringBuilder();        int j = (pageindex - 5 < 1) ? (1) : (pageindex - 5);        int k = (pageindex + 5 > pagecount) ? (pagecount) : (pageindex + 5);        for (int i = j; i < k + 1; i++)        {          if (pageindex == i)            pagenum.AppendFormat("<span>{0}</span>", i);          else            pagenum.AppendFormat(pageformat, i, i);        }        string pagefirst = pageindex == 1 ? String.Empty : String.Format(pagefirstformat, "1", "首页");        string pageprev = pageindex == 1 ? String.Empty : String.Format(pageprevformat, pageindex - 1, "上一页");        string pagenext = (pageindex == pagecount || count == 0) ? String.Empty : String.Format(pagenextformat, pageindex + 1, "下一页");        string pagelast = (pageindex == pagecount || count == 0) ? String.Empty : String.Format(pagelastformat, pagecount, "尾页");        dy.count = count;        dy.pagesize = pagesize;        dy.pagecount = pagecount;        dy.pageindex = pageindex;        dy.pagenum = pagenum;        dy.pagefirst = pagefirst;        dy.pageprev = pageprev;        dy.pagenext = pagenext;        dy.pagelast = pagelast;        return SqlReader(DBSql.Page(table, pagesize, pageindex, "id", where, order));      }      return Query(table, top, field, where, order, list);    }    /// <summary>    /// 筛选    /// </summary>    public static dynamic Query(string table)    {      return Query(table, 0, String.Empty, String.Empty, String.Empty, null);    }    /// <summary>    /// 筛选    /// </summary>    public static dynamic Query(string table, int top, string field, string where, string order, List<string> list = null)    {      DBHelper db = new DBHelper();      if (list != null)      {        for (int i = 0; i < list.Count; i++)        {          db.ParameterAdd("@" + i, list[i]);        }      }      dynamic d = db.ExecuteReader(DBSql.Select(table, top, field, where, order));      db.Dispose();      return d;    }    #endregion    #region 提取数据 sql语句    /// <summary>    /// 筛选    /// </summary>    public static dynamic SqlReader(string sql, List<string> list = null)    {      DBHelper db = new DBHelper();      if (list != null)      {        for (int i = 0; i < list.Count; i++)        {          db.ParameterAdd("@" + i, list[i]);        }      }      dynamic dy = db.ExecuteReader(sql);      db.Dispose();      return dy;    }    /// <summary>    /// 筛选    /// </summary>    public static DataSet Sql(string sql, List<string> list = null)    {      DBHelper db = new DBHelper();      if (list != null)      {        for (int i = 0; i < list.Count; i++)        {          db.ParameterAdd("@" + i, list[i]);        }      }      DataSet ds = db.ExecuteDataSet(sql);      db.Dispose();      return ds;    }    #endregion    #region 添加数据    /// <summary>    /// 添加    /// </summary>    public static int Insert(string table, Dictionary<string, object> field)    {      DBHelper db = new DBHelper();      StringBuilder sql = new StringBuilder();      string key = String.Empty;      string value = String.Empty;      foreach (KeyValuePair<string, object> s in field)      {        key += String.Format("[{0}],", s.Key);        value += String.Format("@{0},", s.Key);        db.ParameterAdd("@" + s.Key, s.Value);      }      sql.AppendFormat("insert into [{0}](", table);      sql.Append(key.TrimEnd(','));      sql.Append(")values(");      sql.Append(value.TrimEnd(','));      sql.Append(")");      int row = db.ExecuteNonQuery(sql.ToString());      db.Dispose();      return row;    }    #endregion    #region 更新数据    /// <summary>    /// 更新    /// </summary>    public static int Update(string table, Dictionary<string, object> field, string where, List<string> list = null)    {      DBHelper db = new DBHelper();      StringBuilder sql = new StringBuilder();      sql.AppendFormat("update [{0}] set ", table);      foreach (KeyValuePair<string, object> s in field)      {        sql.AppendFormat("[{0}]=@{0},", s.Key);        db.ParameterAdd("@" + s.Key, s.Value);      }      sql.Remove(sql.Length - 1, 1);      if (where != "")      {        sql.AppendFormat(" where {0}", where);      }      if (list != null)      {        for (int i = 0; i < list.Count; i++)        {          db.ParameterAdd("@" + i, list[i]);        }      }      int row = db.ExecuteNonQuery(sql.ToString());      db.Dispose();      return row;    }    #endregion    #region 删除数据    /// <summary>    /// 删除    /// </summary>    public static int Delete(string table, string where, List<string> list = null)    {      DBHelper db = new DBHelper();      if (list != null)      {        for (int i = 0; i < list.Count; i++)        {          db.ParameterAdd("@" + i, list[i]);        }      }      int row = db.ExecuteNonQuery(DBSql.Delete(table, where));      db.Dispose();      return row;    }    #endregion    #region 统计数据    /// <summary>    /// 统计    /// </summary>    public static int Count(string table)    {      return Count(table, String.Empty, null);    }    /// <summary>    /// 统计    /// </summary>    public static int Count(string table, string where, List<string> list = null)    {      DBHelper db = new DBHelper();      if (list != null)      {        for (int i = 0; i < list.Count; i++)        {          db.ParameterAdd("@" + i, list[i]);        }      }      int row = Convert.ToInt32(db.ExecuteScalar(DBSql.Count(table, where)).ToString());      db.Dispose();      return row;    }    #endregion    #region 是否存在数据    /// <summary>    /// 是否存在    /// </summary>    public static bool Exists(string table, string where, List<string> list = null)    {      if (Count(table, where, list) > 0)      {        return true;      }      return false;    }    #endregion    #region 返回单行数据    /// <summary>    /// 单行数据    /// </summary>    public static string Row(string table, string field, string where, List<string> list = null)    {      DBHelper db = new DBHelper();      if (list != null)      {        for (int i = 0; i < list.Count; i++)        {          db.ParameterAdd("@" + i, list[i]);        }      }      string row = db.ExecuteScalar(DBSql.Select(table, 1, field, where, String.Empty)).ToString();      db.Dispose();      return row;    }    #endregion  }}

  

user.cshtml 

@using System.Data;@using ST.CMS.Database;@using ST.Framework;@{  Page.Title = "用户管理";  var action = Request["action"];  var id = Request["id"];  var db = DUser.Form(action, id);    if (action == "delete")  {    DUser.Delete(id);    Response.Redirect("user");  }    if (IsPost)  {    Dictionary<string, object> d = new Dictionary<string, object>();    d.Add("username", Request["st_username"]);    d.Add("password", Request["st_password"]);    d.Add("userstyle", "fixed skin-blue-light");    if (action == "update")    {      DUser.Update(d, id);    }    else    {      DUser.Insert(d);    }    Response.Redirect("user");  }}<!-- Main content --><section >  <div >    <div >      <div >        <a href="user" ><i ></i> 会员首页</a>        <a href="?action=insert" ><i ></i> 新建会员</a>      </div>      <div >        <div >          <h3 >用户</h3>          <div >            <button data-widget="collapse"><i ></i></button>          </div>        </div>        <div >          <ul >            @foreach (DataRowView it in DUser.List())            {              <li><a href="?action=update&id=@it["id"]"><i ></i> @it["username"]</a></li>            }          </ul>        </div><!-- /.box-body -->      </div><!-- /. box -->    </div><!-- /.col -->    <div >      @if (action == null)      {        <div >          <div >            <!-- small box -->            <div >              <div >                <h3><i ></i> 用户首页</h3>              </div>              <a href="?action=insert" >新建用户 <i ></i></a>            </div>          </div><!-- ./col -->        </div><!-- /.row -->      }      else      {        <div >          <div >            <h3 ><span>@((action == "update") ? "更新" : "添加")</span>用户</h3>          </div>          <form method="post" action="">            <div >              <div >                <label >用户名称</label>                <div >                  <input type="text" name="st_username" value="@db[0]["username"]" placeholder="用户名称">                </div>              </div>              <div >                <label >用户密码</label>                <div >                  <input type="text" name="st_password" value="@db[0]["password"]" placeholder="用户密码">                </div>              </div>            </div><!-- /.box-body -->            <div >              <div >                <button type="reset" ><i ></i> 重置</button>                <button type="submit" ><i ></i> @((action == "update") ? "更新" : "添加")</button>              </div>              @{if (action == "update")              {<a href="?action=delete&id=@id" onclick="return confirm('确定将此记录删除?')"><i ></i> 删除</a>}}            </div><!-- /.box-footer -->          </form>        </div><!-- /.box -->      }    </div><!-- /.col -->  </div><!-- /.row --></section>

 

 

后台风格管理

 

模型管理

 

栏目管理

 

内容管理

 

 

(QQ群:7469493)

 

下载地址:http://pan.baidu.com/s/1i3i0BpN

后台地址:admin/index

账号:admin

密码:1