代码如下 | 复制代码 |
CREATE DATABASE MedicineDB; USE MedicineDB; CREATE TABLE Medicine( [ID] bigint, [Name] varchar(50), [Price] bigint, [Type] varchar(50), [Intro] text, primary key(ID) ) |
代码如下 | 复制代码 |
CREATE DATABASE MedicineDB; USE MedicineDB; CREATE TABLE Medicine( [ID] bigint, [Name] varchar(50), [Price] bigint, [Type] varchar(50), [Intro] text, primary key(ID) ); USE MedicineDB; INSERT INTO Medicine VALUES (1,'板蓝根','9','冲剂','清热解毒'); INSERT INTO Medicine VALUES (2,'999感冒颗粒','10','冲剂','感冒'); |
代码如下 | 复制代码 |
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; /// |
代码如下 | 复制代码 |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> |
代码如下 | 复制代码 |
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; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) bind(); } public void bind() { string sql = "select * from Medicine"; GridView1.DataKeyNames = new string[]{"ID"}; DB.gvDataBind(GridView1, sql); } //记得绑定删除事件 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e){ string sql = "delete from Medicine where ID = " + GridView1.DataKeys[e.RowIndex].Value; DB.executeCmd(sql); Response.Redirect("Default.aspx"); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { ((LinkButton)(e.Row.Cells[4].Controls[0])).Attributes.Add("onclick", "return confirm('确定要删除吗?')"); } } } |
代码如下 | 复制代码 |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Show.aspx.cs" Inherits="Show" %> |
代码如下 | 复制代码 |
using System; using System.Data; using System.Configuration; using System.Collections; 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 Show : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string id = Request.QueryString["id"].ToString(); string sql = "select * from Medicine where ID = " + id; SqlConnection conn = DB.createCon(); SqlDataAdapter sda = new SqlDataAdapter(sql,conn); DataSet ds = new DataSet(); sda.Fill(ds,"Medicine"); DataRowView drv = ds.Tables["Medicine"].DefaultView[0]; TextBox1.Text = Convert.ToString(drv.Row["ID"]); TextBox2.Text = Convert.ToString(drv.Row["Name"]); TextBox3.Text = Convert.ToString(drv.Row["Price"]); TextBox4.Text = Convert.ToString(drv.Row["Type"]); TextBox5.Text = Convert.ToString(drv.Row["Intro"]); } } protected void Button1_Click(object sender, EventArgs e) { string id = Request.QueryString["id"].ToString(); string sql = "update Medicine set [ID] = " + TextBox1.Text.Trim() + ",[Name] = '" + TextBox2.Text.Trim() +"',[Price] = "+TextBox3.Text.Trim()+",[Type] = '"+TextBox4.Text.Trim()+"',[Intro] = '" + TextBox5.Text.Trim() + "' where ID = " + id; DB.executeCmd(sql); Response.Redirect("Default.aspx"); } } |