數(shù)據(jù)添加篇
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('對不起,該產(chǎn)品編號已經(jīng)存在!')"); 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數(shù)據(jù)庫執(zhí)行的一個Transact-SQL語句或存儲過程 cmd_add.ExecuteNonQuery();//對連接執(zhí)行Transact-SQL語句并返回受影響的行數(shù)。對于 UPDATE、INSERT 和 DELETE 語句,返回值為該命令所影響的行數(shù)。對于所有其他類型的語句,返回值為 -1。如果發(fā)生回滾,返回值也為 -1。 myconn.Dispose(); myconn.Close(); } [/CODE
[COLOR=Red]數(shù)據(jù)顯示篇[/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;//指向數(shù)據(jù)源 shows.DataBind();//綁定數(shù)據(jù) rd_show.Close();//關(guān)閉SqlDataReader } } }
數(shù)據(jù)修改篇
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); }
數(shù)據(jù)刪除篇
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://m.95time.cn/tech/program/2006/3578.asp
出處:藍色理想
責(zé)任編輯:moby
上一頁 ASP.NET入門數(shù)據(jù)篇 [1] 下一頁
◎進入論壇網(wǎng)絡(luò)編程版塊參加討論
|