C#14数据库相关技术
文章目录
- ADO.NET简介
- Connection连接,关闭数据库
- Command操作数据
- 读取数据-DataReader
- DataReader对象常用方法
- 数据集DataSet
- DataSet常用简单方法
- 数据适配:DataAdapter对象
- 本章小结及任务实施
ADO.NET简介
Connection连接,关闭数据库
SQL Server 数据库登录模式设置为:
实现数据库的连接
- windows身份验证实验登录并打开数据库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;namespace _14._9Connection连接关闭数据库
{class Program{static void Main(string[] args){//数据库的连接分为两种方式//SQL Server数据库//windows身份验证实验登录并打开数据库string constr = "Server=DESKTOP-FAVDBP3;integrated security=SSPI;Initial Catalog=csharpzxw";SqlConnection mysqlCon = new SqlConnection(constr);mysqlCon.Open();Console.WriteLine("数据库成功打开");Console.ReadKey();
- SQL验证方式登录
string constr = "Server=DESKTOP-FAVDBP3;user=123;pwd=123;database=csharpzxw";
SqlConnection mysqlCon = new SqlConnection(constr);
mysqlCon.Open();
Console.WriteLine("数据库成功打开");
结果:
数据库的关闭
string constr = "Server=DESKTOP-FAVDBP3;user=123;pwd=123;database=csharpzxw";
SqlConnection mysqlCon = new SqlConnection(constr);
mysqlCon.Open();
Console.WriteLine("数据库成功打开");
mysqlCon.Close();
Console.WriteLine("数据库关闭成功");
Console.ReadKey();
结果:
通过useing语句实现数据库的关闭,当using使用完成后会自动关闭数据库
string constr = "Server=DESKTOP-FAVDBP3;user=123;pwd=123;database=csharpzxw";
SqlConnection mysqlCon = new SqlConnection(constr);
using (mysqlCon)
{mysqlCon.Open();Console.WriteLine("数据库成功打开");
}
Console.ReadKey();
通过try…catch…finally…实现数据库的实时关闭
string constr = "Server=DESKTOP-FAVDBP3;user=123;pwd=123;database=csharpzxw";SqlConnection mysqlCon = new SqlConnection(constr);try{mysqlCon.Open();Console.WriteLine("数据库成功打开");}catch{}finally{mysqlCon.Close();Console.WriteLine("数据库关闭成功");}Console.ReadKey();
using语句与try catch finally 结合使用
//using语句与try catch finally 结合使用string constr = "Server=DESKTOP-FAVDBP3;user=123;pwd=123;database=csharpzxw";SqlConnection mysqlCon = new SqlConnection(constr);try{using (mysqlCon){mysqlCon.Open();Console.WriteLine("数据库成功打开");}}catch{}finally{mysqlCon.Close();Console.WriteLine("数据库关闭成功");}Console.ReadKey();
结果:
Command操作数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;//引入命名空间namespace _14._10Command操作数据
{class Program{static void Main(string[] args){string constr = "Server=DESKTOP-FAVDBP3;user=123;pwd=123;database=csharpzxw";SqlConnection mysqlCon=new SqlConnection(constr);try{//创建Command对象过程mysqlCon.Open();string sql = "select * from mytable001";SqlCommand myCom = new SqlCommand(sql, mysqlCon);//创建Command对象myCom.CommandTimeout = 2;//出错等待时间Console.WriteLine("创建对象成功");}catch(Exception ex){Console.WriteLine(ex.Message.ToString());}finally{mysqlCon.Close();}Console.ReadKey();}}
}
mytable001表中内容
结果:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;//引入命名空间namespace _14._10Command操作数据
{class Program{static void Main(string[] args){string constr = "Server=DESKTOP-FAVDBP3;user=123;pwd=123;database=csharpzxw";SqlConnection mysqlCon=new SqlConnection(constr);try{//更改数据,ExecuteNonQuery()mysqlCon.Open();string sql="insert mytable001(id,name,gender,age,department)values(5,'雪上行者','男',38,'开发部')";SqlCommand myCom = new SqlCommand(sql, mysqlCon);myCom.ExecuteNonQuery();//此方法可实现对数据库的更改Console.WriteLine("数据更改完成"); }catch(Exception ex){Console.WriteLine(ex.Message.ToString());}finally{mysqlCon.Close();}Console.ReadKey();}}
}
结果:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;//引入命名空间namespace _14._10Command操作数据
{class Program{static void Main(string[] args){string constr = "Server=DESKTOP-FAVDBP3;user=123;pwd=123;database=csharpzxw";SqlConnection mysqlCon=new SqlConnection(constr);try{//ExecuteScalar更改数据mysqlCon.Open();string sql = "select max(age) from mytable001";SqlCommand myCom = new SqlCommand(sql, mysqlCon);//要访问必须创建一个Command语句int t = 0;t=(int) myCom.ExecuteScalar();Console.WriteLine("年龄最大的是" + t + "岁");}catch(Exception ex){Console.WriteLine(ex.Message.ToString());}finally{mysqlCon.Close();}Console.ReadKey();}}
}
结果:
读取数据-DataReader
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;namespace _14._11DataReader读取数据
{class Program{static void Main(string[] args){string constr= "Server=DESKTOP-FAVDBP3;user=123;pwd=123;database=csharpzxw";SqlConnection myCon = new SqlConnection(constr);try{myCon.Open();string sql = "select * from mytable001 where gender='男'";SqlCommand myCom = new SqlCommand(sql, myCon);//声明datareaderSqlDataReader mydr;mydr = myCom.ExecuteReader();if (mydr.HasRows) {Console.WriteLine("mytable001中存在数据");}else{Console.WriteLine("mytable001中不存在数据");}mydr.Close();}catch(Exception ex){Console.WriteLine(ex.Message.ToString());}finally{myCon.Close();}Console.ReadKey();}}
}
结果:
看有几个字段 使用FieldCount属性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;namespace _14._11DataReader读取数据
{class Program{static void Main(string[] args){string constr= "Server=DESKTOP-FAVDBP3;user=123;pwd=123;database=csharpzxw";SqlConnection myCon = new SqlConnection(constr);try{myCon.Open();string sql = "select * from mytable001 where gender='男'";SqlCommand myCom = new SqlCommand(sql, myCon);//声明datareaderSqlDataReader mydr;mydr = myCom.ExecuteReader();if (mydr.HasRows) {Console.WriteLine("mytable001中存在数据");}else{Console.WriteLine("mytable001中不存在数据");}Console.WriteLine(mydr.FieldCount);mydr.Close();}catch(Exception ex){Console.WriteLine(ex.Message.ToString());}finally{myCon.Close();}Console.ReadKey();}}
}
结果:
判断是否关闭。使用IsClosed属性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;namespace _14._11DataReader读取数据
{class Program{static void Main(string[] args){string constr= "Server=DESKTOP-FAVDBP3;user=123;pwd=123;database=csharpzxw";SqlConnection myCon = new SqlConnection(constr);try{myCon.Open();string sql = "select * from mytable001 ";SqlCommand myCom = new SqlCommand(sql, myCon);//声明datareaderSqlDataReader mydr;mydr = myCom.ExecuteReader();if (mydr.HasRows) {Console.WriteLine("mytable001中存在数据");}else{Console.WriteLine("mytable001中不存在数据");}Console.WriteLine(mydr.FieldCount);Console.WriteLine(mydr.IsClosed);mydr.Close();Console.WriteLine(mydr.IsClosed);}catch(Exception ex){Console.WriteLine(ex.Message.ToString());}finally{myCon.Close();}Console.ReadKey();}}
}
结果:
DataReader对象常用方法
在这里插入代码片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;namespace _14._11DataReader读取数据
{class Program{static void Main(string[] args){string constr= "Server=DESKTOP-FAVDBP3;user=123;pwd=123;database=csharpzxw";SqlConnection myCon = new SqlConnection(constr);try{myCon.Open();string sql = "select * from mytable001 where gender='男'";SqlCommand myCom = new SqlCommand(sql, myCon);//声明datareaderSqlDataReader mydr;mydr = myCom.ExecuteReader();if (mydr.HasRows) {Console.WriteLine("mytable001中存在数据");}else{Console.WriteLine("mytable001中不存在数据");}Console.WriteLine(mydr.FieldCount);Console.WriteLine(mydr.IsClosed);//flase表示未关闭Console.WriteLine(mydr.GetDataTypeName(0));//方法后面的加(),属性后面不加()Console.WriteLine(mydr.GetName(0));string mystr = "age";Console.WriteLine(mydr.GetOrdinal(mystr));if (mydr.Read()){Console.WriteLine(mydr.GetValue(1));}object[] myobj = new object[mydr.FieldCount];while (mydr.Read()){mydr.GetValues(myobj);foreach (object outobj in myobj){Console.Write(outobj + "\t");}Console.WriteLine();}mydr.Close();//Console.WriteLine(mydr.IsClosed);}catch(Exception ex){Console.WriteLine(ex.Message.ToString());}finally{myCon.Close();}Console.ReadKey();}}
}
结果:
Read方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;namespace _14._11DataReader读取数据
{class Program{static void Main(string[] args){string constr = "Server=DESKTOP-FAVDBP3;user=123;pwd=123;database=csharpzxw";SqlConnection myCon = new SqlConnection(constr);try{myCon.Open();string sql = "select * from mytable001 where gender='男'";SqlCommand myCom = new SqlCommand(sql, myCon);//声明datareaderSqlDataReader mydr;mydr = myCom.ExecuteReader();if (mydr.HasRows){Console.WriteLine("mytable001中存在数据");}else{Console.WriteLine("mytable001中不存在数据");}while (mydr.Read()){Console.Write(mydr[0].ToString() + ",");Console.Write(mydr[1].ToString() + ",");Console.Write(mydr["gender"].ToString() + ",");Console.Write(mydr["department"].ToString());Console.WriteLine();}mydr.Close();//Console.WriteLine(mydr.IsClosed);}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}finally{myCon.Close();}Console.ReadKey();}}
}
结果:
数据集DataSet
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;//引入命名空间namespace _14._13数据集DataSet
{class Program{static void Main(string[] args){//创建Dataset 对象DataSet myds = new DataSet("csharp51zxw");DataTable mydt = new DataTable("mytable001");//创建一张表myds.Tables.Add(mydt);//将表与数据集进行关联DataColumn mydcol1 = new DataColumn("id", Type.GetType("System.Int32"));//创建列DataColumn mydcol2 = new DataColumn("name", Type.GetType("System.String"));DataColumn mydcol3 = new DataColumn("department", Type.GetType("System.String"));mydt.Columns.Add(mydcol1);//将定义好的列加入到DataTable中mydt.Columns.Add(mydcol2);mydt.Columns.Add(mydcol3);//给这三列输入对应的数据string[,] mystr= { { "张三", "开发部" }, { "李四", "销售部" }, { "王五", "技术支持部" }, { "赵六", "人事部" },{ "刘七","项目部"} };for(int i=0;i<mystr.Length/2;i++)//循环体主要是用来赋值的,赋值位于上面,行数据,列字段{DataRow myrow = mydt.NewRow();//为mytb增加一个新行//依次赋值myrow[0] = i+1;myrow["name"] = mystr[i, 0];myrow[2] = mystr[i, 1];mydt.Rows.Add(myrow);//将行加入到mydt}myds.AcceptChanges();//进行更改,类似于保存Console.WriteLine("更改前的数据");outValues(myds);DataTable mydt002 = mydt.Clone();//克隆DataRow mydr = mydt002.NewRow();mydt002.Rows.Add(new object[] { 6, "王小二", "技术部" });mydt002.Rows.Add(new object[] { 7, "张小虎", "开发部" });myds.Merge(mydt002);//将mydt002及其架构合并到myds中 Console.WriteLine("\n合并后的数据集");outValues(myds);DataSet myds001 = myds.Copy();DataSet myds002 = myds.Clone();Console.WriteLine("COPY结果");//对其进行完全复制outValues(myds001);Console.WriteLine("Clone结果");//只是对架构进行复制outValues(myds002);Console.ReadKey();}public static void outValues(DataSet ds)//实现遍历dataset{foreach(DataTable outtable in ds.Tables){Console.WriteLine("表名称:" + outtable.TableName); foreach(DataRow outdr in outtable.Rows){foreach(DataColumn outdcol in outtable.Columns){Console.Write(outdr[outdcol]+"\t");//outdr[outdcol]行数据下的列字段,保证数据成行输出}Console.WriteLine();}}}}
}
结果:
DataSet常用简单方法
数据适配:DataAdapter对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;//引入命名空间与数据库进行连接
using System.Data;namespace _14._15数据适配_DataAdapter对象
{class Program{static void Main(string[] args){string constr="Server=DESKTOP-FAVDBP3;user=123;pwd=123;database=csharpzxw";SqlConnection mycon = new SqlConnection(constr);//声明对象try{mycon.Open();string sql = "select * from mytable001";SqlDataAdapter myda = new SqlDataAdapter(sql, mycon);DataSet myds = new DataSet();myda.Fill(myds, "mytable001");Console.WriteLine("填充成功");OutValues(myds);Console.ReadKey();}catch(Exception ex){Console.WriteLine(ex.Message.ToString());}finally{mycon.Close();}}public static void OutValues(DataSet ds){foreach(DataTable dt in ds.Tables){Console.WriteLine("表名:" + dt.TableName);foreach(DataRow row in dt.Rows){foreach(DataColumn col in dt.Columns){Console.Write(row[col] + "\t");}Console.WriteLine(); }}}}
}
结果:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;//引入命名空间与数据库进行连接
using System.Data;namespace _14._15数据适配_DataAdapter对象
{class Program{static void Main(string[] args){string constr="Server=DESKTOP-FAVDBP3;user=123;pwd=123;database=csharpzxw";SqlConnection mycon = new SqlConnection(constr);//声明对象try{//更改数据mycon.Open(); string updatesql="update mytable001 set gender='女' where name='张三'";SqlDataAdapter myda = new SqlDataAdapter("select * from mytable001",mycon);myda.UpdateCommand = new SqlCommand(updatesql, mycon);DataSet myds = new DataSet();myda.Fill(myds,"mytable001");//acceptchange处于true状态,接受上面的改变已保存Console.WriteLine("原dataset对象内容");OutValues(myds);DataRow row = myds.Tables[0].Rows[0];row["gender"] = "女";myda.Update(myds, "mytable001");Console.WriteLine("更改后的dataset对象内容");OutValues(myds);Console.ReadKey();}catch(Exception ex){Console.WriteLine(ex.Message.ToString());}finally{mycon.Close();}}public static void OutValues(DataSet ds){foreach(DataTable dt in ds.Tables){Console.WriteLine("表名:" + dt.TableName);foreach(DataRow row in dt.Rows){foreach(DataColumn col in dt.Columns){Console.Write(row[col] + "\t");}Console.WriteLine(); }}}}
}
结果:
本章小结及任务实施
在SQLQuery中建立表格
结果:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;namespace _14._16本章小结及任务实施
{class Program{static void Main(string[] args){string constr= "Server=DESKTOP-FAVDBP3;user=123;pwd=123;database=company";SqlConnection mycon = new SqlConnection(constr);try{mycon.Open();string sql = "select *from clerk";//声明一个sql语句为查询语句,在sql中不区分大小写SqlCommand mycom = new SqlCommand(sql, mycon);//声明一个SqlCommand对象来实现查询SqlDataReader mydr = mycom.ExecuteReader();//将sql语句读取得信息返回给sqldatareader,但一次只能返回一条Console.WriteLine("使用DataReader打印输出");for(int i=0;i<mydr.FieldCount;i++){Console.Write(mydr.GetName(i) + "\t");}Console.WriteLine();while(mydr.Read()){for(int i=0;i<mydr.FieldCount;i++){Console.Write(mydr[i].ToString() + "\t");}Console.WriteLine();}mydr.Close();Console.WriteLine("使用DataSet打印输出");SqlDataAdapter myda = new SqlDataAdapter(sql, mycon);DataSet myds = new DataSet();myda.Fill(myds, "Clerk");foreach(DataTable table in myds.Tables)//将列名进行输出,也就是字段{foreach(DataColumn col in table.Columns)//声明表中对应的列{Console.Write(col.ColumnName + "\t");//输出列}Console.WriteLine();foreach (DataRow row in table.Rows)//输出数据{foreach(DataColumn col in table.Columns){Console.Write(row[col] + "\t");}Console.WriteLine();}}}catch(Exception ex){Console.WriteLine(ex.Message.ToString());}finally{mycon.Close();}Console.ReadKey();}}
}
结果:
C#14数据库相关技术相关推荐
- 国开计算机应用技术形考任务4答案,国家开放大学《数据库应用技术》形考任务1-4参考答案...
国家开放大学<数据库应用技术>形考任务1-4参考答案 国家开放大学<数据库应用技术>形考任务1-4参考答案 形考任务1 第1章测验 一.单项选择题(每题2分,共20分) 1.关 ...
- NGS的测序仪和相关技术时间轴 NGS相关数据库和项目时间轴
NGS的测序仪和相关技术时间轴 NGS相关数据库和项目时间轴:
- 【数据库原理及应用】经典题库附答案(14章全)——第七章:数据库恢复技术
[数据库原理及应用]经典题库附答案(14章全)--第一章:数据库基础知识 [数据库原理及应用]经典题库附答案(14章全)--第二章:关系数据库知识 [数据库原理及应用]经典题库附答案(14章全)--第 ...
- 与计算机图形学相关的研究论文,计算机图形学相关论文范文数据库,与计算机图形学的相关技术与相关专科毕业论文范文...
计算机图形学相关论文范文数据库,与计算机图形学的相关技术与相关专科毕业论文范文 关于计算机图形学及计算机及计算机动画方面的免费优秀学术论文范文,计算机图形学相关本科毕业论文,关于计算机图形学的相关技术 ...
- [转]国内各大互联网公司相关技术站点2.0版 (集合腾讯、阿里、百度、搜狐、新浪、360等共49个)...
利用闲暇时间整理了一份国内各大互联网公司的相关技术站点,希望能够对大家有所帮助,也欢迎各位帮忙补充. 腾讯系列(13) 阿里系列(18) 百度系列(3) 搜狐系列(3) 新浪系列(2) 36 ...
- 第5章_数据库相关(一)
第5章_数据库相关 1.CAP定理 2.事务的`ACID`特性 3.并发异常 4.隔离级别 5.`Oracle`.`MySQL`.`SQL Server`的事务隔离级别 6.范式(`Normal Fo ...
- Fabric学习(一) — 区块链及相关技术介绍
目录 一.区块链? 1.1 区块链是什么? 1.2 区块链的分类 1.2.1 公共区块链 1.2.2 联盟区块链 1.2.3 完全私有链 1.3 区块链工作原理 二.区块链相关技术 2.1 基础技术 ...
- JAVA面试汇总第四章 Spring及数据库相关
Spring 核心功能演示 + 面试题 Spring Framework 简称 Spring,是 Java 开发中最常用的框架,地位仅次于 Java API,就连近几年比较流行的微服务框架 Sprin ...
- Redis数据库相关知识总结
Redis数据库相关知识总结 1.NoSQL概述 1.1 为什么用NoSQL 1.单机MySQL的美好年代 在90年代,一个网站的访问量一般不大,用单个数据库完全可以轻松应付! 在那个时候,更多的都是 ...
最新文章
- memcpy,_tcscpy_s的使用
- centos rpm安装mysql5.5_CentOS下以RPM方式安装MySQL5.5
- 【Java基础】List迭代并修改时出现的ConcurrentModificationException问题
- 分段线性变换与直方图修正
- js与jquery操作
- java 对象equals_浅谈Java对象的equals方法
- 获取cookies的简单代码(总结待续)
- C++调用tensorflow训练好的SSD物体检测模型-opencv3.4.3
- Altium Designer安装包下载
- 时间序列分析的计量经济学方法 - Python中的序列性ARIMA
- 【信息安全】-身份认证技术
- 记一次golang memory leak的解决过程
- 数据化建设知识图谱(文末附PDF下载)
- 孙溟㠭绘画篆刻——《梦》
- STM32CubeMX实战教程(七)——TFT_LCD液晶显示(附驱动代码)
- 【电力电子】【2011.08】通过谐波电流注入改善三相整流器输入功率因数
- setAttribute 和 getAttribute区别
- 小米品牌广告引擎与算法实践
- EAST: An Efficient and Accurate Scene Text Detector 论文阅读
- 函数的参数、返回、调用、递归
热门文章
- HEIST攻击:从HTTPS加密数据中获取明文
- Qt TcpSocket 传递数据乱码显示
- Android 仿照QQ剪裁头像(完结篇)
- 1.ODBC连接Postgresql
- centos7安装gparted分区工具
- shell中spawn什么意思_linux expect spawn的用法
- 【教程】潘多拉Pandora-SS+多拨+去广告同时运行配置教(斐讯K1)
- 腾讯一夜注册数万个CN域名
- 极客日报:腾讯企业QQ将停运;雷军:小米汽车预计2024年量产;iPhone或将取消SIM卡槽,全部改用eSIM卡
- 谷粒商城项目笔记之分布式基础(一)