一.了解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. Python学习笔记第二十六周(Django补充)

    一.基于jQuery的ajax实现(最底层方法:$.jax()) $.ajax( url: type:''POST“ ) $.get(url,[data],[callback],[type])  #c ...

  2. 选择器(ID选择器)

    ID选择器: 为HTML标签添加ID属性: <p id="p1">内容1</p> <p id="p2">内容2</p& ...

  3. 负载均衡集群相关、LVS介绍、LVS调度算法、LVS NAT模式搭建

    1.负载均衡集群相关 2.LVS的三种模式:NAT.DR .IP tunnel 3. LVS的调度算法(共有8种) 4.LVS NAT模式搭建准备条件:   在分发服务器上安装:yum install ...

  4. java.net.BindException: Address already in use: JVM_Bind:80 异常的解决办法

    今天遇见了这个端口被占用问题 然后各种百度 先是说 用命令 netstat -a -n -o 最后一个选项表示连接所在进程id. 找到8080端口的PID然后打开任务管理器, 切换到进程选项卡, 在菜 ...

  5. angular6新建项目

    mkdir  angular6project cd angular6project ng new demo      新建一个普通项目 ng new demo --routing  新建一个带路由的项 ...

  6. smartgit的安装

    smartgit是非常好用的一个图形化git工具,有Ubuntu版本的,直接去官网下载即可,但使用smartgit要求先jre. 直接去官网上下载jre:https://www.java.com/zh ...

  7. 前后端如何保持长连接?---websocket

    1. pc端的应用,一般会采用前端定时请求后台; 2. app定时去访问后台的话,对用户来说并不友好,会消耗大量的流量,移动端最好的方式就是后台主动向app推送信息; 3. H5提供了一种比较好的方式 ...

  8. [Java] 例外處裡 try/catch & throws

    public class CheckException { public static void main(String[] args) { File file = new File("xx ...

  9. CSV表格融合

    常用表格融合函数 1 merge() 用于融合的函数 https://blog.csdn.net/brucewong0516/article/details/82707492 pd.merge(lef ...

  10. PythonStudy1——Python 值拷贝 浅拷贝 深拷贝

    拷贝:对值进行复制的过程 # 值拷贝:应用场景最多  ls = [1, 'abc', [10]] ls1 = ls # ls1直接将ls中存放的地址拿过来  # ls内部的值发生任何变化,ls1都会随 ...