日期:2014-05-16 浏览次数:20928 次
基本步骤:
1.在页面选择GridView空间
2.通过ADO.NET方法链接数据库。
3.在代码中,根据删除,编辑,取消,更新等按钮,执行相应的指定方法。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
namespace AspDB
{
??? public partial class _Default : System.Web.UI.Page
??? {
??????? protected void Page_Load(object sender, EventArgs e)
??????? {
??????????? if(!this.IsPostBack)//该段代码表示,当页面第一次加载的时候会绑定一次,当页面点更新的时候,一定要写该行代码,要不然就更新不了。因为点更新按钮后
??????????????????????????????? //没有该行,页面就重新获取原来的值了。
??????????? dbBind();
??????? }
??????? protected void Button1_Click(object sender, EventArgs e)
??????? {
??????????? MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection("Database='test';Data Source='localhost';User Id='root';Password='root';charset='utf8'");
??????????? MySqlCommand commend = new MySqlCommand("select * from user", con);
??????????? con.Open();
??????????
??????????? if (con.State == System.Data.ConnectionState.Open)
??????????? {
????????????
??????????????? MySqlDataAdapter msda = new MySqlDataAdapter("select * from user limit 50",con);
??????????????? DataSet ds = new DataSet();
??????????????? msda.Fill(ds);
??????????????? con.Close();
??????????????? this.GridView1.DataSource = ds;
??????????????? this.GridView1.DataKeyNames = new string[] { "ID" };
??????????????? this.GridView1.DataBind();
??????????? }
??????????? else
??????????? {
??????????????? Response.Write("<script>alert('数据库连接没打开。');</script>");
??????????? }
????????
??????? }
//该控件如果点击分页按钮,默认会触发PageIndexChanging方法
??????? protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
??????? {
??????????? this.GridView1.PageIndex = e.NewPageIndex;//通过点击页面的分页数字。传到后台的固定取值方法。
??????????? dbBind();
??????? }
??????? private void dbBind()
??????? {
??????????? MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection("Database='test';Data Source='localhost';User Id='root';Password='root';charset='utf8'");
??????????? MySqlCommand commend = new MySqlCommand("select * from user", con);
??????????? con.Open();
??????????? if (con.State == System.Data.ConnectionState.Open)
??????????? {
??????????????? MySqlDataAdapter msda = new MySqlDataAdapter("select * from user limit 50", con);
??????????????? DataSet ds = new DataSet();
??????????????? msda.Fill(ds);
??????????????? con.Close();
??????????????? this.GridView1.DataSource = ds;
??????????????? this.GridView1.DataKeyNames = new string[] { "ID"};//该段代码用来绑定你在删除或更新操作的时候,页面所传来的ID表示的是哪个字段。
??????????????? this.GridView1.DataBind();
??????????? }
??????????? else
??????????? {
??????????????? Response.Write("<script>alert('数据库连接没打开。');</script>");
??????????? }
??????? }
??????? //我们在GridView的列表中,选择旁边的按钮,然后选择"编辑列",然后选择CommandField选择“删除”,就会出现删除的列,当点击删除的时候,会指定执行RowDeleting
??????? //方法,你双击事件中的该方法,然后开始写代码。
??????? protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
??????? {
??????????? string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();//指定写法,来得到页面传来的删除的ID值
??????????? //要选择GridView1.DataKeys[e.RowIndex].Value的时候,在绑定数据源的时候,一定要申明你绑定的值:this.GridView1.DataKeyNames = new string[] { "ID"};
??????????? MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection("Database='test';Data Source='localhost';User Id='root';Password='root';charset='utf8'");
??????????? string sql = "delete from user where id="+Convert.ToInt32(id);
??????????
??????????? con.Open();
??????????? MySqlCommand commend = new MySqlCommand(sql, con);
??????????? commend.ExecuteNonQue