newsManager.aspx.cs
上传用户:xy99169
上传日期:2022-08-03
资源大小:139k
文件大小:4k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

ASP/ASPX

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.SqlClient;
  12. using System.Web.Configuration;
  13. public partial class newsManager : System.Web.UI.Page
  14. {
  15.     protected void Page_Load(object sender, EventArgs e)
  16.     {
  17.         if (!IsPostBack)
  18.         {
  19.             bindNews();
  20.         }
  21.     }
  22.     protected void bindNews()
  23.     {
  24.         string connStr = WebConfigurationManager.ConnectionStrings["news"].ConnectionString;
  25.             SqlConnection conn = new SqlConnection(connStr);
  26.             string cmdStr = "select * from news";
  27.             SqlCommand cmd = new SqlCommand();
  28.             cmd.CommandText = cmdStr;
  29.             cmd.Connection = conn;
  30.             try
  31.             {
  32.                 conn.Open();
  33.                 SqlDataReader sdr = cmd.ExecuteReader();
  34.                 
  35.                 listNews.DataSource = sdr;
  36.                 //listNews.DataTextField = "newsTitle";
  37.                 //listNews.DataValueField = "newsID";
  38.                 listNews.DataBind();
  39.                 
  40.             }
  41.             catch (SqlException ex)
  42.             {
  43.                 Response.Write(ex.Message);
  44.             }
  45.             finally
  46.             {
  47.                 conn.Close();
  48.             }
  49.         }
  50.     protected void Button2_Click(object sender, EventArgs e)
  51.     {
  52.         string connStr = WebConfigurationManager.ConnectionStrings["news"].ConnectionString;
  53.         SqlConnection conn = new SqlConnection(connStr);
  54.         string cmdStr = "Insert news(newsTitle,newsContent) Values(@title,@content)";
  55.         SqlCommand cmd = new SqlCommand();
  56.         cmd.CommandText = cmdStr;
  57.         cmd.Connection = conn;
  58.         cmd.Parameters.AddWithValue("@title", TextBox1.Text);
  59.         cmd.Parameters.AddWithValue("@content", TextBox2.Text);
  60.         try
  61.         {
  62.             conn.Open();
  63.             cmd.ExecuteNonQuery();
  64.         }        
  65.         catch (SqlException ex)
  66.         {
  67.             Response.Write(ex.Message);
  68.         }
  69.         finally
  70.         {
  71.             conn.Close();
  72.         }
  73.         bindNews();
  74.         TextBox1.Text = "";
  75.         TextBox2.Text = "";
  76.     }
  77.     protected void listNews_SelectedIndexChanged(object sender, EventArgs e)
  78.     {
  79.         string id = listNews.SelectedValue;
  80.         string connStr = WebConfigurationManager.ConnectionStrings["news"].ConnectionString;
  81.         SqlConnection conn = new SqlConnection(connStr);
  82.         string cmdStr = "select * from news where newsid="+id;
  83.         SqlCommand cmd = new SqlCommand();
  84.         cmd.CommandText = cmdStr;
  85.         cmd.Connection = conn;
  86.         
  87.         try
  88.         {
  89.             conn.Open();
  90.             SqlDataReader sdr = cmd.ExecuteReader();
  91.             if (sdr.Read())
  92.             {
  93.                 Label1.Text = "新闻编号:" + sdr["newsID"].ToString() + "<br/>";
  94.                 Label1.Text += "新闻标题:" + sdr["newsTitle"].ToString() + "<br/>";
  95.                 Label1.Text += "新闻内容:" + sdr["newsContent"].ToString() + "<br/>";
  96.             }
  97.             
  98.         }
  99.         catch (SqlException ex)
  100.         {
  101.             Response.Write(ex.Message);
  102.         }
  103.         finally
  104.         {
  105.             conn.Close();
  106.         }
  107.     }
  108.     protected void Button1_Click(object sender, EventArgs e)
  109.     {
  110.         string id = listNews.SelectedValue;
  111.         string connStr = WebConfigurationManager.ConnectionStrings["news"].ConnectionString;
  112.         SqlConnection conn = new SqlConnection(connStr);
  113.         string cmdStr = "Delete news where newsid="+id;
  114.         SqlCommand cmd = new SqlCommand();
  115.         cmd.CommandText = cmdStr;
  116.         cmd.Connection = conn;
  117.         try
  118.         {
  119.             conn.Open();
  120.             cmd.ExecuteNonQuery();
  121.         }
  122.         catch (SqlException ex)
  123.         {
  124.             Response.Write(ex.Message);
  125.         }
  126.         finally
  127.         {
  128.             conn.Close();
  129.         }
  130.         bindNews();
  131.         Label1.Text = "";
  132.     }
  133. }