using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//连接并打开数据库
//string strConn = "server=.;database=CMS;uid=sa;pwd=654321";
//SqlConnection conn = new SqlConnection(strConn);
//conn.Open();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConn"].ConnectionString);//读取配置文件中的连接字符串
conn.Open();//打开数据库连接
//查询数据并填充到ds
string strSql = "select ID,Title from News where class=1 order by ID desc";
SqlDataAdapter adp = new SqlDataAdapter(strSql, conn);
DataSet ds=new DataSet();
adp.Fill(ds,"News");
//显示数据
DataList1.DataSource=ds;
DataList1.DataBind();
//=======================
string strSql2 = "select ID,Title from News where class=2 order by ID desc";
SqlDataAdapter adp2 = new SqlDataAdapter(strSql2, conn);
DataSet ds2 = new DataSet();
adp2.Fill(ds2, "News");
//显示数据
DataList2.DataSource = ds2;
DataList2.DataBind();
}
}
using System.Data.SqlClient;
public partial class Show : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//连接并打开数据库
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConn"].ConnectionString);//读取配置文件中的连接字符串
conn.Open();//打开数据库连接
//查询数据
string strSql = "select ID,Title,Content from News where ID='"+Request.QueryString["id"]+"'";
SqlDataAdapter adp = new SqlDataAdapter(strSql, conn);
DataSet ds=new DataSet();
adp.Fill(ds,"News");
labTitle.Text = ds.Tables[0].Rows[0]["Title"].ToString();
labContent.Text = ds.Tables[0].Rows[0]["Content"].ToString();
}
}