首页 > 代码库 > ADO.NET与ROM的比较(1):ADO.NET实现CRUD
ADO.NET与ROM的比较(1):ADO.NET实现CRUD
ADO.NET与ROM的比较(1):ADO.NET实现CRUD 转自周公
说明:个人感觉在Java领域大型开发都离不了ORM的身影,所谓的SSH就是Spring+Struts+Hibernate,除了在学习基础知识的时候被告知可以使用JDBC操作数据库之外,大量的书籍中都是讲述使用Hibernate这个ORM工具来操作数据。在.NET中操作数据库的方式有多种,除了最直接的方式就是使用ADO.NET之外,还可以使用NHibernate这个Hibernate在.NET中的实现ORM,如果你对第三方的ORM持怀疑态度,你还可以使用来自微软的实现、根正苗红的Linq或者EntityFramework。
大部分从早期就开始使用.NET开发的程序员可能对ADO.NET有种迷恋,使用ADO.NET可以充分将我们早期的SQL知识发挥得淋漓尽致,并且出于对性能的考虑,有些人对.NET中的ORM还保持一种观望态度,包括我自己也是这种态度。不过即使在实际开发中不用,并不代表我们不能去了解和比较这些技术,任何事物的出现和消亡总有其原因的,我们可以了解它们的优点和长处。所以本人抽出了几个周末的时间分别用ADO.NET、NHibernate、Linq和EntityFramework来实现对数据库单表数据的创建、读取、更新和删除操作,也就是所谓的CRUD(C:Create/R:Read/U:Update/D:Delete)。
通过实现相同功能的比较,大家自己判断那种方式更适合自己。需要说明的是,如果在VS2008中使用EntityFramework就需要安装VS2008SP1。
在开始演示之前先准备好数据,在本系列中所使用的数据库是SQL Server2005,创建和初始化数据库数据的代码如下:
/****** 对象: Table [dbo].[UserInfo] 脚本日期: 03/08/2010 12:20:11 ******/
SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOIF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N‘[dbo].[UserInfo]‘) AND type in (N‘U‘))BEGINCREATE TABLE [dbo].[UserInfo]( [UserID] [int] IDENTITY(1,1) NOT NULL, [UserName] [varchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL, [RealName] [nvarchar](8) COLLATE Chinese_PRC_CI_AS NOT NULL, [Age] [tinyint] NOT NULL, [Sex] [bit] NOT NULL, [Mobile] [char](11) COLLATE Chinese_PRC_CI_AS NULL, [Phone] [char](11) COLLATE Chinese_PRC_CI_AS NULL, [Email] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL, CONSTRAINT [PK_UserInfo] PRIMARY KEY CLUSTERED ( [UserID] ASC)WITH (IGNORE_DUP_KEY = OFF))ENDGOIF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N‘[dbo].[UserInfo]‘) AND name = N‘IX_UserName‘)CREATE UNIQUE NONCLUSTERED INDEX [IX_UserName] ON [dbo].[UserInfo] ( [UserName] ASC)WITH (IGNORE_DUP_KEY = ON)GOSET IDENTITY_INSERT [dbo].[UserInfo] ONINSERT [dbo].[UserInfo] ([UserID], [UserName], [RealName], [Age], [Sex], [Mobile], [Phone], [Email]) VALUES (1, N‘zhangfei‘, N‘测试859‘, 36, 1, N‘13455663420‘, N‘03517890360‘, N‘zhangfei@msn.com‘)INSERT [dbo].[UserInfo] ([UserID], [UserName], [RealName], [Age], [Sex], [Mobile], [Phone], [Email]) VALUES (2, N‘asddf‘, N‘测试80‘, 38, 1, N‘13455663421‘, N‘03517890361‘, N‘guanyu@163.com‘)INSERT [dbo].[UserInfo] ([UserID], [UserName], [RealName], [Age], [Sex], [Mobile], [Phone], [Email]) VALUES (3, N‘liubei‘, N‘刘备‘, 42, 1, N‘13455663422‘, N‘03517890362‘, N‘liubei@163.net‘)INSERT [dbo].[UserInfo] ([UserID], [UserName], [RealName], [Age], [Sex], [Mobile], [Phone], [Email]) VALUES (4, N‘zhougong‘, N‘周公‘, 29, 1, N‘13455663423‘, N‘03517890363‘, N‘zhoufoxcn@tom.com‘)INSERT [dbo].[UserInfo] ([UserID], [UserName], [RealName], [Age], [Sex], [Mobile], [Phone], [Email]) VALUES (5, N‘zhaoyun‘, N‘赵云‘, 32, 1, N‘13455663424‘, N‘03517890364‘, N‘zhaoyun@sohu.com‘)INSERT [dbo].[UserInfo] ([UserID], [UserName], [RealName], [Age], [Sex], [Mobile], [Phone], [Email]) VALUES (6, N‘huanggai‘, N‘黄盖‘, 50, 1, N‘13455663425‘, N‘03517890365‘, N‘huanggai@live.com‘)INSERT [dbo].[UserInfo] ([UserID], [UserName], [RealName], [Age], [Sex], [Mobile], [Phone], [Email]) VALUES (7, N‘zhugeliang‘, N‘诸葛亮‘, 27, 1, N‘13455663426‘, N‘03517890366‘, N‘zhugeliang@hotmail.com‘)INSERT [dbo].[UserInfo] ([UserID], [UserName], [RealName], [Age], [Sex], [Mobile], [Phone], [Email]) VALUES (8, N‘jiangwei‘, N‘姜维‘, 22, 1, N‘13455663427‘, N‘03517890367‘, N‘jiangwei@netease.com‘)INSERT [dbo].[UserInfo] ([UserID], [UserName], [RealName], [Age], [Sex], [Mobile], [Phone], [Email]) VALUES (9, N‘caocao‘, N‘曹操‘, 48, 1, N‘13455663428‘, N‘03517890368‘, N‘caocao@qq.com‘)INSERT [dbo].[UserInfo] ([UserID], [UserName], [RealName], [Age], [Sex], [Mobile], [Phone], [Email]) VALUES (10, N‘guojia‘, N‘郭嘉‘, 32, 1, N‘13455663429‘, N‘03517890369‘, N‘guojia@21cn.com‘)INSERT [dbo].[UserInfo] ([UserID], [UserName], [RealName], [Age], [Sex], [Mobile], [Phone], [Email]) VALUES (11, N‘sunquan‘, N‘孙权‘, 33, 1, N‘13455663430‘, N‘03517890370‘, N‘sunquan@gmail.com‘)INSERT [dbo].[UserInfo] ([UserID], [UserName], [RealName], [Age], [Sex], [Mobile], [Phone], [Email]) VALUES (12, N‘diaochan‘, N‘貂禅‘, 20, 0, N‘13455663431‘, N‘03517890371‘, N‘diaochan@sina.com.cn‘)INSERT [dbo].[UserInfo] ([UserID], [UserName], [RealName], [Age], [Sex], [Mobile], [Phone], [Email]) VALUES (13, N‘yangyuhuan‘, N‘杨玉环‘, 24, 0, N‘13455663432‘, N‘03517890372‘, N‘yangyuhuang@chinaren.com‘)SET IDENTITY_INSERT [dbo].[UserInfo] OFF
下面开始演示如何使用ADO.NET实现CRUD功能。
一、配置
创建一个控制台或者类库项目,并且添加一个app.config配置文件,在此文件中添加数据库配置信息,如下:
<connectionStrings> <add name="Conn" connectionString="Data Source=zhou;Initial Catalog=AspNetStudy;User ID=sa;Password=jerry" /> </connectionStrings>
二、创建实体类
using System; using System.Collections.Generic; using System.Text; namespace ADODoNETDemo { public class UserInfo { /// <summary> /// 用户编号 /// </summary> public int UserId { get; set; } /// <summary> /// 用户名 /// </summary> public string UserName { get; set; } /// <summary> /// 真实姓名 /// </summary> public string RealName { get; set; } /// <summary> /// 年龄 /// </summary> public byte Age { get; set; } /// <summary> /// 性别 /// </summary> public bool Sex { get; set; } /// <summary> /// 电子邮件 /// </summary> public string Email { get; set; } /// <summary> /// 手机号 /// </summary> public string Mobile { get; set; } /// <summary> /// 电话 /// </summary> public string Phone { get; set; } } }
三、创建数据库访问通用类
说明:下面的这个数据库通用类适用于访问数据库中任意表,不管是基于文本方式SQL、参数化SQL语句或者存储过程都可以。
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace ADODoNETDemo { /// <summary> /// 针对SQL Server数据库操作的通用类 /// 作者:周公 /// 日期:2009-01-08 /// Version:1.0 /// </summary> public class SqlDbHelper { private string connectionString; /// <summary> /// 设置数据库连接字符串 /// </summary> public string ConnectionString { set { connectionString = value; } } /// <summary> /// 构造函数 /// </summary> public SqlDbHelper() : this(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString) { } /// <summary> /// 构造函数 /// </summary> /// <param name="connectionString">数据库连接字符串</param> public SqlDbHelper(string connectionString) { this.connectionString = connectionString; } /// <summary> /// 执行一个查询,并返回结果集 /// </summary> /// <param name="sql">要执行的查询SQL文本命令</param> /// <returns>返回查询结果集</returns> public DataTable ExecuteDataTable(string sql) { return ExecuteDataTable(sql, CommandType.Text, null); } /// <summary> /// 执行一个查询,并返回查询结果 /// </summary> /// <param name="sql">要执行的SQL语句</param> /// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> /// <returns>返回查询结果集</returns> public DataTable ExecuteDataTable(string sql, CommandType commandType) { return ExecuteDataTable(sql, commandType, null); } /// <summary> /// 执行一个查询,并返回查询结果 /// </summary> /// <param name="sql">要执行的SQL语句</param> /// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> /// <param name="parameters">Transact-SQL 语句或存储过程的参数数组</param> /// <returns></returns> public DataTable ExecuteDataTable(string sql, CommandType commandType, SqlParameter[] parameters) { DataTable data = new DataTable();//实例化DataTable,用于装载查询结果集 using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandType = commandType;//设置command的CommandType为指定的CommandType //如果同时传入了参数,则添加这些参数 if (parameters != null) { foreach (SqlParameter parameter in parameters) { command.Parameters.Add(parameter); } } //通过包含查询SQL的SqlCommand实例来实例化SqlDataAdapter SqlDataAdapter adapter = new SqlDataAdapter(command); adapter.Fill(data);//填充DataTable } } return data; } /// <summary> /// /// </summary> /// <param name="sql">要执行的查询SQL文本命令</param> /// <returns></returns> public SqlDataReader ExecuteReader(string sql) { return ExecuteReader(sql, CommandType.Text, null); } /// <summary> /// /// </summary> /// <param name="sql">要执行的SQL语句</param> /// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> /// <returns></returns> public SqlDataReader ExecuteReader(string sql, CommandType commandType) { return ExecuteReader(sql, commandType, null); } /// <summary> /// /// </summary> /// <param name="sql">要执行的SQL语句</param> /// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> /// <param name="parameters">Transact-SQL 语句或存储过程的参数数组</param> /// <returns></returns> public SqlDataReader ExecuteReader(string sql, CommandType commandType, SqlParameter[] parameters) { SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = new SqlCommand(sql, connection); //如果同时传入了参数,则添加这些参数 if (parameters != null) { foreach (SqlParameter parameter in parameters) { command.Parameters.Add(parameter); } } connection.Open(); //CommandBehavior.CloseConnection参数指示关闭Reader对象时关闭与其关联的Connection对象 return command.ExecuteReader(CommandBehavior.CloseConnection); } /// <summary> /// /// </summary> /// <param name="sql">要执行的查询SQL文本命令</param> /// <returns></returns> public Object ExecuteScalar(string sql) { return ExecuteScalar(sql, CommandType.Text, null); } /// <summary> /// /// </summary> /// <param name="sql">要执行的SQL语句</param> /// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> /// <returns></returns> public Object ExecuteScalar(string sql, CommandType commandType) { return ExecuteScalar(sql, commandType, null); } /// <summary> /// /// </summary> /// <param name="sql">要执行的SQL语句</param> /// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> /// <param name="parameters">Transact-SQL 语句或存储过程的参数数组</param> /// <returns></returns> public Object ExecuteScalar(string sql, CommandType commandType, SqlParameter[] parameters) { object result = null; using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandType = commandType;//设置command的CommandType为指定的CommandType //如果同时传入了参数,则添加这些参数 if (parameters != null) { foreach (SqlParameter parameter in parameters) { command.Parameters.Add(parameter); } } connection.Open();//打开数据库连接 result = command.ExecuteScalar(); } } return result;//返回查询结果的第一行第一列,忽略其它行和列 } /// <summary> /// 对数据库执行增删改操作 /// </summary> /// <param name="sql">要执行的查询SQL文本命令</param> /// <returns></returns> public int ExecuteNonQuery(string sql) { return ExecuteNonQuery(sql, CommandType.Text, null); } /// <summary> /// 对数据库执行增删改操作 /// </summary> /// <param name="sql">要执行的SQL语句</param> /// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> /// <returns></returns> public int ExecuteNonQuery(string sql, CommandType commandType) { return ExecuteNonQuery(sql, commandType, null); } /// <summary> /// 对数据库执行增删改操作 /// </summary> /// <param name="sql">要执行的SQL语句</param> /// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> /// <param name="parameters">Transact-SQL 语句或存储过程的参数数组</param> /// <returns></returns> public int ExecuteNonQuery(string sql, CommandType commandType, SqlParameter[] parameters) { int count = 0; using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandType = commandType;//设置command的CommandType为指定的CommandType //如果同时传入了参数,则添加这些参数 if (parameters != null) { foreach (SqlParameter parameter in parameters) { command.Parameters.Add(parameter); } } connection.Open();//打开数据库连接 count = command.ExecuteNonQuery(); } } return count;//返回执行增删改操作之后,数据库中受影响的行数 } /// <summary> /// 返回当前连接的数据库中所有由用户创建的数据库 /// </summary> /// <returns></returns> public DataTable GetTables() { DataTable data = null; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open();//打开数据库连接 data = http://www.mamicode.com/connection.GetSchema("Tables"); } return data; } } }
四、创建CRUD类
对数据库实现增删改查功能的类的代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data; namespace ADODoNETDemo { /// <summary> /// 用ADO.NET实现CRUD功能 /// </summary> public class ADODotNetCRUD { /// <summary> /// 统计用户总数 /// </summary> /// <returns></returns> public int Count() { string sql = "select count(1) from UserInfo"; SqlDbHelper db = new SqlDbHelper(); return int.Parse(db.ExecuteScalar(sql).ToString()); } /// <summary> /// 创建用户 /// </summary> /// <param name="info">用户实体</param> /// <returns></returns> public bool Create(UserInfo info) { string sql = "insert UserInfo(UserName,RealName,Age,Sex,Mobile,Email,Phone)values(@UserName,@RealName,@Age,@Sex,@Mobile,@Email,@Phone)"; SqlParameter[] paramters = new SqlParameter[]{ new SqlParameter("@UserName",info.UserName), new SqlParameter("@RealName",info.RealName), new SqlParameter("@Age",info.Age), new SqlParameter("@Sex",info.Sex), new SqlParameter("@Mobile",info.Mobile), new SqlParameter("@Email",info.Email), new SqlParameter("@Phone",info.Phone), }; SqlDbHelper db = new SqlDbHelper(); return db.ExecuteNonQuery(sql, CommandType.Text, paramters) > 0; } /// <summary> /// 读取用户信息 /// </summary> /// <param name="userId">用户编号</param> /// <returns></returns> public UserInfo Read(int userId) { string sql = "select * from UserInfo Where UserId="+userId; SqlDbHelper db = new SqlDbHelper(); DataTable data = db.ExecuteDataTable(sql); if (data.Rows.Count > 0) { DataRow row = data.Rows[0]; UserInfo info = new UserInfo() { UserId=int.Parse(row["UserId"].ToString()), UserName=row["UserName"].ToString(), Age=byte.Parse(row["Age"].ToString()), Email=row["Email"].ToString(), Mobile=row["Mobile"].ToString(), Phone=row["Phone"].ToString(), RealName=row["RealName"].ToString(), Sex=bool.Parse(row["Sex"].ToString()) }; return info; } else { return null; } } /// <summary> /// 更新用户信息 /// </summary> /// <param name="info">用户实体</param> /// <returns></returns> public bool Update(UserInfo info) { string sql = "update UserInfo set UserName=@UserName,RealName=@RealName,Age=@Age,Sex=@Sex,Mobile=@Mobile,Email=@Email,Phone=@Phone where UserID=@UserID"; SqlParameter[] paramters = new SqlParameter[]{ new SqlParameter("@UserName",info.UserName), new SqlParameter("@RealName",info.RealName), new SqlParameter("@Age",info.Age), new SqlParameter("@Sex",info.Sex), new SqlParameter("@Mobile",info.Mobile), new SqlParameter("@Email",info.Email), new SqlParameter("@Phone",info.Phone), new SqlParameter("@UserID",info.UserId), }; SqlDbHelper db = new SqlDbHelper(); return db.ExecuteNonQuery(sql, CommandType.Text, paramters) > 0; } /// <summary> /// 删除用户 /// </summary> /// <param name="userId">用户编号</param> /// <returns></returns> public bool Delete(int userId) { string sql = "delete from UserInfo where UserId=" + userId; SqlDbHelper db = new SqlDbHelper(); return db.ExecuteNonQuery(sql) > 0; } /// <summary> /// 获取用户表中编号最大的用户 /// </summary> /// <returns></returns> public int GetMaxUserId() { string sql = "select max(userId) from UserInfo"; SqlDbHelper db = new SqlDbHelper(); return int.Parse(db.ExecuteScalar(sql).ToString()); } } }
五、NUnit单元测试代码
在进行单元测试时没有使用VS自带的单元测试工具,仍是我平常用惯了的NUnit,下面是代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ADODoNETDemo; using NUnit.Framework; namespace NUnitTest { [TestFixture] class ADODotNetTest { private ADODotNetCRUD instance = null; [SetUp] public void Initialize() { instance = new ADODotNetCRUD(); } [Test] /// <summary> /// 统计用户总数 /// </summary> /// <returns></returns> public void Count() { Assert.Greater(instance.Count(), 0); } [Test] /// <summary> /// 创建用户 /// </summary> /// <param name="info">用户实体</param> /// <returns></returns> public void Create() { UserInfo info = new UserInfo() { Age = 12, Email = "zzz@ccav.com", Mobile = "13812345678", Phone = "01012345678", RealName = "测试" + DateTime.Now.Millisecond.ToString(), Sex = true, UserName = "zhoufoxcn" + DateTime.Now.Millisecond.ToString() }; instance.Create(info); } [Test] /// <summary> /// 读取用户信息 /// </summary> /// <param name="userId">用户编号</param> /// <returns></returns> public void Read() { UserInfo info = instance.Read(1); Assert.NotNull(info); } [Test] /// <summary> /// 更新用户信息 /// </summary> /// <param name="info">用户实体</param> /// <returns></returns> public void Update() { UserInfo info = instance.Read(1); info.RealName = "测试" + DateTime.Now.Millisecond.ToString(); instance.Update(info); } [Test] /// <summary> /// 删除用户 /// </summary> /// <param name="userId">用户编号</param> /// <returns></returns> public void DeleteByID() { int userId = instance.GetMaxUserId(); instance.Delete(userId); } } }
六、点评
在使用ADO.NET操作数据库时个人觉得有如下优点,可以随心所欲地使用熟知的SQL语句甚至是存储过程,在使用统计、分组功能时感觉尤其强烈,当然前提是你的SQL基础要相当好,此外它是其它几种ORM的基础,其它几种ORM都是在底层使用ADO.NET实现与数据库的交互的,所以在效率上来说它也要高一些。它的缺点是需要自己写操作数据的语句并且需要自己编写数据库记录到实体的映射转换代码,一旦表的结构发生变动,就需要修改实体类和映射转换代码。
ADO.NET与ROM的比较(1):ADO.NET实现CRUD