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. 数字化工厂解决方案——OA办公自动化与ERP

    移动办公APP/即时通讯 通过集成手机应用,将移动办公引入到企业信息化管理中,能随时随地的完成审批.查询.警报.知会.公告发布.KPI统计和信息推送.系统已经支持苹果系统.安卓系统和微软WP8系统.企 ...

  2. Pdf File Writer 中文应用(PDF文件编写器C#类库)

    该文由小居工作室(QQ:2482052910)    翻译并提供解答支持,原文地址:Pdf File Writer 中文应用(PDF文件编写器C#类库):http://www.cnblogs.com/ ...

  3. Rotate Array leetcode

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  4. 求int型正整数在内存中存储时1的个数

    题目描述: 输入一个int型的正整数,计算出该int型数据在内存中存储时1的个数. 输入描述: 输入一个整数(int类型) 输出描述: 这个数转换成2进制后,输出1的个数 输入例子: 5 输出例子: ...

  5. 1054: [HAOI2008]移动玩具

    1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1272  Solved: 690[Submit][Statu ...

  6. JAVA高级总结

    一.集合框架和泛型 1.集合框架 1) 定义:JAVA API的一部分,用于处理一组长度可变得数据. 2) 和数组的区别: 数组的长度不可变,但是集合框架处理的数据长度可以动态变化. 3) 结构: 接 ...

  7. Spring Data JPA 实例查询

    一.相关接口方法     在继承JpaRepository接口后,自动拥有了按"实例"进行查询的诸多方法.这些方法主要在两个接口中定义,一是QueryByExampleExecut ...

  8. Win10 UWP开发系列:开发一个自定义控件——带数字徽章的AppBarButton

    最近有个项目有一个这样的需求,在文章浏览页底部有几个AppBarButton,其中有一个是评论按钮,需要在评论按钮上显示一个红色数字,类似微信的新消息提醒: 这种设计在iOS和Android平台都是很 ...

  9. SQL AlawaysOn 之四:故障转移集群

    声明,故障转移集群,仅安装在SQL服务器中,域服务器不能和SQL服务器一起加入集群. 1.添加故障转移集群,下一步 2.安装 3.在域控制服务器上的管理工具里打开故不障转移集群管理器,选择创建集群 4 ...

  10. 解决在eclipse中写ImageView时有警告的问题

    Eclipse中写了一个android程序其中main.xml中ImageView哪行是个黄叹号!不知道为什么? 解决办法: android:contentDescription="@str ...