DAL层
- C# code
using System;using System.Data;using System.Text;using System.Data.SqlClient;using DBUtility;//Please add referencesnamespace DAL{ /// <summary> /// 根据用户名获取用户ID /// </summary> public DataSet GetSessonUserID(string UserName) { StringBuilder strsql = new StringBuilder(); strsql.Append("select UserID "); strsql.Append("from Accounts_Users "); if (UserName!="") { strsql.Append("where " + UserName); } return SQLHelper.Query(strsql.ToString()); } /// <summary> /// 根据用户名获取用户类别ID(RoleID) /// </summary> public DataSet GetSessionRoleID(string UserName) { StringBuilder strsql = new StringBuilder(); strsql.Append("select RoleID "); strsql.Append("from Accounts_Users "); if (UserName.Trim()!="") { strsql.Append("where " + UserName); } return SQLHelper.Query(strsql.ToString()); } /// <summary> /// 获得数据列表 /// </summary> public DataSet GetList(string strWhere) { StringBuilder strSql = new StringBuilder(); strSql.Append("select UserID,UserName,UserPassWord,TrueName,Sex,Age,BirthDay,Phone,Email,EmployeeID,DepartmentID,UserType "); strSql.Append(" FROM Accounts_Users "); if (strWhere.Trim() != "") { strSql.Append(" where " + strWhere); } return SQLHelper.Query(strSql.ToString()); }
web层
- C# code
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using DBUtility;using Model;using DAL;using BLL;using System.Data;using System.Data.SqlClient;using System.Configuration;public partial class Admin_AddNews : System.Web.UI.UserControl{ protected void Page_Load(object sender, EventArgs e) { if (Session["UserName"] == null) { Response.Write("<script>alert('您还没有登录!')</script>"); Response.Redirect("/Admin/Login.aspx"); } if (!IsPostBack) { BindNewsTypeBind(); } } private void BindNewsTypeBind() { BLL.Accounts_NewsType bll = new BLL.Accounts_NewsType(); DataSet ds = bll.GetAllList(); this.NewsType.DataSource = ds.Tables[0]; this.NewsType.DataTextField = "NewsType"; this.NewsType.DataValueField = "NewsTypeID"; this.NewsType.DataBind(); } protected void NewsType_SelectedIndexChanged(object sender, EventArgs e) { BLL.Accounts_NewsType bll = new BLL.Accounts_NewsType(); DataSet ds = bll.GetList(NewsType.SelectedItem.Value); if (ds.Tables[0].Rows.Count>0) { this.NewsType.DataSource = ds; this.NewsType.DataTextField = "NewsType"; this.NewsType.DataValueField = "NewsTypeID"; this.NewsType.DataBind(); } //string NewsTypeID = NewsType.SelectedValue; //BindNewsTypeBind(); } protected void SaveBtn_Click(object sender, EventArgs e) { BLL.Accounts_Users UsersBll = new BLL.Accounts_Users(); BLL.Accounts_News bll = new BLL.Accounts_News(); ImageUpLoad.SaveAs(Server.MapPath("../ImageUpLoad/"+ImageUpLoad.FileName)); try { if (TitleText.Text.Trim() == "" && ImageUpLoad.FileName != "" && CKEditorControl1.Text != "") { Response.Write("<script>alert('请输入新闻标题!')</script>"); } else if (TitleText.Text.Trim() != "" && ImageUpLoad.FileName == "" && CKEditorControl1.Text != "") { Response.Write("<script>alert('请选择一张图片作为标题图片!')</script>"); } else if (TitleText.Text.Trim() != "" && ImageUpLoad.FileName != "" && CKEditorControl1.Text == "") { Response.Write("<script>alert('请输入新闻内容!')</script>"); } else if (TitleText.Text.Trim() == "" && ImageUpLoad.FileName == "" && CKEditorControl1.Text == "") { Response.Write("<script>alert('请填写您要发表的新闻标题、标题图片、新闻内容,并且选择对应的新闻类别!')</script>"); } else if (TitleText.Text.Trim() != "" && ImageUpLoad.FileName != "" && CKEditorControl1.Text != "") { Model.Accounts_News model = new Model.Accounts_News(); model.NewsTitle = TitleText.Text; model.NewsPicture = "../ImageUpLoad/" + ImageUpLoad.FileName; model.NewsTypeID = Convert.ToInt32(NewsType.SelectedItem.Value); model.NewsText = CKEditorControl1.Text; model.NewsDateTime = DateTime.Now; model.UserID = Convert.ToInt32(UsersBll.GetSessonUserID((string)Session["UserName"]));//从Session中获取当前登录用户的用户名 model.RoleID = Convert.ToInt32(UsersBll.GetSessionRoleID((string)Session["Description"]));//从Session中获取当前登录用户的组 bll.Add(model); Response.Write("<script>alert('发布成功!')</script>"); } } catch (Exception ex) { Response.Write("错误:"+ex); } }}