gridview无数据源实现更新数据库(即断开更新数据库)

原文发布时间为:2008-08-01 —— 来源于本人的百度文章 [由搬家工具导入]

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

//gridview无数据源实现更新(断开更新)拖放一个gridview时没有编辑这个项,应该把它的属性AutoGeneraltedEditButton这个属性改成true,这样编辑这个链接就会出现

public partial class Default3 : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Server=.\\SQLEXPRESS;Database=test;uid=sa;pwd=123456");
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind();

    }
    private void Bind()
    {
        SqlDataAdapter sda = new SqlDataAdapter("select * from stu", conn);
        DataSet ds = new DataSet();
        sda.Fill(ds, "temp");
        GridView1.DataSource = ds.Tables["temp"].DefaultView;
        GridView1.DataBind();
    }
    private void fill(int id, string name, string banji)
    {
        SqlDataAdapter sda = new SqlDataAdapter("select * from stu", conn);
        SqlCommandBuilder scbld = new SqlCommandBuilder(sda);//因为有这句,所以关闭连接后也能更新

        DataSet ds = new DataSet();
        sda.Fill(ds, "temp");
        ds.Tables["temp"].DefaultView.Sort = "id";
        int index = ds.Tables["temp"].DefaultView.Find(id);
        ds.Tables["temp"].Rows[index]["name"] = name;
        ds.Tables["temp"].Rows[index]["class"] = banji;

        int rows = sda.Update(ds, "temp");

        Response.Write("update " + rows + " rows datas");
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int index = e.RowIndex;
        int id = Convert.ToInt32(GridView1.Rows[index].Cells[1].Text);
        string name = ((TextBox)GridView1.Rows[index].Cells[2].FindControl("TextBox1")).Text;
        string banji = ((TextBox)GridView1.Rows[index].Cells[3].FindControl("TextBox2")).Text;
        fill(id, name, banji);
        GridView1.EditIndex = -1;
        Bind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Bind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Bind();
    }
}

时间: 2024-08-23 23:17:31

gridview无数据源实现更新数据库(即断开更新数据库)的相关文章

JDBC连接数据库(MySql)步骤,以及查询、插入、删除、更新等十一个处理数据库信息的功能。

主要内容:  JDBC连接数据库步骤. 一个简单详细的查询数据的例子. 封装连接数据库,释放数据库连接方法. 实现查询,插入,删除,更新等十一个处理数据库信息的功能. 把十一个功能都放在一起. 一.JDBC连接数据库(编辑)步骤(主要有六个步骤).  1.注册驱动: Class.formName("com.mysql.jdbc.Driver");  2.获取连接:(1) param1:  要连接数据库的url-----> String url="jdbc:mysql:/

JDBC连接(MySql)数据库步骤,以及查询、插入、删除、更新等十一个处理数据库信息的功能

主要内容:  JDBC连接数据库步骤. 一个简单详细的查询数据的例子. 封装连接数据库,释放数据库连接方法. 实现查询,插入,删除,更新等十一个处理数据库信息的功能.(包括事务处理,批量更新等) 把十一个功能都放在一起. 安装下载的数据库驱动程序jar包,不同的数据库需要不同的驱动程序(这本该是第一步,但是由于属于安装类,所以我们放在最后) 一.JDBC连接数据库(编辑)步骤(主要有六个步骤).  1.注册驱动: Class.forName("com.mysql.jdbc.Driver"

程序执行时,提示数据包堵塞,数据库时不时断开的问题

DB ERROR: (192.168.0.111 -- V2DB_MatchCenter)source = Microsoft OLE DB Provider for SQL ServerError Msg = 未指定的错误Error Content = [DBNETLIB][ConnectionWrite (send()).]一般性网络错误.请检查网络文档.Error list: Error Code: 80004005 之前有出现数据库时不时断开的问题,重启了数据库服务以后暂未继续出现问题,

SQL Server 断开某个数据库所有连接(还原的时候需要)

问题描述: SQL Server数据库备份还原后,在数据库名称后会出现"受限制访问"字样 解决办法: 右键点击数据库 -> 属性 -> 选项 -> 状态 -> 限制访问 -> 选择Multiple -> 确定. 问题描述: 在对SQL Server数据库进行还原时,提示:System.Data.SqlClient.SqlError:因为数据库正在使用,所以无法获得对数据库的独占访问权.(Microsoft.SqlServer.Smo).出现此问题的原

Delphi ADOQuery无法更新定位行问题(其它数据库访问控件类似)

在Delphi中用ADOQuery进行数据库的记录操作时,有时会报 “无法为更新定位行.一些值可能已经在最后一次读取后已更改” 这个错. 这个错通常是 你操作的记录 在数据库中已经被修改过了, 如: 在数据库中已被删除了,你用ADOQuery更新了被删除的记录 数据库表字段有默认值,你用ADOQuery插入新记录后没有重新查询一遍就再次操作该记录 暂时知道的会引起该错误的可能有这两种,归纳起来 就是 ADOQuery的记录 与 数据库 表记录 对应不上 Delphi ADOQuery无法更新定位

数据库字段关联更新

MS SQL Server 子查询更新: update log set uin= b.uin from log a,logs b where a.accountuin = b.accountuin mysql 更新: update t_stat_month_user a INNER JOIN t_dept b on a.op_dept=b.op_id set a.dept_short_name=b.dept_short_name; 数据库字段关联更新,布布扣,bubuko.com

Android开发:自定义GridView/ListView数据源

http://mobile.51cto.com/android-259861.htm 在开发中,我们常常会遇到比较复杂的GridView/ListView的布局,重新实现BaseAdapter不但能帮助我们实现我们想要的布局效果,并且在绑定大数据量时也不会感觉有卡壳现象.记得以前用一个ListView直接去绑定手机内的联系人Cursor(一百多号人),滑动的时候就会有卡的感觉.今天决定写个Demo是因为在项目中可能会要实现这样的一个效果:一个GridView中绑定4个ImageButton,有些

crawldb数据库内容得到更新

inject操作结果:crawldb数据库内容得到更新,包括URL及其状态. 看一下inject调用的函数: public voidinject(Path crawlDb, Path urlDir) throwsIOException { //产生一个文件名是随机的临时文件夹 Path tempDir = newPath(getConf().get("mapred.temp.dir", ".") + "/inject-temp-" + Integ

PHP对于数据库的基本操作——更新数据

PHP对于数据库的基本操作--更新数据 主要的语句:$update = mysql_query("update user set password='abc' where username='123'"); 这句话的意思是,更新user表,设置passWord字段的值为abc,筛选条件为username为123的数据.就是修改user表里面username为123字段里面的password,将password的值为abc. 前面的随笔里面忘了说  where 这个了,这个我理解为筛选,