2017-4-18 ADO.NET
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的更多相关文章
- 九月 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 ...
- 日本IT行业劳动力缺口达22万 在日中国留学生迎来就业好时机 2017/07/18 11:25:09
作者:倪亚敏 来源:日本新华侨报 发布时间:2017/07/18 11:25:09 据日本政府提供的数据,日本2018年应届毕业生的“求人倍率”已经达到了1.78倍.换言之,就是100名大学生 ...
- 2017.7.18 linux下ELK环境搭建
参考来自:Linux日志分析ELK环境搭建 另一篇博文:2017.7.18 windows下ELK环境搭建 0 版本说明 因为ELK从5.0开始只支持jdk 1.8,但是项目中使用的是JDK 1 ...
- 2017.7.18 windows下ELK环境搭建
参考来自:Windows环境下ELK平台的搭建 另一篇博文:2017.7.18 linux下ELK环境搭建 0 版本说明 因为ELK从5.0开始只支持jdk 1.8,但是项目中使用的是JDK 1.7, ...
- 湖南师范大学计算机基础课网络教学平台 版本 V2.0(2017.9.18)
湖南师范大学计算机基础课网络教学平台 版本 V2.0(2017.9.18) 开发环境: 开发工具:VS2013,数据库:Sqlserver2012 开发语言:Asp.net MVC5 ,界面UI:jq ...
- 2017.11.18 手把手教你学51单片机-点亮LED
In Doing We Learning 在操作中学习.如果只是光看教程,没有实际的操作,对编程语言的理解很空泛,所以决定从单片机中学习C语言. #include<reg52.h> ...
- noip2010 真题练习 2017.2.18
第一题比较简单,用exist数组判断是否在循环队列中,就可实现线性算法. Code #include<iostream> #include<cstdio> #include&l ...
- 团队作业4——第一次项目冲刺(Alpha版本)2017.11.18
1.当天站立式会议照片 本次会议在5号公寓312召开,本次会议内容:①:熟悉每个人想做的模块.②:根据老师的要求将项目划分成一系列小任务.③:在上次会议内容完成的基础上增加新的任务. 2.每个人的工作 ...
- 2017.11.18 IAP下载(STM8,PIC,STM32)
客户要求用IAP下载,mark一下,客户还给了stm32的引导码.仅供参考. 1 PIC单片机的IAP 2 STm32 IAP https://www.cnblogs.com/WeyneChen/p ...
- May 2 2017 Week 18 Tuesday
The beauty of the journey is found in the scenery along the way. 旅行之美在于沿途所见的风景. Several years ago, I ...
随机推荐
- 电器ERP行业案例——环力科技
环力科技ERP案例 企业简介 [规模] 环力公司始建于1992年,是一家专业生产电子压力控制器.水泵压力控制器.气泵压力控制器.电泵浮球控制器.全自动水泵及其它配套产品研究.开发.生产.销售为一体的专 ...
- ACM 重建二叉树
重建二叉树 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 题目很简单,给你一棵二叉树的后序和中序序列,求出它的前序序列(So easy!). 输入 输入有多组数 ...
- 3406: [Usaco2009 Oct]Invasion of the Milkweed 乳草的入侵
3406: [Usaco2009 Oct]Invasion of the Milkweed 乳草的入侵 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 8 ...
- 1088: [SCOI2005]扫雷Mine
1088: [SCOI2005]扫雷Mine Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1635 Solved: 979[Submit][Sta ...
- netflix zuul-simple-webapp.war在tomcat下启动
按照netflix 在github 的wiki的文档使用 gradlew jettyRun 可以启动jetty来进行测试. 在本地build war 以后,我放在tomcat 运行的时候,却不可以运行 ...
- wp8数据存储--独立存储文件 【转】
出自 : http://www.cnblogs.com/MyBeN/p/3339019.html 文章篇幅有点大,建议去源网看看 1.调用手机的独立存储 例如:IsolatedStorageFile ...
- HttpURLConnection实现两个服务端的对接
在企业开发中,很多时候需要用到两个服务端的对接,在java类中进行连接并传递参数,其中的HttpURLConnection是一种轻量化,并且简单的方法! package httptest; impor ...
- 手把手教你做个AR涂涂乐
前段时间公司有一个AR涂涂乐的项目,虽然之前接触过AR也写过小Demo,但是没有完整开发过AR项目.不过经过1个多星期的学习,现在已经把项目相关的技术都学会了,在此向互联网上那些乐于分享的程序员前辈们 ...
- 最短路径之BF算法+线性规划(图片格式)
- 利用python的爬虫技术爬取百度贴吧的帖子
在爬取糗事百科的段子后,我又在知乎上找了一个爬取百度贴吧帖子的实例,为了巩固提升已掌握的爬虫知识,于是我打算自己也做一个. 实现目标:1,爬取楼主所发的帖子 2,显示所爬去的楼层以及帖子题目 3,将爬 ...