您的位置:首页 > 其它

执行命令方法的封装(添加/修改/删除)

2013-03-19 00:44 453 查看
View Code

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//引入命名空间
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
SqlDb db = new SqlDb();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();//调用自定义方法绑定数据到控件
}
}
public void BindData()
{
string sqlstr = "select * from bccd01 order by BccdID desc";//定义执行查询操作的SQL语句
db.BindData(GridView1, sqlstr);
if (GridView1.Rows.Count > 0)
{
return;//有数据,不要处理
}
else//显示表头并显示没有数据的提示信息
{
StrHelper.GridViewHeader(GridView1);
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//定义一个整型变量count存储根据当前行索引获取到的指定单元格中的字段值
int count = int.Parse(GridView1.Rows[e.RowIndex].Cells[3].Text.Trim());
if (count > 0)
{
e.Cancel = true;//取消删除动作
lblMessage.Text = "警告:库存量大于0的商品不得删除!";
lblMessage.ForeColor = System.Drawing.Color.Red;
return;
}
string delete_sql = "delete from bccd01 where BccdID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
bool delete = db.ExceSQL(delete_sql);//调用ExceSQL执行删除操作
if (delete)
{
StrHelper.Alert("删除成功!");
BindData();//调用自定义方法重新绑定控件中数据
}
else
{
StrHelper.Alert("删除失败!");
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//取得编辑行的关键字段的值
string bccdID = GridView1.DataKeys[e.RowIndex].Value.ToString();
//取得文本框中输入的内容
string bccdName=((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string bccdPrice=((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string bccdInStock=((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
//定义更新操作的SQL语句
string update_sql = "update bccd01 set BccdName='" + bccdName + "',BccdPrice='" + bccdPrice + "',BccdInStock='" + bccdInStock + "' where BccdID='" + bccdID + "'";
bool update = db.ExceSQL(update_sql);//调用ExceSQL执行更新操作
if (update)
{
StrHelper.Alert("修改成功!");
//设置GridView控件的编辑项的索引为-1,即取消编辑
GridView1.EditIndex = -1;
BindData();
}
else
{
StrHelper.Alert("修改失败!");
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//设置GridView控件的编辑项的索引为-1,即取消编辑
GridView1.EditIndex = -1;
BindData();//数据绑定
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();//数据绑定
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();//数据绑定
}
protected void btnInsert_Click(object sender, EventArgs e)
{
string InsertSql = "Insert Into bccd01(BccdName,BccdPrice,BccdInStock) values('" + txtName.Text + "','" + txtPrice.Text + "','" + txtSum.Text + "')";
bool insert = db.ExceSQL(InsertSql);//调用公共类中的ExceSQL()执行添加操作
if (insert)//如果添加成功
{
StrHelper.Alert("添加成功!");
BindData();//重新绑定数据
}
else
{
StrHelper.Alert("添加失败!");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: