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

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();
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值