GridView的代码增、删、改、查

来源:互联网 发布:java如何得到.xlsx文件 编辑:程序博客网 时间:2024/06/08 02:41

1Web.Servers配置

 <appSettings>

   <addkey="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>

publicclass BindData

{

    public BindData()

    {

        //

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

        //

    }

   public SqlConnection GetCon()

   {

       return newSqlConnection(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 = newDataSet();

       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;

publicpartial 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);

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

       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();

   }

}

原创粉丝点击