1、什么是ADO.NET?     (是一种数据库访问技术)

ADO.NET的名称起源于ADO(ActiveX Data Objects),是一个COM组件库,用于在以往的Microsoft技术中访问数据。之所以使用ADO.NET名称,是因为Microsoft希望表明,这是在NET编程环境中优先使用的数据访问接口。

2、ADO.NET 的作用?

通过程序来操作数据库

3、连接,基础增删改:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//连接字符串
string sql = "server=.;database=Data0216;user=sa;pwd=123;"; //数据库连接类
SqlConnection conn = new SqlConnection(sql); //数据库操作类
SqlCommand cmd = conn.CreateCommand(); //cmd.CommandText = "insert into Users values('zhaoliu','1234','赵六',1,'2004-4-4','N001');"; //cmd.CommandText = "update Users set NickName = '小六子' where username = 'zhaoliu'"; //编写TSQL语句
cmd.CommandText = "delete from Users where username='zhaoliu'"; //打开数据库连接
conn.Open(); //执行操作
cmd.ExecuteNonQuery(); //关闭数据库连接
conn.Close(); Console.ReadLine();
}
}
}

4、查询:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient; namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string sql = "server=.;database=Data0216;user=sa;pwd=123";
SqlConnection conn = new SqlConnection(sql);
SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select *from Users"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows)
{
while (dr.Read())
{
string ids = dr[].ToString();
string username = dr[].ToString();
string birthday = dr["Birthday"].ToString();
string password = dr["PassWord"].ToString();
string nickname = dr["NickName"].ToString();
string sex = dr["Sex"].ToString();
string nation = dr["Nation"].ToString(); Console.WriteLine(ids + " | " + username + " | " + password + " | " + nickname + " | " + sex + " | " + birthday + " | " + nation);
}
} conn.Close(); Console.ReadKey();
}
}
}

5、完整的增删改:

删除:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string sql = "server=.;database=Data0216;user=sa;pwd=123";
SqlConnection conn = new SqlConnection(sql);
SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "delete from users where username='zhangsan'"; conn.Open(); int a = cmd.ExecuteNonQuery(); if (a > ) Console.WriteLine("删除成功,本次共删除" + a + "行");
else Console.WriteLine("删除失败,本次未删除任何数据"); conn.Close(); Console.ReadLine();
}
}
}

增和改:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text; namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
bool has = false;
string sql = "server=.;database=Data0216;user=sa;pwd=123";
SqlConnection conn = new SqlConnection(sql);
SqlCommand cmd = conn.CreateCommand(); Console.Write("请输入要修改的用户的用户名:");
string name = Console.ReadLine(); cmd.CommandText = "select *from Users where UserName = '" + name + "'";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
has = true;
}
conn.Close(); if (!has)
Console.WriteLine("用户名输入错误!查无此用户");
else
{
Console.WriteLine("已查询到此用户,请输入更改的信息");
Console.Write("请输入修改后的密码:");
string password = Console.ReadLine();
Console.Write("请输入修改后的昵称:");
string nickname = Console.ReadLine();
Console.Write("请输入修改后的性别:");
string sex = Console.ReadLine();
Console.Write("请输入修改后的生日:");
string birthday = Console.ReadLine();
Console.Write("请输入修改后的民族:");
string nation = Console.ReadLine(); cmd.CommandText = "update Users set PassWord='" + password + "',NickName='" + nickname + "',Sex='" + sex + "',Birthday='" + birthday + "',Nation='" + nation + "' where UserName = '" + name + "'";
conn.Open();
int aaa = cmd.ExecuteNonQuery();
conn.Close();
if (aaa > ) Console.WriteLine("修改成功");
else Console.WriteLine("修改失败");
} Console.ReadLine(); }
}
}

2017-4-18 ADO.NET的更多相关文章

  1. 九月 26, 2017 10:18:14 上午 com.sun.jersey.server.impl.application.RootResourceUriRules <init> 严重: The ResourceConfig instance does not contain any root resource classes.

    Tomcat启动错误:九月 26, 2017 10:18:14 上午 com.sun.jersey.server.impl.application.RootResourceUriRules <i ...

  2. 日本IT行业劳动力缺口达22万 在日中国留学生迎来就业好时机 2017/07/18 11:25:09

    作者:倪亚敏 来源:日本新华侨报 发布时间:2017/07/18 11:25:09     据日本政府提供的数据,日本2018年应届毕业生的“求人倍率”已经达到了1.78倍.换言之,就是100名大学生 ...

  3. 2017.7.18 linux下ELK环境搭建

    参考来自:Linux日志分析ELK环境搭建  另一篇博文:2017.7.18 windows下ELK环境搭建   0 版本说明 因为ELK从5.0开始只支持jdk 1.8,但是项目中使用的是JDK 1 ...

  4. 2017.7.18 windows下ELK环境搭建

    参考来自:Windows环境下ELK平台的搭建 另一篇博文:2017.7.18 linux下ELK环境搭建 0 版本说明 因为ELK从5.0开始只支持jdk 1.8,但是项目中使用的是JDK 1.7, ...

  5. 湖南师范大学计算机基础课网络教学平台 版本 V2.0(2017.9.18)

    湖南师范大学计算机基础课网络教学平台 版本 V2.0(2017.9.18) 开发环境: 开发工具:VS2013,数据库:Sqlserver2012 开发语言:Asp.net MVC5 ,界面UI:jq ...

  6. 2017.11.18 手把手教你学51单片机-点亮LED

    In Doing We Learning 在操作中学习.如果只是光看教程,没有实际的操作,对编程语言的理解很空泛,所以决定从单片机中学习C语言. #include<reg52.h>     ...

  7. noip2010 真题练习 2017.2.18

    第一题比较简单,用exist数组判断是否在循环队列中,就可实现线性算法. Code #include<iostream> #include<cstdio> #include&l ...

  8. 团队作业4——第一次项目冲刺(Alpha版本)2017.11.18

    1.当天站立式会议照片 本次会议在5号公寓312召开,本次会议内容:①:熟悉每个人想做的模块.②:根据老师的要求将项目划分成一系列小任务.③:在上次会议内容完成的基础上增加新的任务. 2.每个人的工作 ...

  9. 2017.11.18 IAP下载(STM8,PIC,STM32)

    客户要求用IAP下载,mark一下,客户还给了stm32的引导码.仅供参考. 1 PIC单片机的IAP  2 STm32 IAP https://www.cnblogs.com/WeyneChen/p ...

  10. May 2 2017 Week 18 Tuesday

    The beauty of the journey is found in the scenery along the way. 旅行之美在于沿途所见的风景. Several years ago, I ...

随机推荐

  1. 用smarty来做简易留言系统,明细步骤简单操作

    留言信息是之前用php做过的一个例子,现在把它用smarty模板来做 大概是这样子 点击发布信息 然后填写内容,发送后会返回表格,写的内容都会出现在表格里 数据库的数据是这样的: 先建两个文件.php ...

  2. 强化学习读书笔记 - 06~07 - 时序差分学习(Temporal-Difference Learning)

    强化学习读书笔记 - 06~07 - 时序差分学习(Temporal-Difference Learning) 学习笔记: Reinforcement Learning: An Introductio ...

  3. [LeetCode] Dp

    Best Time to Buy and Sell Stock 题目: Say you have an array for which the ith element is the price of ...

  4. oracle目录操作

    1.创建目录 create directory dir_name as 'dir_path'  (dir_path必须事先手动创建) 2.授权 grant read,write on director ...

  5. SQLServer存储过程实现单条件分页

    SQLServer Procedure Pagination_basic: ALTER PROCEDURE [qiancheng].[Pagination_basic] ( ), --name of ...

  6. 1131: [POI2008]Sta

    1131: [POI2008]Sta Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 783  Solved: 235[Submit][Status] ...

  7. Android 一个改善的okHttp封装库

    膜拜一下~ 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/49734867: 本文出自:[张鸿洋的博客] 一.概述 之前写了篇A ...

  8. 使用IDEA的gradle整合spring+ mybatis 采用javaconfig配置

    1.新建一个工程 2.工程目录 3.添加gradle.propertes文件 activeMQVersion=5.7.0 aspectJVersion=1.7.2 commonsLangVersion ...

  9. canvas的beginPath和closePath分析总结,包括多段弧的情况

    参考博文: Html5 canvas画图教程17:论beginPath的重要性 先看两个例子 例1: <canvas id="myCanvas" width="30 ...

  10. 通过spring 中的@Scheduled来执行定时任务

    以前开发定时任务的功能的时候,是框架里面写好的quartz配置方式,由于功力尚浅,感觉定时跑披定时任务什么的云里雾里,很高大上,每次都不知道怎么修改配置,后来几次接触定时任务发现,还是比较好掌握的,特 ...