Ado.net之对数据库的增删改查
一.了解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之对数据库的增删改查的更多相关文章
- Asp.net MVC4 使用EF实现数据库的增删改查
EF的使用 步骤: (1)将EF添加到项目:在Model右击添加新建项 找到ADO.NET实体数据模型,接着... (2)实现数据库的增删改查 查询 (因为在Model中已经添加EF实体了 ...
- 使用EF实现数据库的增删改查
EF的使用步骤:(1)将EF添加到项目:在Model右击添加新建项找到ADO.NET实体数据模型,接着…(2)实现数据库的增删改查查询(因为在Model中已经添加EF实体了,所以就可以在Control ...
- ThinkPHP实现对数据库的增删改查
好久都没有更新博客了,之前老师布置的任务总算是现在可以说告一段落了,今天趁老师还没提出其他要求来更新一篇博客. 今天我想记录的是我之前做项目,自己所理解的ThinkPHP对数据库的增删改查. 首先要说 ...
- Android学习---数据库的增删改查(sqlite CRUD)
上一篇文章介绍了sqlite数据库的创建,以及数据的访问,本文将主要介绍数据库的增删改查. 下面直接看代码: MyDBHelper.java(创建数据库,添加一列phone) package com. ...
- Android 系统API实现数据库的增删改查和SQLite3工具的使用
在<Android SQL语句实现数据库的增删改查>中介绍了使用sql语句来实现数据库的增删改查操作,本文介绍Android 系统API实现数据库的增删改查和SQLite3工具的使用. 系 ...
- Android SQL语句实现数据库的增删改查
本文介绍android中的数据库的增删改查 复习sql语法: * 增 insert into info (name,phone) values ('wuyudong','111') * 删 delet ...
- java jdbc 连接mysql数据库 实现增删改查
好久没有写博文了,写个简单的东西热热身,分享给大家. jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打 ...
- 【转载】通过JDBC对MySQL数据库的增删改查
通过JDBC进行简单的增删改查(以MySQL为例) 目录 前言:什么是JDBC 一.准备工作(一):MySQL安装配置和基础学习 二.准备工作(二):下载数据库对应的jar包并导入 三.JDBC基本操 ...
- 利用API方式进行数据库的增删改查
/* 将数据库的增删改查单独放进一个包 */ package com.itheima28.sqlitedemo.dao; import java.util.ArrayList; import java ...
随机推荐
- 剑指Offer 38. 二叉树的深度 (二叉树)
题目描述 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 题目地址 https://www.nowcoder.com/prac ...
- vue 安卓5.1 ios9 兼容性 白屏问题
// 针对安卓4.4/ios的兼容 import 'babel-polyfill' import Es6Promise from 'es6-promise' require('es6-promise' ...
- SpringMVC + MyBatis分库分表方案
mybatis作为流行的ORM框架,项目实际使用过程中可能会遇到分库分表的场景.mybatis在分表,甚至是同主机下的分库都可以说是完美支持的,只需要将表名或者库名作为动态参数组装sql就能够完成.但 ...
- Dijkstra(迪杰斯特拉)模板
直接将模板封装在结构体里面. struct Edge{ int from,to,dist; Edge(int u, int v,int d): from(u),to(v),dist(d){} }; s ...
- [Split The Tree][dfs序+树状数组求区间数的种数]
Split The Tree 时间限制: 1 Sec 内存限制: 128 MB提交: 46 解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...
- Win下更新pip出现OSError:[WinError17]与PerrmissionError:[WinError5]及解决
环境:Win7 64位,python3.6.0 我在准备用pip装东西的时候,在cmd里先更新了一下pip,大概是9.0.1更新到9.0. 尝试更新pip命令: pip install --upgra ...
- PythonStudy——数据类型 Type of data
数据类型:信息存在的状态为什么要来描述事物的具体状态:不同的事物需要不同的状态加以描述可以达到描述的最优化 python中有哪些常见的数据类型 1.整型 num = 10000000000000000 ...
- Game Physics Cookbook (Gabor Szauer 著)
Chapter1: Vectors Chapter2: Matrices Chapter3: Matrix Transformations Chapter4: 2D Primitive Shapes ...
- 使用tcpdump测试反向代理和lvs的nat区别
关于反向代理,一个请求过来,实际反向代理服务器要和两个对象做3次握手 客户端到反向代理服务器,是一个3次握手 反向代理服务器请求后端web服务器,是一个3次握手 lvs的nat和反向代理不同. lvs ...
- 第三章 C#程序结构[3.2 选择结构的应用(Windows窗体应用程序)(四)]
[案例]设计一个顾客选购商品的系统.其中,顾客身份有两类,一类是VIP,另一类是普通会员:商品种类有3种.分别是上衣.裤子和鞋子.其中,VIP享受8折优惠和商店赠送的礼品,而普通会员都不享受.单击[确 ...