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中获取配置文件中的参数.
web.xml (添加init-param) <?xml version="1.0" encoding="UTF-8"?> <web-app ...
- php+nginx环境下的php报错设置
修改php.ini的配置: display_errors = Off(关闭) display_errors = On(开启) 设置修改完成后重启php-cgi进程 killall -9 php-cgi ...
- python写exploit采集器
前言: 根据天文地理与风水学,我掐指一算的看到了一篇不错的文章,文章里面写到整理exploit 我顿时心理想写一个exploit采集器,那么说时迟那时快.就开始写了 代码: 思路:http://exp ...
- WordVBA常用项
将光标所在段落选中 Selection.MoveUp unit:=wdParagraphSelection.MoveDown unit:=wdParagraph, Extend:=wdExtend ' ...
- C入门程序整体框架图
0.1:概述, 从头开始介绍一门编程语言总是显得很困难,因为有许多的细节还没有介绍,很难让读者在大脑中形成一幅完整的图, 所以起步时以一个列程序向学折介绍大体的C,试图使大家对C有一个整体大概 影响. ...
- 【CentOS 6.5】【转】新版本linux生成xorg.conf
新版本的linux如何生成xorg.conf 较新版本的linux系统都已经没有xorg.conf文件,但是有时候为了对显示做微调或为了支持多屏显示等原因,还需要手工生成一个xorg.conf,然后根 ...
- vim调整粘贴时的文本缩进
解决办法: 1. 在拷贝前输入:set paste (这样的话,vim就不会启动自动缩进,而只是纯拷贝粘贴)2. 拷贝完成之后,输入:set nopaste (关闭paste) 在 Vim 中粘贴文本 ...
- Mybatis之XML使用Enum枚举传递数据
在Mybatis中,处理枚举类的TypeHandler有两个: EnumTypeHandler: 用于保存枚举名 EnumOrdinalTypeHandler: 用于保存枚举的序号. 在实际项目中,以 ...
- 若a与m互质,则a不影响m的完全剩余组
[若a与m互质,则a不影响m的完全剩余组] 设t通过m的完全剩余组,若at不通过m的完全剩余组, 则会有at1=at2(mod m),即a(t1-t2)|m. 因为(a,m)=1,所以(t1-t2)| ...
- go_gc
如果想知道当前的内存状态,可以使用: // fmt.Printf("%d\n", runtime.MemStats.Alloc/1024) // 此处代码在 Go 1.5.1下不再 ...