using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=MyDb;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select * from class", con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
con.Open();
DataSet ds = new DataSet();
ad.Fill(ds);
ds.Tables[0].Columns[0].ColumnName = "班级ID";
ds.Tables[0].Columns[1].ColumnName = "班级";
ds.Tables[0].Columns[2].ColumnName = "班级位置";
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
this.txtClassId.Text = this.GridView1.Rows[0].Cells[1].Text.ToString();
this.txtClassName.Text = this.GridView1.Rows[0].Cells[2].Text.ToString().Trim();
this.txtClassLocation.Text = this.GridView1.Rows[0].Cells[3].Text.ToString().Trim();
this.GridView1.Rows[0].RowState = DataControlRowState.Selected;
con.Close();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (GridView1.SelectedRow.RowIndex != 0)
{
this.GridView1.Rows[0].RowState = DataControlRowState.Normal;
}
this.txtClassId.Text = GridView1.SelectedRow.Cells[1].Text.ToString();
this.txtClassName.Text = GridView1.SelectedRow.Cells[2].Text.ToString().Trim();
this.txtClassLocation.Text = GridView1.SelectedRow.Cells[3].Text.ToString().Trim();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=MyDb;Integrated Security=True");
string sql = "insert into class values ('" + this.txtClassId.Text + "', '" + this.txtClassName.Text + "', '" + this.txtClassLocation.Text + "')";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();
cmd.CommandText = "select * from class";
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.Fill(ds);
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
con.Close();
Response.Redirect(Request.RawUrl);
}
protected void btnChange_Click(object sender, EventArgs e)
{
int oldcode = Convert.ToInt32(this.GridView1.SelectedRow.Cells[1].Text);
SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=MyDb;Integrated Security=True");
string sql = "update class set classname = '" + this.txtClassName.Text + "', classlocation = '" + this.txtClassLocation.Text
+ "'where cid = " + oldcode;
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();
cmd.CommandText = "select * from class";
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.Fill(ds);
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
con.Close();
Response.Redirect(Request.RawUrl);
}
protected void btnDel_Click(object sender, EventArgs e)
{
int oldcode = Convert.ToInt32(this.GridView1.SelectedRow.Cells[1].Text);
SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=MyDb;Integrated Security=True");
string sql = "delete from class where cid = " + oldcode;
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();
cmd.CommandText = "select * from class";
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.Fill(ds);
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
con.Close();
Response.Redirect(Request.RawUrl);
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
}
}
Asp.net增删改查的基本操作(最简单操作)
最新推荐文章于 2024-03-20 21:25:02 发布