现在的位置: 首页 > 综合 > 正文

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

2013年08月03日 ⁄ 综合 ⁄ 共 3310字 ⁄ 字号 评论关闭
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.DataGrid DataGrid1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			if (!Page.IsPostBack)
			{
				BindData();
			}
		}
		#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.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
			this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
			this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion


		private void BindData()
		{
			string strSql = "SELECT ID,Username,Password,Content,Email,Homepage FROM Table_1";
			string connectionString = "Server=(local);User ID=user;Pwd=useruser;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();

			SqlDataReader dr = cmd.ExecuteReader();

			DataGrid1.DataSource = dr;
			DataGrid1.DataBind();

			dr.Close();
			cmd.Connection.Close();
			cmd.Connection.Dispose();
		}

		public void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGrid1.EditItemIndex = Convert.ToInt32(e.Item.ItemIndex);
			BindData();
		}

		public void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGrid1.EditItemIndex = -1;
			BindData();
		}

		public void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			string connectionString = "Server=(local);User ID=sa;Pwd=zxbbugu;Database=TestDb;Connect Timeout=5";
			string id = e.Item.Cells[0].Text.ToString();
			string username = ((TextBox)e.Item.Cells[1].Controls[0]).Text.ToString();
			string password = ((TextBox)e.Item.Cells[2].Controls[0]).Text.ToString();
			string content = ((TextBox)e.Item.Cells[3].Controls[0]).Text.ToString();
			string email = ((TextBox)e.Item.Cells[4].Controls[0]).Text.ToString();
			//string homepage = ((TextBox)e.Item.Cells[5].Controls[0]).Text.ToString();
			string strSql = "UPDATE Table_1 SET Username='" + username + "',Password='" + password;
			//strSql += "',Content='" + content + "',Email='" + email + "',Homepage='" + homepage;
			strSql += "',Content='" + content + "',Email='" + email;
			strSql += "' WHERE ID=" + id;
//
//			string username = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
//			string strSql = @"UPDATE Table_1 SET Username='"+username+"' WHERE ID=" + id;

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

			cmd.Connection.Open();
			cmd.ExecuteNonQuery();

			cmd.Dispose();
			conn.Close();
			conn.Dispose();

			DataGrid1.EditItemIndex = -1;
			BindData();
		}
	}
}

抱歉!评论已关闭.