ADO.NET操作SQL Server:数据库操作类(未封装)
1.添加数据
/// <summary>
/// 添加数据
/// </summary>
/// <param name="newEntity"></param>
/// <returns></returns>
public static int Insert(Student newEntity)
{
string sql = @"insert into [Student] ([StudentId],[StudentNo],[IdCard],[RealName]) values (@StudentId,@StudentNo,@IdCard,@RealName)";
using (SqlConnection conn = new SqlConnection(datalink.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
#region MyRegion
cmd.Parameters.Add("@StudentId", SqlDbType.UniqueIdentifier).Value = newEntity.StudentId;
cmd.Parameters.Add("@StudentNo", SqlDbType.Int).Value = newEntity.StudentNo;
cmd.Parameters.Add("@IdCard", SqlDbType.NVarChar).Value = newEntity.IdCard ?? (object)DBNull.Value;
cmd.Parameters.Add("@RealName", SqlDbType.NVarChar).Value = newEntity.RealName ?? (object)DBNull.Value;
#endregion conn.Open();
return cmd.ExecuteNonQuery();
}
}
}
2.修改数据
/// <summary>
/// 修改数据
/// </summary>
/// <param name="updateEntity"></param>
/// <returns></returns>
public static int Update(Student updateEntity)
{
string sql = @"update [Student] set [StudentNo]=@StudentNo,[IdCard]=@IdCard,[RealName]=@RealName where [StudentId]=@StudentId";
using (SqlConnection conn = new SqlConnection(datalink.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
#region MyRegion
cmd.Parameters.Add("@StudentId", SqlDbType.UniqueIdentifier).Value = updateEntity.StudentId;
cmd.Parameters.Add("@StudentNo", SqlDbType.Int).Value = updateEntity.StudentNo;
cmd.Parameters.Add("@IdCard", SqlDbType.NVarChar).Value = updateEntity.IdCard ?? (object)DBNull.Value;
cmd.Parameters.Add("@RealName", SqlDbType.NVarChar).Value = updateEntity.RealName ?? (object)DBNull.Value;
#endregion conn.Open();
return cmd.ExecuteNonQuery();
}
}
}
3.删除数据
/// <summary>
/// 删除数据
/// </summary>
/// <param name="studentId"></param>
/// <returns></returns>
public static int Delete(Guid studentId)
{
string sql = @"delete from [Student] where [StudentId]=@StudentId";
using (SqlConnection conn = new SqlConnection(datalink.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.Add("@StudentId", SqlDbType.UniqueIdentifier).Value = studentId;
conn.Open();
return cmd.ExecuteNonQuery();
}
}
}
4.读取数据
/// <summary>
/// 读取数据
/// </summary>
/// <param name="reader"></param>
/// <returns></returns>
private static Student Reader(SqlDataReader reader)
{
Student newEntity = new Student();
if (reader != null && !reader.IsClosed)
{
if (reader["StudentId"] != DBNull.Value) newEntity.StudentId = (Guid)reader["StudentId"];
if (reader["StudentNo"] != DBNull.Value) newEntity.StudentNo = (int)reader["StudentNo"];
if (reader["IdCard"] != DBNull.Value) newEntity.IdCard = (string)reader["IdCard"];
if (reader["RealName"] != DBNull.Value) newEntity.RealName = (string)reader["RealName"];
}
return newEntity;
}
5.查询1行数据
/// <summary>
/// 查询1行数据
/// </summary>
/// <param name="studentId"></param>
/// <returns></returns>
public static Student GetEntity(Guid studentId)
{
Student entity = null;
string sql = @"select * from [Student] where [StudentId]=@StudentId";
using (SqlConnection conn = new SqlConnection(datalink.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.Add("@StudentId", SqlDbType.UniqueIdentifier).Value = studentId;
conn.Open();
using (SqlDataReader sr = cmd.ExecuteReader())
{
while (sr.Read())
{
entity = Reader(sr);
}
}
}
}
return entity;
}
6.查询n行数据
/// <summary>
/// 查询n行数据
/// </summary>
/// <returns></returns>
public static List<Student> SearchAllList()
{
List<Student> list = new List<Student>();
string sql = @"select * from [Student]";
using (SqlConnection conn = new SqlConnection(datalink.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
conn.Open();
using (SqlDataReader sr = cmd.ExecuteReader())
{
while (sr.Read())
{
Student info = Reader(sr);
list.Add(info);
}
}
}
}
return list;
}
7.添加数据列表
/// <summary>
/// 添加数据列表
/// </summary>
/// <param name="newEntityList"></param>
/// <param name="message"></param>
/// <returns></returns>
public static bool Insert(List<Student> newEntityList, out string message)
{
message = string.Empty;
int rows = ;
string sql = @"insert into [Student] ([StudentId],[StudentNo],[IdCard],[RealName]) values (@StudentId,@StudentNo,@IdCard,@RealName)";
using (SqlConnection conn = new SqlConnection(datalink.ConnectionString))
{
conn.Open(); SqlCommand cmd = conn.CreateCommand();
SqlTransaction tran = conn.BeginTransaction();
cmd.CommandText = sql;
cmd.Connection = conn;
cmd.Transaction = tran; try
{
//打开数据库连接后,循环执行insert语句。在循环过程中,如果没有出现异常,则提交事务;如果出现异常,则回滚事务。
foreach (var newEntity in newEntityList)
{
cmd.Parameters.Clear();//注意 #region MyRegion
cmd.Parameters.Add("@StudentId", SqlDbType.UniqueIdentifier).Value = newEntity.StudentId;
cmd.Parameters.Add("@StudentNo", SqlDbType.Int).Value = newEntity.StudentNo;
cmd.Parameters.Add("@IdCard", SqlDbType.NVarChar).Value = newEntity.IdCard ?? (object)DBNull.Value;
cmd.Parameters.Add("@RealName", SqlDbType.NVarChar).Value = newEntity.RealName ?? (object)DBNull.Value;
#endregion rows += cmd.ExecuteNonQuery();
}
message = rows.ToString();
tran.Commit();//提交事务
return true;
}
catch (Exception e)
{
message = e.Message;
tran.Rollback();//回滚事务
return false;
}
}
}
8.修改数据列表
/// <summary>
/// 修改数据列表
/// </summary>
/// <param name="updateEntityList"></param>
/// <param name="message"></param>
/// <returns></returns>
public static bool Update(List<Student> updateEntityList, out string message)
{
message = string.Empty;
int rows = ;
string sql = @"update [Student] set [StudentNo]=@StudentNo,[IdCard]=@IdCard,[RealName]=@RealName where [StudentId]=@StudentId";
using (SqlConnection conn = new SqlConnection(datalink.ConnectionString))
{
conn.Open(); SqlCommand cmd = conn.CreateCommand();
SqlTransaction tran = conn.BeginTransaction();
cmd.CommandText = sql;
cmd.Connection = conn;
cmd.Transaction = tran; try
{
//打开数据库连接后,循环执行insert语句。在循环过程中,如果没有出现异常,则提交事务;如果出现异常,则回滚事务。
foreach (var newEntity in updateEntityList)
{
cmd.Parameters.Clear();//注意
#region MyRegion
cmd.Parameters.Add("@StudentId", SqlDbType.UniqueIdentifier).Value = newEntity.StudentId;
cmd.Parameters.Add("@StudentNo", SqlDbType.Int).Value = newEntity.StudentNo;
cmd.Parameters.Add("@IdCard", SqlDbType.NVarChar).Value = newEntity.IdCard ?? (object)DBNull.Value;
cmd.Parameters.Add("@RealName", SqlDbType.NVarChar).Value = newEntity.RealName ?? (object)DBNull.Value;
#endregion
rows += cmd.ExecuteNonQuery();
}
message = rows.ToString();
tran.Commit();//提交事务
return true;
}
catch (Exception e)
{
message = e.Message;
tran.Rollback();//回滚事务
return false;
}
}
}
9.删除数据列表
/// <summary>
/// 删除数据列表
/// </summary>
/// <param name="studentIdList"></param>
/// <param name="message"></param>
/// <returns></returns>
public static bool Delete(List<Student> studentIdList, out string message)
{
message = string.Empty;
int rows = ;
string sql = @"delete from [Student] where [StudentId]=@StudentId";
using (SqlConnection conn = new SqlConnection(datalink.ConnectionString))
{
conn.Open(); SqlCommand cmd = conn.CreateCommand();
SqlTransaction tran = conn.BeginTransaction(); cmd.CommandText = sql;
cmd.Connection = conn;
cmd.Transaction = tran; try
{
//打开数据库连接后,循环执行insert语句。在循环过程中,如果没有出现异常,则提交事务;如果出现异常,则回滚事务。
foreach (var studentId in studentIdList)
{
cmd.Parameters.Clear();//注意
cmd.Parameters.Add("@StudentId", SqlDbType.UniqueIdentifier).Value = studentId;
rows += cmd.ExecuteNonQuery();
}
message = rows.ToString();
tran.Commit();//提交事务
return true;
}
catch (Exception e)
{
message = e.Message;
tran.Rollback();//回滚事务
return false;
}
}
}
ADO.NET操作SQL Server:数据库操作类(未封装)的更多相关文章
- [转]C#操作SQL Server数据库
转自:C#操作SQL Server数据库 1.概述 ado.net提供了丰富的数据库操作,这些操作可以分为三个步骤: 第一,使用SqlConnection对象连接数据库: 第二,建立SqlComman ...
- SQL Server学习之路(七):Python3操作SQL Server数据库
0.目录 1.前言 2.准备工作 3.简单测试语句 4.提交与回滚 5.封装成类的写法 1.前言 前面学完了SQL Server的基本语法,接下来学习如何在程序中使用sql,毕竟不能在程序中使用的话, ...
- Python 学习笔记:Python 操作 SQL Server 数据库
最近要将数据写到数据库里,学习了一下如何用 Python 来操作 SQL Server 数据库. 一.连接数据库: 首先,我们要连接 SQL Server 数据库,需要安装 pymssql 这个第三方 ...
- 基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD
完成一个RESTful服务,提供几个访问接口,用来操作较简单的联系人信息,数据保存在Sql Server数据库中. 1.使用STS创建工程. 使用STS创建RESTful工程,可以参考: <用S ...
- 【转】sql server数据库操作大全——常用语句/技巧集锦/经典语句
本文为累计整理,有点乱,凑合着看吧! ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆ ☆ ☆ ☆ sql 宝 典 ☆ ☆ ☆ 2012年-8月 修订版 ☆ ...
- SQL语句操作SQL SERVER数据库登录名、用户及权限
要想成功访问 SQL Server 数据库中的数据, 我们需要两个方面的授权: 获得准许连接 SQL Server 服务器的权利: 获得访问特定数据库中数据的权利(select, update, de ...
- 使用ADO.NET对SQL Server数据库进行訪问
在上一篇博客中我们给大家简介了一下VB.NET语言的一些情况,至于理论知识的学习我们能够利用VB的知识体系为基础.再将面向对象程序设计语言的知识进行融合便可进行编程实战. 假设我们须要訪问一个企业关系 ...
- c# SQL Server数据库操作-数据适配器类:SqlDataAdapter
SqlDataAdapter类主要在MSSQL与DataSet之间执行数据传输工具,本节将介绍如何使用SqlDataAdapter类来填充DataSet和MSSQL执行新增.修改..删除等操作. 功能 ...
- 转发:C#操作SQL Server数据库
转发自:http://www.cnblogs.com/rainman/archive/2012/03/13/2393975.html 1.概述 2.连接字符串的写法 3.SqlConnection对象 ...
- [资料]C#操作SQL Server数据库
1.概述 ado.net提供了丰富的数据库操作,这些操作可以分为三个步骤: 第一,使用SqlConnection对象连接数据库: 第二,建立SqlCommand对象,负责SQL语句的执行和存储过程的调 ...
随机推荐
- ckplayer的Error #2033:Can not call javascript:ckstyle()解决
在我们添加多个视频的时候,就会出现这个报错:Error #2033:Can not call javascript:ckstyle(); 但是也不是所有的浏览器都不能正常运行,我这边就是IE10不能正 ...
- 第2章 Linux操作系统简介
1. Linux操作系统的构成 (1)内核(kernel) ①操作系统的核心,负责管理系统的进程.内存.设备驱动程序.文件和网络系统. ②控制系统和硬件之间的相互通信. ③决定着系统的性能和稳定性. ...
- 使用打印方法时,要先引用命名空间: Using System.Drawing.Pringing
使用打印方法时,要先引用命名空间: Using System.Drawing.Pringing PrintDocument类的重要属性和方法:属性:DocumentName 设置打印文档时要显示的文 ...
- android手机 ping 虚拟机ubuntu的ip地址
今天使用android手机往虚拟机上ubuntu 上搭建的nginx 和rtmp服务器推送东西的时候,怎么都推不上去. 后来在windows下的cmd里: # adb shell # ping 192 ...
- Spring Boot下Druid连接池的使用配置分析
https://blog.csdn.net/blueheart20/article/details/52384032
- Java Reference & ReferenceQueue一览
Overview The java.lang.ref package provides more flexible types of references than are otherwise ava ...
- css常用属性总结:颜色和单位
在css代码编写中,估计颜色和单位是必不可少的,然而在css中关于颜色和单位值的写法有很多种写法,所以有必要把它弄清楚. 颜色 当初我在初学前端的时候,就会冒出一个疑问“我该如何设置网页颜色?”,一般 ...
- linux进程的几个状态
[linux进程的几个状态] 1. Linux进程状态:R (TASK_RUNNING),可执行状态&运行状态(在run_queue队列里的状态) 2. Linux进程状态:S (TASK_I ...
- C# Action
Action<T> Delegate Encapsulates a method that has a single parameter and does not return a val ...
- sql中从指定位置截取指定长度字符串
1. 字符串函数应用 --从指定索引截取指定长度的字符串 ,) --获取字符串中指定字符的索引(从1开始) select charindex(',','ab,cdefg') --实际应用中的语句 , ...