string conn = ConfigurationManager.ConnectionStrings[\].ConnectionString;
//取得DSN字符
SqlConnection con = new SqlConnection(conn);//连接字符串 con.Open();
SqlCommand cmd = new SqlCommand(\, con); cmd.CommandType = CommandType.StoredProcedure;
SqlParameter name = cmd.Parameters.Add(\, SqlDbType.Char, 200); //调用并设置存储过程参数
name.Value = username.Trim(); int r = cmd.ExecuteNonQuery(); }
protected void addClassNum() {
string username = (string)Session[\];
string conn = ConfigurationManager.ConnectionStrings[\].ConnectionString;
//取得DSN字符
SqlConnection con = new SqlConnection(conn);//连接字符串 con.Open();
SqlCommand cmd = new SqlCommand(\, con); cmd.CommandType = CommandType.StoredProcedure;
SqlParameter name = cmd.Parameters.Add(\, SqlDbType.Char, 200); //调用并设置存储过程参数
name.Value = ClassName.SelectedItem.Text.Trim(); int r = cmd.ExecuteNonQuery(); }
protected void addArticle() {
int hl = 0; int HL = 0;
string classname = ClassName.SelectedItem.Text.Trim(); if (Headline.Checked) {
hl = 1; }
else hl = 0;
if (HighLight.Checked) {
HL = 1; }
else HL = 0;
string title = Title.Text;
//获得新闻标题
string content = Body.Text;//获得新闻内容 content = Server.HtmlDecode(content); string at = Author.Text;
//获得作者名
//获得是否新闻醒目
//获得分类名
//获得是否是头条新闻
//发表文章
//建立命令
//执行命令
//建立命令
string sr = Source.Text; //获得来源 try {
string
conn
=
ConfigurationManager.ConnectionStrings[\].ConnectionString; //取得DSN字符 SqlConnection con = new SqlConnection(conn);//连接字符串 con.Open();
SqlCommand cmd = new SqlCommand(\, con); cmd.CommandType = CommandType.StoredProcedure;
SqlParameter cl = cmd.Parameters.Add(\, SqlDbType.Char, 200);
//调用并设置存储过程参数 cl.Value = classname;
SqlParameter tl = cmd.Parameters.Add(\, SqlDbType.NVarChar, 400); tl.Value = title;
SqlParameter cn = cmd.Parameters.Add(\, SqlDbType.NText); cn.Value = content;
SqlParameter hd = cmd.Parameters.Add(\, SqlDbType.Int); hd.Value = hl;
SqlParameter hil = cmd.Parameters.Add(\, SqlDbType.Int); hil.Value = HL;
SqlParameter author = cmd.Parameters.Add(\, SqlDbType.Char, 400); author.Value = at;
SqlParameter source = cmd.Parameters.Add(\, SqlDbType.Char, 400); source.Value = sr;
SqlParameter username = cmd.Parameters.Add(\, SqlDbType.Char, 400); username.Value = (string)Session[\];
SqlParameter popedom = cmd.Parameters.Add(\, SqlDbType.Char, 100); popedom.Value = Session[\];
SqlParameter checkup = cmd.Parameters.Add(\, SqlDbType.Int); string ul = (string)Session[\]; if (ul.Trim() == \系统管理员\) {
checkup.Value = 1; }
else checkup.Value = (int)Session[\];
int d = cmd.ExecuteNonQuery(); if (d > 0) {
myLabel.Text = \添加新闻成功\; con.Close(); } else {
//添加新闻
//建立命令
myLabel.Text = \添加新闻错误\; con.Close(); } }
catch (Exception e) {
Console.WriteLine(\ + e.Message); //出错处理 } }
protected void upload_Click(object sender, System.EventArgs e) {
if (FileUp.PostedFile.ContentLength != 0) { if
(FileUp.PostedFile.ContentType
==
\
||
FileUp.PostedFile.ContentType == \ || FileUp.PostedFile.ContentType == \) //设置上传文件类型 {
string filename = FileUp.PostedFile.FileName; int i = filename.Length;
filename = filename.Remove(0, i - 4);
string s = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +DateTime.Now.Hour.ToString()+ DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
string d = Server.MapPath(\) + s + filename; //设置文件名 FileUp.PostedFile.SaveAs(d); Label1.Text = \上传成功\; }
else Label1.Text = \只能上传图形文件\; }
else Label1.Text = \请选择上传文件\; }
protected void btadd_Click(object sender, EventArgs e) {
if (Page.IsValid) {
addArticle();
string userclass = (string)Session[\]; int i = (int)Session[\];
if (userclass.Trim() == \系统管理员\ || i == 1) {
addhints(); addClassNum(); }
//保存文件
Body.Text += \ + s + filename + \; //更改新闻内容
//取得文件名
} } }
4.3 新闻或其评论删除功能
管理员只要拥有相应权限就可以通过此页面进入新闻修改页面、删除某条
新闻或删除其有关评论。
其功能界面如图4-3-1所示:
图4-3-1新闻管理界面图
其部分代码如下:
public partial class admin_admin_check : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
string username;
username = (string)Session[\]; if (username.Trim() != \) {
getArticle(); }
else Page.Visible = false; }
}
protected void getArticle() { try {
string
con
=
ConfigurationManager.ConnectionStrings[\].ConnectionString; SqlConnection conn = new SqlConnection(con);//连接字符串 conn.Open();
SqlDataAdapter myCommand = new SqlDataAdapter(); //创建SqlDataAdapter 类 myCommand.SelectCommand = new SqlCommand(\, conn); myCommand.SelectCommand.CommandType = CommandType.StoredProcedure; DataSet ds = new DataSet(); //建立并填充数据集 myCommand.Fill(ds, \); MyDataGrid.DataSource = ds; MyDataGrid.DataBind();
lblCurrentIndex.Text = \第\ + ((Int32)MyDataGrid.CurrentPageIndex + 1) + \页\;
lblPageCount.Text = \共\ + MyDataGrid.PageCount + \页\; conn.Close(); }
catch (SqlException e) {
Console.WriteLine(\ + e.Message); //出错处理 } }
protected void passArticle(object a) {
string con = ConfigurationManager.ConnectionStrings[\].ConnectionString; SqlConnection conn = new SqlConnection(con); conn.Open();
SqlCommand delAdmin = new SqlCommand(\, conn); delAdmin.CommandType = CommandType.StoredProcedure;
SqlParameter id = delAdmin.Parameters.Add(\, SqlDbType.BigInt); id.Value = a; if (r > 0) {
myLabel.Text = \通过审核!\; conn.Close();
MyDataGrid.EditItemIndex = -1; //
if(MyDataGrid.PageCount/MyDataGrid.PageSize)
if (MyDataGrid.CurrentPageIndex > 0)
//获得参数的值
int r = delAdmin.ExecuteNonQuery(); //删除
//建立命令
//建立连接
//通过审核
//绑定