ADO.NET基础学习 二(Command对象)

②command对象用来操作数据库。(三个重要的方法:ExecuteNonQuery(),ExecuteReader(),ExecuteScalar())

⑴以update(改数据)为例,用到ExecuteNonQuery()方法(执行SQL语句,返回受影响行)

1private void button2_Click(object sender, EventArgs e) 2 { 3 SqlConnection conn =new SqlConnection("server=.;Initial catalog=db_PWMS;integrated security=SSPI"); 4 conn.Open();//老规矩,先连接 5 try 6 { 7 SqlCommand cmd = new SqlCommand();//实例操作项cmd 8 cmd.Connection = conn;//操作conn这个数据库 9 cmd.CommandText = "update Table_1 set Prices =3333 where Origin ='国产'";//操作这样一句SQL语句 10 cmd.CommandType = CommandType.Text;//书上这么写的,不知道干嘛的,以后知道了再说。去掉这句话也没事。 11 cmd.ExecuteNonQuery();//command对象重要的三个方法之一,执行增删改 12 int i = Convert.ToInt32(cmd.ExecuteNonQuery()); 13 label2.Text = i + "条数据发生改动"; 14 } 15 catch (Exception ex){ MessageBox.Show(ex.Message); } 16 }

点击事件(button2)

执行前数据库

 

执行后

 ⑵以各种姿势查数据ExecuteScalar()方法(执行SQL语句,返回结果集中第一行第一列),但此方法通常与聚合函数一起使用

此方法的聚合函数

 

说明

AVG()

平均值

count(列名)/count(*)

此列值的计数(不包括空值)/此表所有行的计数(包括空值)

max()

最大值

min()

最小值

sum()

以count()和max()为例

1private void button3_Click(object sender, EventArgs e) 2 { 3 conn = new SqlConnection("server=.;Initial catalog=db_PWMS;integrated security=SSPI"); 4 conn.Open(); 5 try 6 { 7 string s1 = "select count (*) from Table_1";//表数量count() 8 string s2 = "select max (Prices) from Table_1";//Prices最大值max() 9 10 SqlCommand cmd = new SqlCommand(s1,conn); 11 SqlCommand cmd1 = new SqlCommand(s2,conn); 12 int i = Convert.ToInt32(cmd.ExecuteScalar());//对象转int类型 13 int j = Convert.ToInt32(cmd1.ExecuteScalar()); 14 15 label2.Text = i+"条数据"; 16 label1.Text = "最贵的" + j; 17 } 18 catch (Exception ex){ MessageBox.Show(ex.Message); } 19 }

⑶ExecuteReader()方法(执行SQL语句,生成一个SqlDataReader对象的实例,返回 一个SqlDataReader对象)

1private void button5_Click(object sender, EventArgs e) 2 { 3 SqlConnection conn = new SqlConnection("server=.;Initial catalog=db_PWMS;integrated security=SSPI"); 4 conn.Open();//连接并打开 5 SqlCommand cmd = new SqlCommand("select * from Table_1",conn);//操作 6 SqlDataReader sdr = cmd.ExecuteReader();//ExecuteReader方法实例化个SqlDataReader对象 7 while (sdr.Read())//SqlDataReader的Read()方法 循环读取数据 8 { 9 label3.Text += sdr[1].ToString();//读取第一列 10 listView1.Items.Add(sdr[2].ToString());//读取第二列 11 } 12 }

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

MySQL 临时表的原理以及优化手段

1临时表sortbuffer、内存临时表和joinbuffer,这三个数据结构都是用来存放语句执行过程中的中间数据,以辅助SQL语句的执行的。其中,在排序的时候用到了sortbuffer,在使用join语句的时候用到了joinbuffer。而使用临时表的时候,Explain的Extra字段中具有Usingtemporary标记。union、gro

Python3:sqlalchemy对mysql数据库操作,非sql语句

Python3:sqlalchemy对mysql数据库操作,非sql语句python3authorlizmdatetime2018020110:00:00coding:utf8'''

Python-调用运行系统命令

os.popen方法可以获取到返回内容languageHeadTextos.popen('sedn1p\"\"'.format(DirFile)).read()os.system方法执行运行命令Command"sh/home/TradeInfo/new/tradeplan/py/CopyTradeplan.sh"os.system(Comm

Python 常用的ORM框架简介

ORM概念ORM(ObjectRalationalMapping,对象关系映射)用来把对象模型表示的对象映射到基于SQL的关系模型数据库结构中去。这样,我们在具体的操作实体对象的时候,就不需要再去和复杂的SQL语句打交道,只需简单的操作实体对象的属性和方法。ORM技术是在对象和关系之间提供了一条桥梁,前台的对象型数据和数据

Python Django 之 直接执行自定义SQL语句(一)

一、执行自定义SQL方法1、ExecutingcustomSQLdirectly   直接执行自定义SQL,这种方式可以完全避免数据模型,而是直接执行原始的SQL语句。2、Manager.raw()   执行原始查询并返回模型实例二、ExecutingcustomSQLdire

ADO.NET基础学习 二(Command对象) - HelloWorld