using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page
{
private string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; SqlHelper helper = new SqlHelper();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (SqlConnection conn = new SqlConnection(constr))
{ SqlDataAdapter ad = new SqlDataAdapter("select * from tbuser", conn);
SqlCommandBuilder builder = new SqlCommandBuilder(ad);
DataTable dt = new DataTable();
ad.Fill(dt);
this.GridView1.DataSource = dt;
this.DataBind();
}
}
} private void userlistbind()
{
GridView1.DataSource = helper.SelectSqlReturnDataset("select id, username as '用户名',birthday as '生日', '部门号'=(select departmentname from tbdepartment where tbdepartment.departmentid=tbUser.departmentid) from tbUser").Tables[];
}
protected void btnAdd_Click(object sender, EventArgs e)
{ string sql = "insert into tbUser(username,birthday,departmentid) values('" + txtUserName.Text + "','" + txtBirthday.Text + "'," + ddlDepartment.SelectedValue.ToString() + ")";
int count = helper.ExecuteReturnInt(sql, null, System.Data.CommandType.Text);
if (count != )
{
userlistbind();
Response.Write("<script>alert('数据添加成功!')</script>");
txtBirthday.Text = "";
txtUserName.Text = ""; GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{ }
protected void bind()
{
SqlConnection myconn = new SqlConnection(constr);
myconn.Open();
string sql = "select id,username 用户名,birthday 生日,departmentid 部门号 from tbUser";
SqlDataAdapter myda = new SqlDataAdapter(sql, myconn);
DataSet myds = new DataSet();
myda.Fill(myds);
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
myda.Dispose();
myds.Dispose();
myconn.Close();
} protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{ int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[].Controls[])).Text.ToString();
string birthday = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[].Controls[])).Text.ToString();
string department = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[].Controls[])).Text.ToString();
string sql = "update tbUser set username='" + name + "',birthday='"+birthday+"',departmentid="+department+" where id=" + id + "";
SqlConnection myconn = new SqlConnection(constr);
myconn.Open();
SqlCommand mycmd = new SqlCommand(sql, myconn);
mycmd.ExecuteNonQuery();
mycmd.Dispose();
myconn.Close();
GridView1.EditIndex = -;
this.bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -;
this.bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string sql = "delete from tbUser where id=" + id + "";
SqlConnection myconn = new SqlConnection(constr);
myconn.Open();
SqlCommand mycmd = new SqlCommand(sql, myconn);
mycmd.ExecuteNonQuery();
mycmd.Dispose();
myconn.Close();
GridView1.EditIndex = -;
this.bind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{ if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)e.Row.Cells[].Controls[]).Attributes.Add("onclick", "return confirm('确定要删除吗')");
}
}
}

web.config

<configuration>
<connectionStrings>
<add name="constr" connectionString="server=.\sqlexpress;database=db2016;uid=sa;pwd=123;" />
<add name="db2016ConnectionString" connectionString="Data Source=.\sqlexpress;Initial Catalog=db2016;Persist Security Info=True;User ID=sa;Password=123"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>

</configuration>

GridView的datakeyname属性 设为id

数据库表

create table tbUser

(
id int primary key identity(1,1),
username nvarchar(20),
userpass varbinary(128),
birthday datetime,
departmentid int foreign key references tbdepartment(departmentid)
)

asp.net 对数据库表增加,删除,编辑更新修改的更多相关文章

  1. MySQL数据库表的数据插入、修改、删除、查询操作及实例应用

    一.MySQL数据库表的数据插入.修改.删除和查询 CREATE DATABASE db0504; USE db0504; CREATE TABLE student ( sno ) NOT NULL ...

  2. SQL语句的使用,SELECT - 从数据库表中获取数据 UPDATE - 更新数据库表中的数据 DELETE - 从数据库表中删除数据 INSERT INTO - 向数据库表中插入数据

    SQL DML 和 DDL 可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). SQL (结构化查询语言)是用于执行查询的语法. 但是 SQL 语言也包含用于更新. ...

  3. [SQL基础教程] 1-5 表的删除和更新

    [SQL基础教程] 1-5 表的删除和更新 表的删除 语法 DROP TABLE <表名>; 法则 1-12 删除的表无法恢复 表定义的更新 语法 ALTER TABLE<表名> ...

  4. sqlserver为数据库表增加自增字段

     需求: 数据库为SQLServer.对已有的数据库表customer加一个序号字段,一次性对所有现存客户加上编号,并在新建客户时自动增加一个编号,数值自增1. 解决方法: 1. 复制表结构.把原 ...

  5. Mysql 创建数据库表(删除,删除,插入)

    MySQL 创建数据表 创建MySQL数据表需要以下信息: 表名 表字段名 定义每个表字段 语法 以下为创建MySQL数据表的SQL通用语法: CREATE TABLE table_name (col ...

  6. MySQL从查找数据库表到删除全过程

    使用DOS命令进入MySQL:mysql -u root -p   按回车键输入密码显示如下界面成功进入MySQL交互界面. 如果此时不知道MySQL有哪些数据库,使用显示所有数据库名语句:show ...

  7. django from组件 实现增加 删除 编辑(推荐用法)

    实现效果: 代码示例: 代码: models.py from django.db import models class UserInfo(models.Model): username = mode ...

  8. 017.CI4框架CodeIgniter数据库操作之:Updata更新修改一条数据

    01. 在Model中写入数据库操作的代码如下: <?php namespace App\Models\System; use CodeIgniter\Model; class User_mod ...

  9. Mysql 数据库表操作

    ☞ 创建表CREATE TABLE `数据库`.`表` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT COMMENT '注释',`type_name` VARCHA ...

随机推荐

  1. 使Eclipse下支持编写HTML/JS/CSS/JSP页面的自动提示。

    我们平时用eclipse开发jsp页面时智能提示效果不太理想,今天用了两个小时发现了eclipse也可以像Visual Studio 2008那样完全智能提示HTML/JS/CSS代码,使用eclip ...

  2. JS toFixed 四舍六入五成双

    以前一直以为toFixed就是四舍五入的方法,后来又有一段时间以为toFixed是五舍六入.今天终于写的时候,终于才知道toFixed是一个叫做四舍六入无成双的诡异的方法... 完全不明白为什么要这么 ...

  3. Python 之 时间字符串、时间戳、时间差、任意时间字符串转换时间对象

    1. 时间字符串 --> 时间戳 1) time 模块 timestring = '2016-12-21 10:22:56' print time.mktime(time.strptime(ti ...

  4. ORACLE10gRAC数据库迁移至10gRAC

    1.数据库备份RUN {ALLOCATE CHANNEL ch00 DEVICE TYPE disk;ALLOCATE CHANNEL ch01 DEVICE TYPE disk;ALLOCATE C ...

  5. [转]CAP原理与最终一致性 强一致性 透析

    在足球比赛里,一个球员在一场比赛中进三个球,称之为帽子戏法(Hat-trick).在分布式数据系统中,也有一个帽子原理(CAP Theorem),不过此帽子非彼帽子.CAP原理中,有三个要素: 一致性 ...

  6. 【原创】ORACLE常见使用问题解决

    ORACLE常见使用问题解决 一.安装了oracle客户端后,发现plsql客户端找不到之前已经配置过的TNS连接信息 或许大家再使用ORACLE软件的过程中,经常会遇到这样的问题: 问题现象描述: ...

  7. 用svg制作loading动画

    首先说明:由于各浏览器对svg动画事件支持不统一,此loading动画在Firefox,Opera,Chrome中均没有问题,IE和Safari中有问题,可能是不支持SIML写动画的语法, 但是用Ca ...

  8. JAVA 多线程和并发学习笔记(三)

    Java并发编程中使用Executors类创建和管理线程的用法 1.类 Executors Executors类可以看做一个“工具类”.援引JDK1.6 API中的介绍: 此包中所定义的 Execut ...

  9. C# 访问MongoDB 通用方法类

    using MongoDB.Driver; using System; namespace MongoDBDemo { public class MongoDb { public MongoDb(st ...

  10. Mongodb故障转移

    测试环境 四台测试服务器10.151.151.150:27017(主节点),10.151.151.151:27017(从节点),10.151.151.152:27018(从节点),10.151.151 ...