当前位置: 首页 > 工具软件 > Database .NET > 使用案例 >

[我的ASP.net学习历程]DataBase Settion(6)

詹亮
2023-12-01
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;

namespace _172._24._17._134
{
	/// <summary>
	/// WebForm1 的摘要说明。
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label lblChoise;
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			string strSql = "SELECT ID,Username,Password,Content,Email,Homepage FROM Table_1";
			string connectionString = "Server=(local);User ID=sa;Pwd=testPass;Database=TestDb;Connect Timeout=5";

			SqlConnection conn = new SqlConnection(connectionString);
			SqlCommand cmd = new SqlCommand(strSql,conn);

			cmd.Connection.Open();

			DataSet ds = new DataSet();
			SqlDataAdapter da = new SqlDataAdapter();

			da.SelectCommand = cmd;

			da.Fill(ds,"Table");

			DataGrid1.DataSource = ds.Tables["Table"].DefaultView;
			DataGrid1.DataBind();

			ds.Clear();
			da.Dispose();
			cmd.Connection.Close();
			cmd.Connection.Dispose();
		}
		#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.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			TableRow tr = e.Item;
			lblChoise.Text = "您选择的用户是:" + tr.Cells[1].Text;
			lblChoise.Text = lblChoise.Text + " 密码是:" + tr.Cells[2].Text;
			lblChoise.Text = lblChoise.Text + " 内容是:" + tr.Cells[3].Text;
		}
	}
}
 类似资料: