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. java数据传递例子+内存分析

    一.引用传递 1.例子1 package com.jikexueyuan.ref; class Ref1{ int temp = 10; } public class RefDemo01 { publ ...

  2. SQL Server选项综述

    I. 基本概念 SQL Server中的选项根据其作用范围分为如下几类: 实例选项 —— 在数据库实例范围内有效,通过 sp_configure 存储过程进行配置. 数据库选项 —— 在数据库范围内有 ...

  3. pointer on c

    http://blog.csdn.net/daniel_ice/article/details/6857019 http://www.cppblog.com/cuigang/archive/2008/ ...

  4. 安装SQl 2008为SQL Server代理服务提供的凭据无效

    解决:安装SQl 2008为SQL Server代理服务提供的凭据无效 sql server 2008 代理服务提供的凭据无效sql server 2008 代理服务提供的凭据无效在Windows S ...

  5. WINDOWS Server2008上部署Oracle10g及oracle SQL语法小记

    首先安装10G客户端 情况一:一般都会安装到一般报错.因为10G是32BIT客户端.而操作系统是64位的.但是不会影响配置监听程序.自主开发的应用程序依然可以运行. 情况二:报错但是配置完监听程序始终 ...

  6. oracle 查询执行过的SQL语句

    SELECT * FROM v$sqlarea t WHERE t.FIRST_LOAD_TIME between '2016-12-23/16:03:00' and '2016-12-23/16:0 ...

  7. php 判断文件或目录是否存在

    判断文件或目录是否存在有自带的函数 file_exists:文件是否存在 $file = "check.txt"; if(file_exists($file)) {     ech ...

  8. null值与空值比较

    JAVA中判断字符串或者数值是否为空时,常用到  .equals函数对空值进行判断 例如  values[5]为参数值 "".equals(values[5]) 常在if语句判断中 ...

  9. BufferedReader 中的 readLine()

    BufferedReader中的readLine()方法,API解释如下: Reads a line of text. A line is considered to be terminated by ...

  10. php中cookie技术关于跨目录调用cookie值的问题

    今天做项目发现了一个奇葩错误,以cookie技术为主,反复测试发现cookie不能跨目录调用. 我在F:wamp\www\test\下面有1.php和2.php其中1.php接受2.php中setco ...