前台:
<asp:LinkButton ID="LinkButton2" runat="server" CommandArgument='<%# Bind("Id") %>' CommandName='del'>删除</asp:LinkButton>后台:protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "del")
{
if (DelArticle(Convert.ToInt32(e.CommandArgument)) == 1)
{
Response.Write("删除成功!");
BindData();
}
else
Response.Write("删除失败!");
}
} public static int DelArticle(int id)
{
string sql = "delete Article where id=@id";
return Execute(sql,CommandType.Text,new OleDbParameter("@id",id));
}
//执行增删改的操作
public static int Execute(string sql, CommandType type, params OleDbParameter[] param)
{
int result = -1;
OleDbConnection con = new OleDbConnection(conStr);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = type;
cmd.CommandText = sql;
cmd.Connection = con;
if (param.Length > 0)
cmd.Parameters.AddRange(param); try
{
con.Open();
result = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
con.Close();
cmd.Dispose();
throw ex;
}
finally
{
con.Close();
cmd.Dispose();
}
return result; }
<asp:LinkButton ID="LinkButton2" runat="server" CommandArgument='<%# Bind("Id") %>' CommandName='del'>删除</asp:LinkButton>后台:protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "del")
{
if (DelArticle(Convert.ToInt32(e.CommandArgument)) == 1)
{
Response.Write("删除成功!");
BindData();
}
else
Response.Write("删除失败!");
}
} public static int DelArticle(int id)
{
string sql = "delete Article where id=@id";
return Execute(sql,CommandType.Text,new OleDbParameter("@id",id));
}
//执行增删改的操作
public static int Execute(string sql, CommandType type, params OleDbParameter[] param)
{
int result = -1;
OleDbConnection con = new OleDbConnection(conStr);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = type;
cmd.CommandText = sql;
cmd.Connection = con;
if (param.Length > 0)
cmd.Parameters.AddRange(param); try
{
con.Open();
result = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
con.Close();
cmd.Dispose();
throw ex;
}
finally
{
con.Close();
cmd.Dispose();
}
return result; }
解决方案 »
- 求ext.net ComboBox 的省市级联动例子!第一个适合的分全给!!!!
- C# 除法运算
- 求一条复杂的sql语句
- VS2005 水晶报表问题
- 怎么使用这样的top.location.href=''Main.aspx'语句跳到一个框架,而不是跳出呢?
- 请问 Visual Studio.NET 2003 免费下载?
- 怎么读出数据库字段的长度??
- 请问在C#里怎么才能让多行文本框中的文本保持格式?
- 我的项目,我快要疯了~!!怎么会变成这样呢 换台电脑数据库访问不了??
- ☆★☆★ 怎样能把TextBox转换为DATATIME?? ★☆★☆
- 求一条SQL语句
- 配置FCKeditor文本编辑器的问题
new OleDbParameter("@id", OleDbType.Integer,4)};
parameters[0].Value = id;
return Execute(sql,CommandType.Text,parameters);