GridView的代码增、删、改、查

1Web.Servers配置

  <appSettings>

    <add key="ConnectionString" value="Server=2012-0901-1153;Database=jk;uid=sa;pwd=sa"/>

  </appSettings>

2、公共类

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Data.SqlClient;

using System.Data;

using System.Configuration;

/// <summary>

///BindData 的摘要说明

/// </summary>

public class BindData

{

     public BindData()

     {

         //

         //TODO: 在此处添加构造函数逻辑

         //

     }

    public SqlConnection GetCon()

    {

        return new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());

    }

    //执行数据库操作:增、删、改

    public int sqlEx(string cmdstr)

    {

        SqlConnection con = GetCon();

        con.Open();

        SqlCommand cmd = new SqlCommand(cmdstr, con);

       try

        {

            cmd.ExecuteNonQuery();

            return 1;

        }

        catch (Exception e)

        {

            return 0;

        }

        finally

        {

            con.Dispose();

        }

    }

    //查询操作,返回结果集

    public DataTable reDt(string cmdstr)

    {

        SqlConnection con = GetCon();

        SqlDataAdapter da = new SqlDataAdapter(cmdstr, con);

        DataSet ds = new DataSet();

        da.Fill(ds);

        return (ds.Tables[0]);

    }

    public SqlDataReader reDr(string str)

    {

        SqlConnection con = GetCon();

        con.Open();

        SqlCommand cmd = new SqlCommand(str, con);

        SqlDataReader sda = cmd.ExecuteReader(CommandBehavior.CloseConnection);

        return sda;

    }

}

3、增、删、更、查

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Data;

using System.Configuration;

public partial class _Default : System.Web.UI.Page

{

    int reValue = 0;

    private void Bind()

    {

        BindData bind = new BindData();

        string strsql = "select * from jk10";

        DataTable dt = bind.reDt(strsql);

        GridView1.DataKeyNames = new string[] { "id" };

        GridView1.DataSource = dt;

        GridView1.DataBind();

    }

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            Bind();

        }

    }

    //分页

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

        GridView1.PageIndex = e.NewPageIndex;

        Bind();

    }

    //删除

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {

        BindData db = new BindData();

        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();

        string strsql = "delete from jk10 where id=" + Convert.ToInt32(id); ;

        db.sqlEx(strsql);

        Bind();

    }

    //编辑

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

    {

        GridView1.EditIndex = e.NewEditIndex;

        Bind();

    }

    //编辑 --更新

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

    {

       BindData db = new BindData();

        SqlConnection con = db.GetCon();

        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();

        string dname = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();

        string dclass = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();

        string dzw = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString();

        string dphone = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();

        string sqlstr = "update jk10 set name='" + dname + "', class='" + dclass + "', zw='" + dzw + "', phone='" + dphone + "' where id=" + Convert.ToInt32(id);

        //update的SQL语句写错了,不要忘记了每个字段后面以逗号结尾

        con.Open();

        SqlCommand cmd = new SqlCommand(sqlstr, con);

        cmd.ExecuteNonQuery();

        cmd.Dispose();

        con.Close();

        GridView1.EditIndex = -1;

        Bind();

    }

    //编辑---取消

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

    {

        GridView1.EditIndex = -1;

        Bind();

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值