一.了解Command对象

  1.Command对象:封装了所有对外部数据源的操作,包括增删改查和执行存储过程,并在执行完成后返回合适的结果,同Connection一样,对于不同的数据源,Ado.net有不同的类

  2.重要的属性:

    ①.CommandText:设置对数据库进行的操作

    ②.CommandType:设置操作的类型

      值为"1":表示CommandText中的内容为Sql语句

      值为"4":表示CommandText中的内容为存储过程

    ③.Connection:获取或设置与数据源的连接

    ④.Parameters:绑定Sql语句或存储过程的参数

    ⑤.Tranction:获取或设置在其中执行.NET Framework数据提供程序的Command对象的事务

  3.重要的方法:

    ①.ExecuteNonQuery:执行不返回数据行的操作,并返回一个int类型的数据,在对数据库进行Update,Insert,Delete的操作时,返回该语句影响的行数,对于其他所有类型的语句,返回值为-1

    ②.ExecuteReader:返回DataReader类型的对象

    ③.ExecuteScalar:返回结果集中的第一行第一列,如果没有返回null

  4.创建Command对象

    ①.通过构造函数创建对象:

string strSQL = "Select * from tb_SelCustomer";
SqlCommand cmd = new SqlCommand(strSQL, conn);

    ②.通过Command对象的属性:

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = strSQL;

  5.增删改

 using System;
 using System.Collections.Generic;
 using System.Data;
 using System.Data.SqlClient;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

 namespace ConsoleApp5
 {
     class Program
     {
         static void Main(string[] args)
         {
             SqlConnectionStringBuilder conStr = new SqlConnectionStringBuilder();
             conStr.DataSource = "DESKTOP-0MBGCKA\\SQL2016";
             conStr.InitialCatalog = "test";
             conStr.IntegratedSecurity = true;
             SqlConnection conn = new SqlConnection();
             conn.ConnectionString = conStr.ConnectionString;
             SqlCommand com = conn.CreateCommand();
             //com.CommandText = "delete from classmate where ID=2";//删除一条记录
             //com.CommandText = "insert into classmate values('燕子')";//新增一条记录
             com.CommandText = "update classmate set Name='柳岩' where ID =3 ";//修改一条记录
             conn.Open();
             int i = com.ExecuteNonQuery();
             conn.Close();
             conn.Dispose();
             )
             {
                 Console.WriteLine("删除成功");
             }
             else
             {
                 Console.WriteLine("删除失败");
             }
             Console.ReadKey();
         }
     }
 }

  6.查询数据

    ①.返回多条数据时使用ExecuteReader()方法,返回一个DataReader类型的对象,该对象包含一行数据,read()方法,在没有下一行时返回false.

      特别注意,DataReader类型的对象在用完有需要close掉,Sql Server默认一次只能打开一个DataReader对象

 using System;
 using System.Collections.Generic;
 using System.Data;
 using System.Data.SqlClient;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

 namespace ConsoleApp5
 {
     class Program
     {
         static void Main(string[] args)
         {
             //创建连接字符串
             SqlConnectionStringBuilder strConn = new SqlConnectionStringBuilder();
             strConn.DataSource = "DESKTOP-0MBGCKA\\SQL2016";
             strConn.InitialCatalog = "SS";
             strConn.IntegratedSecurity = true;
             using (SqlConnection conn = new SqlConnection(strConn.ConnectionString))
             {
                 string strSql = "select * from Student";
                 SqlCommand cmd = new SqlCommand(strSql, conn);
                 conn.Open();
                 try
                 {
                     SqlDataReader reader = cmd.ExecuteReader();
                     if (reader != null && reader.HasRows)
                     {
                         ;
                         Console.WriteLine("*****************************************\n");
                         while (reader.Read())
                         {
                             ; i < reader.FieldCount; ++i)
                             {
                                 Console.WriteLine("{0}:{1}", reader.GetName(i), reader.GetValue(i));
                             }
                             ++rows;
                         }
                         Console.WriteLine("\n共{0}行记录", rows);
                     }
                     reader.Close();
                 }
                 catch (Exception ex)
                 {
                     Console.WriteLine("\nError:\n{0}", ex.Message);
                 }
             }
             Console.Read();
         }
     }
 }

    ②.查询结果返回一个值时,使用ExecuteScalar()方法,该方法返回一个object类型的对象,在调用该返回值时需要进行强制转换

 using System;
 using System.Collections.Generic;
 using System.Data;
 using System.Data.SqlClient;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

 namespace ConsoleApp5
 {
     class Program
     {
         static void Main(string[] args)
         {
             //创建连接字符串
             SqlConnectionStringBuilder strConn = new SqlConnectionStringBuilder();
             strConn.DataSource = "DESKTOP-0MBGCKA\\SQL2016";
             strConn.InitialCatalog = "SS";
             strConn.IntegratedSecurity = true;
             using (SqlConnection conn = new SqlConnection(strConn.ConnectionString))
             {
                 string strSql = "select count(*) from Student";
                 SqlCommand cmd = new SqlCommand(strSql, conn);
                 conn.Open();
                 try
                 {
                     int row = (int)cmd.ExecuteScalar();
                     Console.WriteLine("共有{0}行数据", row);
                 }
                 catch (Exception ex)
                 {
                     Console.WriteLine("\nError:\n{0}", ex.Message);
                 }
             }
             Console.Read();
         }
     }
 }

Ado.net之对数据库的增删改查的更多相关文章

  1. Asp.net MVC4 使用EF实现数据库的增删改查

    EF的使用 步骤: (1)将EF添加到项目:在Model右击添加新建项 找到ADO.NET实体数据模型,接着... (2)实现数据库的增删改查       查询 (因为在Model中已经添加EF实体了 ...

  2. 使用EF实现数据库的增删改查

    EF的使用步骤:(1)将EF添加到项目:在Model右击添加新建项找到ADO.NET实体数据模型,接着…(2)实现数据库的增删改查查询(因为在Model中已经添加EF实体了,所以就可以在Control ...

  3. ThinkPHP实现对数据库的增删改查

    好久都没有更新博客了,之前老师布置的任务总算是现在可以说告一段落了,今天趁老师还没提出其他要求来更新一篇博客. 今天我想记录的是我之前做项目,自己所理解的ThinkPHP对数据库的增删改查. 首先要说 ...

  4. Android学习---数据库的增删改查(sqlite CRUD)

    上一篇文章介绍了sqlite数据库的创建,以及数据的访问,本文将主要介绍数据库的增删改查. 下面直接看代码: MyDBHelper.java(创建数据库,添加一列phone) package com. ...

  5. Android 系统API实现数据库的增删改查和SQLite3工具的使用

    在<Android SQL语句实现数据库的增删改查>中介绍了使用sql语句来实现数据库的增删改查操作,本文介绍Android 系统API实现数据库的增删改查和SQLite3工具的使用. 系 ...

  6. Android SQL语句实现数据库的增删改查

    本文介绍android中的数据库的增删改查 复习sql语法: * 增 insert into info (name,phone) values ('wuyudong','111') * 删 delet ...

  7. java jdbc 连接mysql数据库 实现增删改查

    好久没有写博文了,写个简单的东西热热身,分享给大家. jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打 ...

  8. 【转载】通过JDBC对MySQL数据库的增删改查

    通过JDBC进行简单的增删改查(以MySQL为例) 目录 前言:什么是JDBC 一.准备工作(一):MySQL安装配置和基础学习 二.准备工作(二):下载数据库对应的jar包并导入 三.JDBC基本操 ...

  9. 利用API方式进行数据库的增删改查

    /* 将数据库的增删改查单独放进一个包 */ package com.itheima28.sqlitedemo.dao; import java.util.ArrayList; import java ...

随机推荐

  1. C语言gcc处理过程

    gcc编译C文件一共四步,预处理(Preprocess),编译(Compilation),汇编(Assembly)和链接(Linking) 1. 预处理(Preprocess) 预处理是预处理中会展开 ...

  2. 关于Ble通信库BluetoothKit的使用 以及可能出现的问题分析

    首先,这个库是用于BLE(低功耗蓝牙)通信的,地址:https://github.com/dingjikerbo/BluetoothKit 当然,也可以选择根据andorid提供的底层接口自己完成这部 ...

  3. 官网下载MySQL最新版本的安装包

    下载地址:http://www.mysql.com/downloads/ 1.选择下载社区版本 MySQL Community Edition (GPL)Community (GPL) Downloa ...

  4. Windows 局域网ping获取设备IP

    /********************************************************************** * Windows 局域网ping获取设备IP * 说明 ...

  5. BZOJ - 3170: 松鼠聚会 (切比雪夫转曼哈顿距离)

    pro:  有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1.现在N个松鼠要走到一个松鼠家去,求走过的最短距离.0&l ...

  6. python3配置 opencv

    python3配置 opencv 本文适用于windows 64位系统 下的Python3.5.python3.5.pip为必备前提. 配置过程: 第一步:打开cmd命令行窗口 第二步:输入pip指令 ...

  7. 第3章 Java数组(上): 一维数组和二维数组

    3.数组及排序算法(2天) 3.1 数组的概述 2课时 3.2 一维数组的使用 3课时 3.3 多维数组的使用 3课时 3.4 数组中涉及到的常见算法 3课时 3.5 Arrays工具类的使用 3课时 ...

  8. zabbix与tomcat(六)

    一.zabbix监控远程tomcat的流程   Zabbix-server 找 zabbix Java Gateway获取Java数据 zabbix Java Gateway 找Java程序(zabb ...

  9. 1.1.8 怎样在Word的页眉中插入一级标题

    可以通过域来实现,其具体的操作步骤: 1.为章.节标题使用标题样式.例如:章标题使用标题1样式,节标题使用标题2样式.操作方法:选中章(节)标题,然后点击选项卡中“样式”中的). 2.设置文档页眉和页 ...

  10. Excel技巧--反向查询

    当要从左侧的表格,查询某人所在的部门时,那么需要逆向查询.VLOOKUP函数只能正向查询.可以使用Match和index函数: Match函数:查询某个值在指定区域所在的位置: Index函数:查询指 ...