long

asp.net中对Sql Server 的操作

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace WebNews.admin
{
 /// <summary>
 /// admin_article 的摘要说明。
 /// </summary>
 public class admin_article : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Label myLabel;
  protected System.Web.UI.WebControls.DropDownList search;
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.WebControls.Label PageList;
  protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
  protected System.Web.UI.WebControls.TextBox keyword;
  protected System.Web.UI.WebControls.LinkButton LinkButton1;
  protected System.Web.UI.WebControls.LinkButton btnFirst;
  protected System.Web.UI.WebControls.LinkButton LinkButton2;
  protected System.Web.UI.WebControls.LinkButton Linkbutton3;
  protected System.Web.UI.WebControls.LinkButton Linkbutton4;
  protected System.Web.UI.WebControls.Label lblCurrentIndex;
  protected System.Web.UI.WebControls.Label lblPageCount;
  protected System.Web.UI.WebControls.DataGrid MyDataGrid;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!Page.IsPostBack)
   {
     getArticle();
   
   }

  }
  private void getArticle()      //取得Article数据
  {
   try
   { 
    string con=ConfigurationSettings.AppSettings["dsn"];
    SqlConnection  conn = new SqlConnection(con);//连接字符串
    conn.Open();
        
    SqlDataAdapter myCommand = new SqlDataAdapter();  //创建SqlDataAdapter 类
     myCommand.SelectCommand=new SqlCommand("sp_selArticleAllCk",conn);
    myCommand.SelectCommand.CommandType=CommandType.StoredProcedure ;
      DataSet ds=new DataSet();  //建立并填充数据集
    myCommand.Fill(ds,"Article");
      
    MyDataGrid.DataSource=ds;    //绑定
    MyDataGrid.DataBind();
   
    lblCurrentIndex.Text="第"+((Int32)MyDataGrid.CurrentPageIndex+1)+"页";
    lblPageCount.Text="/共"+MyDataGrid.PageCount+"页";

    conn.Close();
   }
   catch(SqlException e)
   {
    Console.WriteLine("Exception in Main: " + e.Message); //出错处理
   }
  
  }

  private void searchTitle()        //搜索标题
  {
   try
   { 
    string con=ConfigurationSettings.AppSettings["dsn"];
    SqlConnection  conn = new SqlConnection(con);//连接字符串
    conn.Open();
        
    SqlDataAdapter myCommand = new SqlDataAdapter();  //创建SqlDataAdapter 类
     myCommand.SelectCommand=new SqlCommand("sp_searchCkArticleByTitle",conn);
    myCommand.SelectCommand.CommandType=CommandType.StoredProcedure ;
   SqlParameter title=myCommand.SelectCommand.Parameters.Add("@title",SqlDbType.NVarChar ,500);
   title.Value=Request["keyword"] ;

    DataSet ds=new DataSet();  //建立并填充数据集
    myCommand.Fill(ds,"Articl");
      
    MyDataGrid.DataSource=ds;    //绑定
    MyDataGrid.DataBind();
    

    conn.Close();
   }
   catch(SqlException e)
   {
    Console.WriteLine("Exception in Main: " + e.Message); //出错处理
   }
  
  }

  private void searchContent()       //搜索内容
  {
   try
   { 
    string con=ConfigurationSettings.AppSettings["dsn"];
    SqlConnection  conn = new SqlConnection(con);//连接字符串
    conn.Open();
        
    SqlDataAdapter myCommand = new SqlDataAdapter();  //创建SqlDataAdapter 类
     myCommand.SelectCommand=new SqlCommand("sp_searchCkArticleByContent",conn);
    myCommand.SelectCommand.CommandType=CommandType.StoredProcedure ;
    SqlParameter content=myCommand.SelectCommand.Parameters.Add("@content",SqlDbType.NText );
    content.Value=Request["keyword"].Trim() ;
    
    DataSet ds=new DataSet();  //建立并填充数据集
    myCommand.Fill(ds,"Article");
      
    MyDataGrid.DataSource=ds;    //绑定
    MyDataGrid.DataBind();
   

    conn.Close();
   }
   catch(SqlException e)
   {
    Console.WriteLine("Exception in Main: " + e.Message); //出错处理
   }
  
  }
  public string show(object a,object b,object c)       //设置权限
  {
     string  dr=" <img src=../pic/canedit.gif align=absMiddle><a href=admin_articleEdit.aspx?articleid="+b+">"+a+"</a>" ;
     string de="<img src=../pic/cantedit.gif align=absMiddle>"+a;
     string g=(string)Session["userclass"];
     string d=(string)Session["classname"];
     string f=(string)c;
   if(g.Trim()=="系统管理员") 
   {
    return dr;
   }
   else
   {
    if((int)Session["chgnews"]==1&& d.Trim()==f.Trim())
     return dr;
    else return de;
   }

  }
 
  public string show(object a,object d)
 {  

   string b="<img src=../pic/canedit.gif align=absMiddle><a href=admin_remark.aspx?articleid="+a+"&classname="+d+"   target=_self>评论</a>";
   string c="<img src=../pic/cantedit.gif align=absMiddle>评论";
   string g=(string)Session["userclass"];
   string e=(string)Session["classname"];
   string f=(string)d;
   if(g.Trim()=="系统管理员")
   {
    return b;
   }
   else
   {
    if((int)Session["remark"]==1 && e.Trim()==f.Trim())
      return b;
    else  return c;
     }

                   
  }

  private void delarticle(object a)
   {
   try
   {
    string con=ConfigurationSettings.AppSettings["dsn"];
    SqlConnection conn=new SqlConnection(con);   //建立连接
    conn.Open();
    SqlCommand delAdmin=new SqlCommand("sp_delArticle",conn);   //建立命令
    delAdmin.CommandType=CommandType.StoredProcedure;

    SqlParameter id=delAdmin.Parameters.Add("@articleid",SqlDbType.BigInt);
    id.Value=a ;     //获得参数的值
                       
    int r=delAdmin.ExecuteNonQuery();   //删除
    if(r>0)
    {
     myLabel.Text="删除成功!";
     conn.Close();
     MyDataGrid.EditItemIndex = -1;
     int d=MyDataGrid.PageCount % MyDataGrid.PageSize;
     if(MyDataGrid.CurrentPageIndex>0)
     {

      MyDataGrid.CurrentPageIndex= MyDataGrid.CurrentPageIndex-1;
     }
     
     getArticle();

    }
    else
    {
     myLabel.Text="删除错误!";
     conn.Close();
     MyDataGrid.EditItemIndex = -1;
     int d=MyDataGrid.PageCount % MyDataGrid.PageSize;
     if(MyDataGrid.CurrentPageIndex>0)
     {

      MyDataGrid.CurrentPageIndex= MyDataGrid.CurrentPageIndex-1;
     }
     getArticle();

    }
   }
   catch(SqlException e)
   {
    Console.WriteLine("Exception in Main: " + e.Message); //出错处理
   }
   }

  private void delnum(string dr)       //删除管理员发表文章数
  {
   try
   {
    string username=(string)Session["username"];
    
    string conn=ConfigurationSettings.AppSettings["dsn"]; //取得DSN字符
   
    SqlConnection con = new SqlConnection(conn);//连接字符串
    con.Open();
   
    SqlCommand cmd=new SqlCommand("sp_delNum",con);   //建立命令
    cmd.CommandType=CommandType.StoredProcedure;
   
    SqlParameter name=cmd.Parameters.Add("@username",SqlDbType.Char,200);  //调用并设置存储过程参数
    name.Value=dr.Trim();
    int r=cmd.ExecuteNonQuery();    //执行命令
    con.Close();
   }
   catch(SqlException e)
   {
    Console.WriteLine("Exception in Main: " + e.Message); //出错处理
   }
  }
 


  private void delClassNum(string d)    //删除分类发表文章数
  {
    try
   {
    string conn=ConfigurationSettings.AppSettings["dsn"]; //取得DSN字符
   
    SqlConnection con = new SqlConnection(conn);//连接字符串
    con.Open();
   
    SqlCommand cmd=new SqlCommand("sp_delClassNum",con);   //建立命令
    cmd.CommandType=CommandType.StoredProcedure;
   
    SqlParameter cls=cmd.Parameters.Add("@className",SqlDbType.Char,200);  //调用并设置存储过程参数
   cls.Value =d.Trim();
    int r=cmd.ExecuteNonQuery();
    con.Close();
   }
    catch(SqlException e)
    {
     Console.WriteLine("Exception in Main: " + e.Message); //出错处理
    }
  }


  public void PagerButtonClick(Object sender, EventArgs e)
  {
   //获得LinkButton的参数值
   string arg = ((LinkButton)sender).CommandArgument;

   switch(arg)
   {
    case ("next"):
     if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
      MyDataGrid.CurrentPageIndex ++;
     break;
    case ("prev"):
     if (MyDataGrid.CurrentPageIndex > 0)
      MyDataGrid.CurrentPageIndex --;
     break;
    case ("last"):
     MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
     break;
    case("first"):
     MyDataGrid.CurrentPageIndex =0;
     break;
   }
   getArticle();
  }
  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  public void MyDataGrid_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {   object b=this.MyDataGrid.DataKeys[e.Item.ItemIndex];
     string c=(string)Session["userclass"];
   string del=(string)e.Item.Cells[3].Text;
   string g=(string)e.Item.Cells[1].Text;
   
   
   string f=(string)Session["classname"];
   if(c.Trim()=="系统管理员")
   { 
    Response.Write(g);
    delClassNum(g);
    delarticle(b);
    delnum(del);
    
   }
   else
   {
     if((int)Session["chgnews"]==1 && g.Trim()==f.Trim())
      { 
     
      delClassNum(g);
     
        delarticle(b);
      delnum(del);
      

      }
      else myLabel.Text="你无权删除此新闻";
            }


  }

  private void LinkButton1_Click(object sender, System.EventArgs e)
  {
    if(Page.IsValid)
     {
      if(search.SelectedIndex==0)
      {
       searchTitle();
      }
      else searchContent();
     }

  }
  public void MyDataGrid_SelectedIndexChanged(object sender,System.EventArgs e)
  {
          
   getArticle();
  }

 
 }
}

分类:

技术点:

相关文章: