博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ado.net五大对象
阅读量:6877 次
发布时间:2019-06-26

本文共 3358 字,大约阅读时间需要 11 分钟。

五大对象分别是:

1、 Connection:与数据源建立连接。

2、 Command:对数据源执行SQL命令并返回结果。

3、 DataReader:读取数据源的数据,只允许对将数据源以只读、顺向的方式查看其中所存储的数据。其常用于检索大量数据,DataReader对象还是一种非常节省资源的数据对象。

4、 DataAdapter:对数据源执行操作并返回结果,在DataSet与数据源之间建立通信,将数据源中的数据写入DataSet中,或根据DataSet中的数据绑定数据源。

5、 DataSet:内存中的数据库,是数据表的集合,它可以包含任意多个数据表。

class UserBLL {        public const string constring = "User Id=root;Host=localhost;Database=dbdemo;password=wdf123;charset='utf8'";        //添加数据        public int Add(User entity) {            string sql = "insert into User(ID,UserName)Value(?ID,?UserName)";            using (MySqlConnection conn = new MySqlConnection(constring)) {                conn.Open();                MySqlCommand command = new MySqlCommand(sql,conn);                command.Parameters.AddWithValue("?ID",entity.ID);                command.Parameters.AddWithValue("?UserName", entity.UserName);                return command.ExecuteNonQuery();            }        }        //修改数据        public int Update(User entity) {            string sql = "update User set UserName=?UserName where ID=?ID";            using (MySqlConnection conn = new MySqlConnection(constring)) {                conn.Open();                MySqlCommand command = new MySqlCommand(sql, conn);                command.Parameters.AddWithValue("?ID", entity.ID);                command.Parameters.AddWithValue("?UserName", entity.UserName);                return command.ExecuteNonQuery();            }        }        //删除数据        public int Delete(int ID) {            string sql = "delete from User where ID=?ID";            using (MySqlConnection conn = new MySqlConnection(constring)) {                conn.Open();                MySqlCommand command = new MySqlCommand(sql, conn);                command.Parameters.AddWithValue("?ID", ID);                return command.ExecuteNonQuery();            }        }        //根据主键查询        public User Get(int ID) {            string sql = "select ID,UserName from User where ID=?ID";            using (MySqlConnection conn = new MySqlConnection(constring)) {                conn.Open();                MySqlCommand command = new MySqlCommand(sql, conn);                command.Parameters.AddWithValue("?ID", ID);                MySqlDataReader reader = command.ExecuteReader();                User user = null;                if (reader.Read()) {                    user = new User();                    user.ID = Convert.ToInt32(reader["ID"]);                    user.UserName = reader["UserName"].ToString();                }                return user;            }        }        //查询集合        public IList
GetList() { string sql = "select* from User"; using (MySqlConnection conn = new MySqlConnection(constring)) { conn.Open(); MySqlCommand command = new MySqlCommand(sql, conn); MySqlDataReader reader = command.ExecuteReader(); IList
list = new List
(); while(reader.Read()){ User user = new User(); user.ID = Convert.ToInt32(reader["ID"]); user.UserName = reader["UserName"].ToString(); list.Add(user); } return list; } } }

  

 

转载于:https://www.cnblogs.com/as3lib/p/6238649.html

你可能感兴趣的文章
UIWebView加载html网页时使用缓存和清空缓存
查看>>
我的友情链接
查看>>
设计模式学习笔记(六)之策略模式(Strategy)
查看>>
python运行spark脚本程序
查看>>
我的友情链接
查看>>
通过libvirt使用ceph块设备
查看>>
优秀交互设计师成长指南
查看>>
SDN网络系统之MiniNet的安装与使用
查看>>
java的Iterator和listIterator的区别
查看>>
服务器虚拟化的好处
查看>>
AxureRP7.0基础教程系列 部件详解 表格Tabel
查看>>
ORACLE之sql语句优化
查看>>
一台机器同时启动多个tomcat
查看>>
Java中的多线程
查看>>
Zookeeper不适合注册中心的原因
查看>>
内核是什么
查看>>
标签的语义
查看>>
Freemarker入门例子
查看>>
利用busybox工具制作微型linux系统二
查看>>
商业无小事,现实生活不在童话故事里
查看>>