ADO.NET基础学习 二(Command对象)
②command对象用来操作数据库。(三个重要的方法:ExecuteNonQuery(),ExecuteReader(),ExecuteScalar())
⑴以update(改数据)为例,用到ExecuteNonQuery()方法(执行SQL语句,返回受影响行)
private void button2_Click(object sender, EventArgs e)
{
SqlConnection conn =new SqlConnection("server=.;Initial catalog=db_PWMS;integrated security=SSPI");
conn.Open();//老规矩,先连接
try
{
SqlCommand cmd = new SqlCommand();//实例操作项cmd
cmd.Connection = conn;//操作conn这个数据库
cmd.CommandText = "update Table_1 set Prices =3333 where Origin ='国产'";//操作这样一句SQL语句
cmd.CommandType = CommandType.Text;//书上这么写的,不知道干嘛的,以后知道了再说。去掉这句话也没事。
cmd.ExecuteNonQuery();//command对象重要的三个方法之一,执行增删改
int i = Convert.ToInt32(cmd.ExecuteNonQuery());
label2.Text = i + "条数据发生改动";
}
catch (Exception ex){ MessageBox.Show(ex.Message); }
}
点击事件(button2)
执行前数据库

执行后

⑵以各种姿势查数据ExecuteScalar()方法(执行SQL语句,返回结果集中第一行第一列),但此方法通常与聚合函数一起使用
此方法的聚合函数
| 说明 | |
| AVG() | 平均值 |
| count(列名)/count(*) | 此列值的计数(不包括空值)/此表所有行的计数(包括空值) |
| max() | 最大值 |
| min() | 最小值 |
| sum() | 和 |
以count()和max()为例
private void button3_Click(object sender, EventArgs e)
{
conn = new SqlConnection("server=.;Initial catalog=db_PWMS;integrated security=SSPI");
conn.Open();
try
{
string s1 = "select count (*) from Table_1";//表数量count()
string s2 = "select max (Prices) from Table_1";//Prices最大值max() SqlCommand cmd = new SqlCommand(s1,conn);
SqlCommand cmd1 = new SqlCommand(s2,conn);
int i = Convert.ToInt32(cmd.ExecuteScalar());//对象转int类型
int j = Convert.ToInt32(cmd1.ExecuteScalar()); label2.Text = i+"条数据";
label1.Text = "最贵的" + j;
}
catch (Exception ex){ MessageBox.Show(ex.Message); }
}
⑶ExecuteReader()方法(执行SQL语句,生成一个SqlDataReader对象的实例,返回 一个SqlDataReader对象)
private void button5_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;Initial catalog=db_PWMS;integrated security=SSPI");
conn.Open();//连接并打开
SqlCommand cmd = new SqlCommand("select * from Table_1",conn);//操作
SqlDataReader sdr = cmd.ExecuteReader();//ExecuteReader方法实例化个SqlDataReader对象
while (sdr.Read())//SqlDataReader的Read()方法 循环读取数据
{
label3.Text += sdr[].ToString();//读取第一列
listView1.Items.Add(sdr[].ToString());//读取第二列
}
}

ADO.NET基础学习 二(Command对象)的更多相关文章
- Go基础学习(二)
数组[array] 数组定义[定义后长度不可变] 12 symbol := [...]string{USD: "$", EUR: "€", GBP: " ...
- Python入门基础学习 二
Python入门基础学习 二 猜数字小游戏进阶版 修改建议: 猜错的时候程序可以给出提示,告诉用户猜测的数字偏大还是偏小: 没运行一次程序只能猜测一次,应该提供多次机会给用户猜测: 每次运行程序,答案 ...
- Python基础学习二
Python基础学习二 1.编码 utf-8编码:自动将英文保存为1个字符,中文3个字符.ASCll编码被囊括在内. unicode:将所有字符保存为2给字符,容纳了世界上所有的编码. 2.字符串内置 ...
- java基础学习总结六(对象与类、类的属性与方法)
一:面向过程与面向对象的区别 举例:一个人开门的动作,可以分解为开门,人进去,关门. 面向过程:人作为执行者,1:开门 2:进入 3:关门 面向对象:人作为指挥者,将开门,关门的动作都封装到门上 ...
- Django基础学习二
今天继续学习django的基础 学习用户提交url如何获得返回值 1.首先需要在工程的urls文件定义指定的urls要路由给哪个函数 在这个例子中,我们定义home的urls路由给views里的tes ...
- 基础学习day06---面向对象二---static,类的初始化和调用顺序、单例模式
一.static关键字 1.1.static关键字 静态:static用法:是一个修饰符,用于修饰成员(成员变量,成员函数)static 修饰的内容,所有对象共享当成员被静态修饰后,就多了一个调用方式 ...
- ADO.net基础学习总结(二)
将连接字符串放入配置文件中 1.添加一个“应用程序配置文件:app.config” <?xml version="1.0" encoding="utf-8" ...
- salesforce lightning零基础学习(二) lightning 知识简单介绍----lightning事件驱动模型
看此篇博客前或者后,看一下trailhead可以加深印象以及理解的更好:https://trailhead.salesforce.com/modules/lex_dev_lc_basics 做过cla ...
- WebService基础学习(二)—三要素
一.Java中WebService规范 JAVA 中共有三种WebService 规范,分别是JAX-WS.JAX-RS.JAXM&SAAJ(废弃). 1.JAX-WS规范 ...
随机推荐
- 28 ArcMap 运行特别慢怎么办
小编电脑配置如下: , 虽然不是太好吧,但还是满足ArcMap运行的要求的,但不知道为什么,就是很慢,终于在无意中,发现了一个位置,取消勾选以后,ArcMap变的快很多,亲测有效 取消后台处理后,Ar ...
- Tensorboard可视化
# -*- coding: utf-8 -*-"""Created on Sun Nov 5 09:29:36 2017 @author: Admin"&quo ...
- Mesos源码分析(5): Mesos Master的启动之四
5. Create an instance of allocator. 代码如下 Mesos源码中默认的Allocator,即HierarchicalDRFAllocator的位置在$ME ...
- 基于LinkedList实现桶排序
需要考虑以下问题: 1.桶的大小,这里我们可以根据输入的元素的个数来确定桶的大小. 2.怎么样确定当前元素进入哪一个桶,这里我们使用到的是通过一个哈希函数来进行计算. int index = (ele ...
- Oracle客户端、服务的安装及干净卸载Oracle
软件下载地址: 链接:https://pan.baidu.com/s/1Sluf890eNuaV8muL55eO2w 提取码:oez7 服务端因文件过大,所以分了两个文件压缩包,下载后将内容解压后放置 ...
- [Swift]LeetCode199. 二叉树的右视图 | Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- [Swift]LeetCode254.因子组合 $ Factor Combinations
Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...
- [Swift]LeetCode365. 水壶问题 | Water and Jug Problem
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- [Swift]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor
Given a non-empty array of unique positive integers A, consider the following graph: There are A.len ...
- CentOS7 Linux中通过加密grub防止黑客通过单用户系统破解root密码
如何防止别人恶意通过单用户系统破解root密码,进入系统窃取数据? 给grub加密,不让别人通过grub进入单用户. 17.3.1 基于centos6进行grub加密 [root@63 ~]# gr ...