news.aspx.cs
上传用户:xy99169
上传日期:2022-08-03
资源大小:139k
文件大小:2k
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Data.SqlClient;
- using System.Web.Configuration;
- public partial class news : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- string connStr = WebConfigurationManager.ConnectionStrings["news"].ConnectionString;
- SqlConnection conn = new SqlConnection(connStr);
- string cmdStr = "select * from news";
- SqlCommand cmd = new SqlCommand();
- cmd.CommandText = cmdStr;
- cmd.Connection = conn;
- try
- {
- conn.Open();
- SqlDataReader sdr = cmd.ExecuteReader();
- while (sdr.Read())
- {
- Response.Write(sdr[0].ToString() + "<br/>");
- Response.Write(sdr[1].ToString() + "<br/>");
- Response.Write(sdr[2].ToString() + "<br/>");
- }
- }
- catch (SqlException ex)
- {
- Response.Write(ex.Message);
- }
- finally
- {
- conn.Close();
- }
- }
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- string connStr = WebConfigurationManager.ConnectionStrings["news"].ConnectionString;
- SqlConnection conn = new SqlConnection(connStr);
- string cmdStr = "select * from news where newsID=@id";
- SqlCommand cmd = new SqlCommand();
- cmd.CommandText = cmdStr;
- cmd.Connection = conn;
- cmd.Parameters.AddWithValue("@id", TextBox1.Text);
- try
- {
- conn.Open();
- SqlDataReader sdr = cmd.ExecuteReader();
- while (sdr.Read())
- {
- Response.Write(sdr[0].ToString() + "<br/>");
- Response.Write(sdr[1].ToString() + "<br/>");
- Response.Write(sdr[2].ToString() + "<br/>");
- }
- }
- catch (SqlException ex)
- {
- Response.Write(ex.Message);
- }
- finally
- {
- conn.Close();
- }
- }
- }