当前位置: 首页 > 编程日记 > 正文

使用command对象操作数据库

1.Command对象查询数据库

 protected void Button1_Click(object sender, EventArgs e){//读取web.config节点配置string strcon = ConfigurationManager.ConnectionStrings["testjm"].ConnectionString;//实例化sqlConnection对象SqlConnection con = new SqlConnection(strcon);//数据库建立连接打开
            con.Open();string strsql = "select * from userinfo where name=@name";//查询语句SqlCommand mycmd = new SqlCommand(strsql, con);mycmd.Parameters.Add("@name", SqlDbType.VarChar,20).Value = TextBox1.Text.Trim();SqlDataAdapter myda = new SqlDataAdapter(mycmd);//实例化SqlDataAdapter,把strsql查询语句通过con传递给数据库DataSet myds = new DataSet();//实例化DataSet为mydsmyda.Fill(myds, "userinfo");//填充数据集GridView1.DataSource = myds;//界面上显示返回的数据集,指定数据源GridView1.DataBind();//绑定数据库
myda.Dispose();myds.Dispose();con.Close();//关闭连接}

2.Command对象添加数据

/// <summary>/// 封装查询userinfo表信息/// </summary>protected void bind(){SqlConnection con = getcon();//数据库建立连接打开
        con.Open();string strsql = "select * from userinfo";//查询语句SqlDataAdapter myda = new SqlDataAdapter(strsql,con);DataSet myds = new DataSet();myda.Fill(myds);GridView1.DataSource = myds;//界面上显示返回的数据集,指定数据源GridView1.DataKeyNames = new string[] { "id" };GridView1.DataBind();//绑定数据库
        myda.Dispose();myds.Dispose();con.Close();}/// <summary>/// 封装数据库连接/// </summary>/// <returns></returns>protected SqlConnection getcon(){//读取web.config节点配置string strcon = ConfigurationManager.ConnectionStrings["testjm"].ConnectionString;//实例化sqlConnection对象SqlConnection con1 = new SqlConnection(strcon);return con1;}

/// <summary>/// 添加数据/// </summary>/// <param name="sender"></param>/// <param name="e"></param>protected void btSumbit_Click(object sender, EventArgs e){SqlConnection con = getcon();//数据库建立连接打开
        con.Open();string strinsert = "insert into userinfo(id,name,password,age) values(" + this.tbid.Text.Trim() + ",'" + this.tbname.Text.Trim() + "','" + this.tbpwd.Text.Trim() + "'," + this.tbage.Text.Trim() + ")";SqlCommand mycmd = new SqlCommand(strinsert, con);mycmd.ExecuteNonQuery();mycmd.Dispose();con.Close();//关闭连接this.bind();}/// <summary>/// 添加数据中的重置/// </summary>/// <param name="sender"></param>/// <param name="e"></param>protected void btReset_Click(object sender, EventArgs e){tbid.Text = "";tbname.Text = "";tbpwd.Text = "";tbage.Text = "";}

3.Command对象修改数据

 /// <summary>/// 单击编辑按钮,会触发RowEditing事件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e){GridView1.EditIndex = e.NewEditIndex;this.bind();}/// <summary>/// 更新数据,RowUpdating更新前的事件,RowUpdated更新后的事件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e){int cid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());string cName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();string cPwd = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();string strupdate = "update userinfo set name='" + cName + "',password='"+ cPwd +"'where id=" + cid;SqlConnection con = getcon();con.Open();SqlCommand mycmd = new SqlCommand(strupdate, con);mycmd.ExecuteNonQuery();mycmd.Dispose();con.Close();//关闭连接this.bind();}/// <summary>/// 单击更新中的取消按钮,触发RowCancelingEdit事件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e){GridView1.EditIndex = -1;this.bind();}

4.Command对象删除数据

 /// <summary>/// 删除数据/// </summary>/// <param name="sender"></param>/// <param name="e"></param>protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e){int cid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());string strdelete = "delete from userinfo where id=" + cid;SqlConnection con = getcon();con.Open();SqlCommand mycmd = new SqlCommand(strdelete,con);mycmd.ExecuteNonQuery();mycmd.Dispose();con.Close();this.bind();}protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){if (e.Row.RowType == DataControlRowType.DataRow){((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "return confirm('确定要删除这天数据吗 ?')");}}

转载于:https://www.cnblogs.com/wishjm/p/5733517.html

相关文章:

浅析C语言之uint8_t / uint16_t / uint32_t /uint64_t

一、C语言基本数据类型回顾 在C语言中有6种基本数据类型&#xff1a;short、int、long、float、double、char 1、数值类型 1&#xff09;整型&#xff1a;short、int、long 2&#xff09;浮点型&#xff1a;float、double 2、字符类型&#xff1a;char 二、typedef回顾 …

【ACM】UVa 489 刽子手游戏(自顶向下)

【题目】 Hangman Judge是一个猜英文单字的小游戏&#xff08;在电子字典中常会看到&#xff09;&#xff0c;游戏规则如下&#xff1a; 1、答案单字写在纸上&#xff08;每个字元一张纸&#xff09;&#xff0c;并且被盖起来&#xff0c;玩家每次猜一个英文字元&#xff08;le…

ssh远程执行多个命令

shell远程执行&#xff1a; 经常需要远程到其他节点上执行一些shell命令&#xff0c;如果分别ssh到每台主机上再去执行很麻烦&#xff0c;因此能有个集中管理的方式就好了。一下介绍两种shell命令远程执行的方法。 前提条件&#xff1a; 配置ssh免密码登陆 对于简单的命令&am…

【ACM】魔方矩阵

输出魔方矩阵 1、将1放在第一行中间一列&#xff1b; 2、从2开始直到nn止各数依次按下列规则存放&#xff1b;每一个数存放的行比前一个数的行数减1&#xff0c;列数加1&#xff1b; 3、如果上一个数的行数为1&#xff0c;则下一个数的行数为n&#xff08;指最下一行&#x…

iOS 秒数转换成时间,时,分,秒

//转换成时分秒 - (NSString *)timeFormatted:(int)totalSeconds{ int seconds totalSeconds % 60; int minutes (totalSeconds / 60) % 60; int hours totalSeconds / 3600; return [NSString stringWithFormat:"%02d:%02d:%02d",hours, minutes,…

charles和Fiddler感觉哪个更好用

1.fiddler还可以抓HTTPS的包&#xff0c;解析出来都可以2.charles更直观&#xff0c;可能是我先用charles的缘故。charles遍历一个站点&#xff0c;可以右键另存&#xff0c;保存全站文件资源。扒站首选&#xff0c; charles也可以抓https&#xff0c;我改游戏也是抓的https包

systemd用法

一、开机启动 对于那些支持 Systemd 的软件&#xff0c;安装的时候&#xff0c;会自动在/usr/lib/systemd/system目录添加一个配置文件。 如果你想让该软件开机启动&#xff0c;就执行下面的命令&#xff08;以httpd.service为例&#xff09;。 $ sudo systemctl enable http…

C#实现php的hash_hmac函数

from:http://blog.csdn.net/ciaos/article/details/12618487 PHP代码示例如下<?php $res1 hash_hmac("sha1","signatureString", "secret");echo $res1."\n";//ee1b654aa861c41fd5813dc365ef106c9801f8f6echo base64_encode($res…

【ACM】杭电OJ 2015

注意输出格式&#xff01;&#xff01;&#xff01;&#xff01; #include <iostream> #include <cstring> using namespace std; int main () {int m,n,i,sum,flag;while(cin>>n>>m){sum0;flag0;for(i1;i<n;i){sum(2*i);flag;if(flagm){sum/m;cou…

AGC002[BCDEF]题解

F是计数于是就做&#xff08;kan ti jie&#xff09;了 B - Box and Ball 模拟一下 每个盒子开一个d表示有的球数 可能存在红球的打个标记 传递一下就行了 #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define inf 2002…

物联网11种通信协议

今天的网络通信技术也是日新月异&#xff0c;有众所周知的WIFI、Bluetooth、Zigbee、2G、3G、4G蜂窝网络&#xff0c;也有新兴的LiFi、AirGig、量子通信等&#xff0c;更有物联网产业爆发前夜&#xff0c;市场衍生出来的一些比较有前景的通信技术&#xff0c;如以窄带物联网NB&…

php 数组的使用

2019独角兽企业重金招聘Python工程师标准>>> 一、字符串和对象&#xff0c;数组之间的相互转换 public function index(){$product array();$product["name"] "apple";$product["price"] 6000;$products array();$products[] $pr…

【ACM】图像旋转

逆时针 //图像旋转 #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> using namespace std; int main () {int a[105][105];int m,n,i,j;while(scanf("%d%d",&n,&m)!EOF)//n行m列 {for(i0;i<n;i…

do一下来了一个redux

导语 一开看redux的时候还是比较蒙的&#xff0c;感觉比较绕&#xff0c;但是又好像是那么回事&#xff0c;接触一个新概念的时候可能都是如此&#xff0c;多去接触就熟悉了&#xff0c;今天就来分享下redux的三大核心为什么就能如此神奇的施展魔法&#xff0c;干撸完源码&…

JavaMail API 概述

JavaMail API提供了一种与平台无关和协议独立的框架来构建邮件和消息应用程序。 JavaMail API提供了一组抽象类定义构成一个邮件系统的对象。它是阅读&#xff0c;撰写和发送电子信息的可选包&#xff08;标准扩展&#xff09;。 JavaMail 规定&#xff0c;用于构造一个接口&am…

利用c语言结构体和union实现类似c++的public,private的实现

最近在看strongswan源代码&#xff0c;看到strongswan的代码框架很有意思&#xff0c;用C语言实现类的思想。当我们编写完一个模块&#xff0c;我们需要提供的是H的文件给其他模块使用&#xff0c;我们希望H文件中就只能包含一些公有函数&#xff0c;和一些类型的申明&#xff…

【ACM】连续出现的字符

【描述】给定一个字符串&#xff0c;在字符串中找到第一个连续出现k次的字符 【输入】第一行包含一个正整数k&#xff0c;表示至少需要连续出现的次数。1<k<1000。第二行包含需要查找的字符串。字符串的长度在1到1000之间&#xff0c;且不包含任何空白字符。 【输出】若…

Django使用数据库(Mariadb/Mysql)

Django默认使用SQLite作为数据库&#xff0c;配置文件在settings.py 让我们来看一下 """ Django settings for test1 project.Generated by django-admin startproject using Django 2.1.4.For more information on this file, see https://docs.djangoproject.…

I2C和SPI总线优缺点对比

IIC vs SPI现今&#xff0c;在低端数字通信应用领域&#xff0c;我们随处可见IIC (Inter-Integrated Circuit) 和 SPI (Serial Peripheral Interface)的身影。原因是这两种通信协议非常适合近距离低速芯片间通信。Philips&#xff08;for IIC&#xff09;和Motorola&#xff08…

查看CentOS的网络带宽出口

检查维护系统的时候&#xff0c;经常会要查看服务器的网络端口是多大的&#xff0c;所以需要用到Linux的一个命令。 如何查看CentOS的网络带宽出口多大&#xff1f;可以用下面的命令来查看。 # ethtool eth0 前面是命令&#xff0c;后面跟的是设备名&#xff0c;如果对外连接的…

【ACM】删数问题(待更)

【描述】键盘输入一个正整数N&#xff0c;去掉其中任意S个数字后剩下的数字按原左右次序将组成一个新的正整数。编程对给定的N和S寻找一种方案使得剩下的数字组成的新数最小。&#xff08;N不超过240位&#xff0c;N>S&#xff09; 【输入】两行&#xff0c;第一行&#xf…

2019,商业智能的10大未来趋势

2019独角兽企业重金招聘Python工程师标准>>> 当我们深思熟虑接下来会发生什么时&#xff0c;Tableau 收集了来自内外部专家的广泛意见。内部专家们把握着行业的脉搏&#xff0c;并与世界各地成千上万的客户接洽交流&#xff1b;外部专家们则与众多数据团队并肩作战&…

c语言信号机制以及中断

用户态到内核态切换途径&#xff1a; 1&#xff1a;系统调用 2&#xff1a;中断 3&#xff1a;异常 中断类型分为如下两大类&#xff1a; 一、强迫性中断&#xff1a;正在运行的程序所不期望的&#xff0c;来自硬件故障或外部请求。 1、I/O 中断&#xff1a;来自…

【ACM】纸牌搭建

【题目】现有N张扑克牌&#xff0c;最多可以搭建几层 【题目分析】找到通项公式 f[ i ]f[ i-1 ]3*i-1。先打出表&#xff0c;再二分搜索。不断缩小范围。 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using na…

DataBase 之 拉链表结构设计

一、概念 拉链表是针对数据仓库设计中表存储数据的方式而定义的&#xff0c;顾名思义&#xff0c;所谓拉链&#xff0c;就是记录历史。记录一个事物从开始&#xff0c;一直到当前状态的所有变化的信息。 在历史表中对客户的一生的记录可能就这样几条记录&#xff0c;避免了按每…

给每个函数写一个记录日志的功能.

# 功能要求: 每一次调用函数之前, 要将函数名称, 时间节点记录到log的日志中.# 所需模块:# import time## def logger(fn):# def inner(*args, **kwargs):# # fn.__name__ # 函数名字# f open("log", mode"a", encoding"utf-8&q…

c如何正常中断一个运行的线程

最近开发一些东西&#xff0c;线程数非常之多&#xff0c;当用户输入CtrlC的情形下&#xff0c;默认的信号处理会把程序退出&#xff0c;这时有可能会有很多线程的资源没有得到很好的释放&#xff0c;造成了内存泄露等等诸如此类的问题&#xff0c;本文就是围绕着这么一个使用场…

Vertica 分区表设计(续)

在上篇Vertica 分区表设计中&#xff0c;已经提过了Vertica的分区表创建和分区删除&#xff0c;但举例上并不系统&#xff0c; 本篇文章将系统的对分区表设计及后续的删除分区进行讲解。 概述&#xff1a;Vertica分区表&#xff08;天和月&#xff09;创建以及删除分区 1.分区表…

【ACM】杭电OJ 1181

http://acm.hdu.edu.cn/showproblem.php?pid1181 DFS搜索&#xff08;递归函数&#xff09; #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> using namespace std; char s[1000]; int k…

最热开源无服务器函数:五大Fission架构参考

“无服务器”现在是极具诱惑的技术趋势&#xff0c;没有什么比管理服务器更让人痛苦。亚马逊、微软和谷歌都在云中提供无服务器专有接口。相较于这些云供应商的商业化产品&#xff0c;开源无服务器架构可免于被云厂商锁定&#xff0c;但要以牺牲云便利性和易用性为代价。近一年…