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语句的执行和存储过程的调 ...
随机推荐
- SPOJ Count on a tree(主席树+LCA)
一.题目 COT - Count on a tree You are given a tree with N nodes. The tree nodes are numbered from 1 to ...
- 【Codeforces】Codeforces Round #491 (Div. 2) (Contest 991)
题目 传送门:QWQ A:A - If at first you don't succeed... 分析: 按照题意模拟 代码: #include <bits/stdc++.h> usin ...
- [saiku] 通过管理台配置用户、schema和数据源
上一篇讲到了如何下载和安装saiku [http://www.cnblogs.com/avivaye/p/4877680.html] 本文简介下saiku用户的配置操作和需要注意的点 一.添加用户 S ...
- hive基本结构与数据存储
一.Hive简介 Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供类SQL查询功能.还可以将 SQL 语句转换为 MapReduce 任务进行运行,通过自 ...
- Python开发一个堡垒机
项目实战:运维堡垒机开发 前景介绍 到目前为止,很多公司对堡垒机依然不太感冒,其实是没有充分认识到堡垒机在IT管理中的重要作用的,很多人觉得,堡垒机就是跳板机,其实这个认识是不全面的,跳板功能只是堡垒 ...
- hadoop学习day3 mapreduce笔记
1.对于要处理的文件集合会根据设定大小将文件分块,每个文件分成多块,不是把所有文件合并再根据大小分块,每个文件的最后一块都可能比设定的大小要小 块大小128m a.txt 120m 1个块 b.txt ...
- ES6系列_15之class类的使用
JS语言的传统方法是通过构造函数,定义并生成新对象,是一种基于原型的面向对象系统.在ES6中新增加了类的概念,可以使用 class 关键字声明一个类,之后以这个类来实例化对象.1.先来看看es5与es ...
- 使用MVC实现登录功能
首先,从底层开始即Models: (1)通用数据访问类(封装数据访问类方法):SqlHelper类 使用命名空间:using System.Data; using System.Data.SqlCli ...
- 解决SQL将varchar值转换为数据类型为int的列时发生语法错误
今天遇到一个这样的错误,具体的报错情况如下 解决的方案如下. 数据库MSSQL在比较大小时,出错提示:“将 varchar 值 '24.5' 转换为数据类型为 int 的列时发生语法错!”分析数据库设 ...
- Mac 下输入特殊字符
[Mac 下输入特殊字符] 快捷键是 Control + Commans + Space