数据添加篇
protected void btnAdd_Click(object sender, EventArgs e) { string ProductId = this.txtProductId.Text; string CategoryId = this.txtCategoryId.Text; string Name = this.txtName.Text; string Description = this.txtDescription.Text; string Price =this.txtPrice.Text;
string sql_Exeits = "select * from Product where ProductId='" + ProductId + "'"; SqlCommand cmd_Exeits = new SqlCommand(sql_Exeits, myconn); myconn.Open(); SqlDataReader rdr = cmd_Exeits.ExecuteReader(); while (rdr.Read()) { Response.Write("<script language='JavaScript'>"); Response.Write("alert('对不起,该产品编号已经存在!')"); Response.Write("</script>"); this.txtCategoryId.Text = ""; this.txtDescription.Text = ""; this.txtName.Text = ""; this.txtPrice.Text = ""; this.txtProductId.Text = ""; return; } rdr.Close();
string sql_add = "insert into Product(ProductId,CategoryId,Name,Description,Price)values('" + ProductId + "','" + CategoryId + "','" + Name + "','" + Description + "','" + Price + "')"; SqlCommand cmd_add = new SqlCommand(sql_add, myconn);//SqlCommand:表示要对SQL Server数据库执行的一个Transact-SQL语句或存储过程 cmd_add.ExecuteNonQuery();//对连接执行Transact-SQL语句并返回受影响的行数。对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。对于所有其他类型的语句,返回值为 -1。如果发生回滚,返回值也为 -1。 myconn.Dispose(); myconn.Close(); } [/CODE
[COLOR=Red]数据显示篇[/COLOR] [CODE] protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string id = Request.Params["id"]; if (id == null || id.Trim() == "") { Response.Redirect("default.aspx"); Response.End(); } else { string sql_show = "select * from Product Where ProductId=" + id; SqlCommand cmd_show = new SqlCommand(sql_show, conn); conn.Open(); SqlDataReader rd_show = cmd_show.ExecuteReader();//使用SqlDataReader对象读取并返回一个记录集 shows.DataSource = rd_show;//指向数据源 shows.DataBind();//绑定数据 rd_show.Close();//关闭SqlDataReader } } }
数据修改篇
protected void btnAdd_Click(object sender, EventArgs e) { string ProductId = this.lblProductId.Text; string CategoryId = this.txtCategoryId.Text; string Name = this.txtName.Text; string Description = this.txtDescription.Text; decimal Price = decimal.Parse(this.txtPrice.Text);
string sql_edit = "update Product set CategoryId='" + CategoryId + "',Name='" + Name + "',Description='" + Description + "',Price='" + Price + "' where ProductId =" + ProductId; SqlCommand cmd_edit = new SqlCommand(sql_edit, conn);
conn.Open(); cmd_edit.ExecuteNonQuery(); conn.Close(); Response.Write("<script language=javascript>window.alert('保存成功!')</script>"); Response.Redirect("show.aspx?id=" + ProductId); }
数据删除篇
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string ProductId = Request.Params["id"]; string sql_del = "delete from Product where ProductId=" + ProductId; SqlCommand cmd_del = new SqlCommand(sql_del, conn); conn.Open(); cmd_del.ExecuteNonQuery(); conn.Close(); Response.Redirect("default.aspx"); } }
例子下载
本文链接:http://www.blueidea.com/tech/program/2006/3578.asp
出处:蓝色理想
责任编辑:moby
上一页 ASP.NET入门数据篇 [1] 下一页
◎进入论坛网络编程版块参加讨论
|