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

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 news : System.Web.UI.Page
  14. {
  15.     protected void Page_Load(object sender, EventArgs e)
  16.     {
  17.         if (!IsPostBack)
  18.         {
  19.             string connStr = WebConfigurationManager.ConnectionStrings["news"].ConnectionString;
  20.             SqlConnection conn = new SqlConnection(connStr);
  21.             string cmdStr = "select * from news";
  22.             SqlCommand cmd = new SqlCommand();
  23.             cmd.CommandText = cmdStr;
  24.             cmd.Connection = conn;
  25.             try
  26.             {
  27.                 conn.Open();
  28.                 SqlDataReader sdr = cmd.ExecuteReader();
  29.                 while (sdr.Read())
  30.                 {
  31.                     Response.Write(sdr[0].ToString() + "<br/>");
  32.                     Response.Write(sdr[1].ToString() + "<br/>");
  33.                     Response.Write(sdr[2].ToString() + "<br/>");
  34.                 }
  35.             }
  36.             catch (SqlException ex)
  37.             {
  38.                 Response.Write(ex.Message);
  39.             }
  40.             finally
  41.             {
  42.                 conn.Close();
  43.             }
  44.         }
  45.     }
  46.     protected void Button1_Click(object sender, EventArgs e)
  47.     {
  48.         string connStr = WebConfigurationManager.ConnectionStrings["news"].ConnectionString;
  49.         SqlConnection conn = new SqlConnection(connStr);
  50.         string cmdStr = "select * from news where newsID=@id";
  51.         SqlCommand cmd = new SqlCommand();
  52.         cmd.CommandText = cmdStr;
  53.         cmd.Connection = conn;
  54.         cmd.Parameters.AddWithValue("@id", TextBox1.Text);
  55.         try
  56.         {
  57.             conn.Open();
  58.             SqlDataReader sdr = cmd.ExecuteReader();
  59.             while (sdr.Read())
  60.             {
  61.                 Response.Write(sdr[0].ToString() + "<br/>");
  62.                 Response.Write(sdr[1].ToString() + "<br/>");
  63.                 Response.Write(sdr[2].ToString() + "<br/>");
  64.             }
  65.         }
  66.         catch (SqlException ex)
  67.         {
  68.             Response.Write(ex.Message);
  69.         }
  70.         finally
  71.         {
  72.             conn.Close();
  73.         }
  74.     }
  75. }