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语句的执行和存储过程的调 ...
随机推荐
- 手工编写servlet
index.jsp: <%@ page language="java" import="java.util.*" contentType="te ...
- Oracle 字符集的查看和修改 --转载
原文地址:Oracle 字符集的查看和修改 作者:piaoliuxiong 一.什么是Oracle字符集 Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关系.ORACLE ...
- 利用MessageFormat实现短信模板的匹配
其实没什么技术含量,因为老是想不起来,所以在此文做下记录. 通常我们的应用系统中都会有很多短信的发送,或者是信息邮件等的推送,而这些信息却有着相同的共性,比如只是用户名换了下. 像下面这条,除了红色字 ...
- 温故而知新-面向对象的PHP
1 类的多态 不同的类对同一操作可以有不同的行为. 比如自行车和汽车都有移动这个成员函数行为, 那么自行车类可以移动,行为和汽车的移动行为肯定不同. 2 析构函数不能有参数 3 __set和__get ...
- 好记性不如烂笔头-linux学习笔记1
好记性不如烂笔头-linux学习笔记1 linux的文件系统有ext2,ext3,ext4,目前主流是ext4 linux主要用于服务器级别的操作系统,安装时需要至少2个分区 一个是交换分区,swap ...
- [代码]set容器查找操作使用
对于set容器来说,查找功能是该容器的主要优势,故针对该容器查找功能作一测试. 主要有如下API接口: 测试源码如下: #include<set> void test(){ set< ...
- Spring Boot实践——AOP实现
借鉴:http://www.cnblogs.com/xrq730/p/4919025.html https://blog.csdn.net/zhaokejin521/article/detai ...
- CCS5配置使用GenCodecPkg生成CODEC SERVER
1. 引言 CCS5中集成了算法生成工具的插件,使用XDAIS Tools条目里面的GenAlg命令生成的算法框架如下: 不过,我没找到如何在CCS中使用这个工程.难不成要把这个框架文件放在Linux ...
- 18.4Sum (Map)
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- C# \/date(1498820611133+0800)\/ 转DateTime
开发中经常遇到日期转换问题,特别是做接口的时候,现在整理了下时间戳转为C#格式时间的方法: /// <summary> /// 时间戳转为C#格式时间 /// </summary&g ...