常用DBhelper封装方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public class DBhelper
{
string dbStr = "Data Source=.;Initial Catalog=EducationManagement;Integrated Security=True";
/// <summary>
/// 模块:DBhelper
/// 作者:杨萍
/// 时间:2020-4-1
/// </summary>
//创建增删改的方法
public int ExecDML(string sql)
{
try
{
int res = 0;
//连接数据库
using (SqlConnection con = new SqlConnection(dbStr))
{
//打开连接数据库
con.Open();
//执行sql
SqlCommand com = new SqlCommand(sql, con);
res = com.ExecuteNonQuery();
}
return res;
}
catch (Exception ex)
{
Console.WriteLine("错误信息:DBhelper:ExecDML" + ex);
throw ex;
}
}
/// <summary>
/// 模块:DBhelper
/// 作者:杨萍
/// 时间:2020-4-1
/// </summary>
//调用存储过程增删改的方法
public int ProcedureExecDML(string sql,SqlParameter [] parameters)
{
try
{
int res = 0;
//连接数据库
using (SqlConnection con = new SqlConnection(dbStr))
{
//执行sql
SqlCommand com = new SqlCommand(sql, con);
//设置命令的类型是存储过程的类型
com.CommandType = CommandType.StoredProcedure;
if (parameters != null && parameters.Length>0)
{
com.Parameters.AddRange(parameters);
}
//打开连接数据库
con.Open();
res = com.ExecuteNonQuery();
}
return res;
}
catch (Exception ex)
{
Console.WriteLine("错误信息:DBhelper:PreventSqlInjectionExecDML" + ex);
throw ex;
}
}
/// <summary>
/// 模块:DBhelper
/// 作者:杨萍
/// 时间:2020-4-1
/// </summary>
//防止sql注入增删改的方法
public int PreventSqlInjectionExecDML(string sql, SqlParameter[] parameters)
{
try
{
int res = 0;
//连接数据库
using (SqlConnection con = new SqlConnection(dbStr))
{
//执行sql
SqlCommand com = new SqlCommand(sql, con);
//设置命令的类型是存储过程的类型
if (parameters != null && parameters.Length > 0)
{
com.Parameters.AddRange(parameters);
}
//打开连接数据库
con.Open();
res = com.ExecuteNonQuery();
}
return res;
}
catch (Exception ex)
{
Console.WriteLine("错误信息:DBhelper:PreventSqlInjectionExecDML" + ex);
throw ex;
}
}
/// <summary>
/// 模块:DBhelper
/// 作者:杨萍
/// 时间:2020-4-1
/// </summary>
//创建查询总数的方法,即查询一行一列的数据的方法
public object ExecCount(string sql)
{
try
{
object obj = null;
using (SqlConnection con = new SqlConnection(dbStr))
{
con.Open();
SqlCommand com = new SqlCommand(sql, con);
obj = com.ExecuteScalar();
}
return obj;
}
catch (Exception ex)
{
Console.WriteLine("错误信息:DBhelper:ExecCount"+ex);
throw ex;
}
}
/// <summary>
/// 模块:DBhelper
/// 作者:杨萍
/// 时间:2020-4-1
/// </summary>
//读取单条或多条数据,如果是单行,直接去取值就行,如果是多行,使用for循环或foreach逐行遍历
public SqlDataReader FindDataRead(string sql)
{
try
{
SqlDataReader sdr = null;
SqlConnection con = new SqlConnection(dbStr);
con.Open();
SqlCommand com = new SqlCommand(sql, con);
sdr = com.ExecuteReader();
return sdr;
}
catch (Exception ex)
{
Console.WriteLine("错误信息:DBhelper:FindDataRead" + ex);
throw ex;
}
}
/// <summary>
/// 模块:DBhelper
/// 作者:杨萍
/// 时间:2020-4-1
/// </summary>
//创建查询单表的方法
public DataTable DataTable(string sql)
{
try
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(dbStr))
{
con.Open();
SqlCommand com = new SqlCommand(sql, con);
SqlDataAdapter sda = new SqlDataAdapter(com);
sda.Fill(dt);
}
return dt;
}
catch (Exception ex)
{
Console.WriteLine("错误信息:DBhelper:DataTable" + ex);
throw ex;
}
}
/// <summary>
/// 模块:DBhelper
/// 作者:杨萍
/// 时间:2020-4-1
/// </summary>
//带参数的单表查询
public DataTable FindDataTable(string sql, SqlParameter[] sqlParameters)
{
try
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(dbStr))
{
con.Open();
SqlCommand com = new SqlCommand(sql, con);
if (sqlParameters != null)
{
com.Parameters.AddRange(sqlParameters);
}
SqlDataAdapter sda = new SqlDataAdapter(com);
sda.Fill(dt);
}
return dt;
}
catch (Exception ex)
{
Console.WriteLine("错误信息:DBhelper:DataTable" + ex);
throw ex;
}
}
/// <summary>
/// 模块:DBhelper
/// 作者:杨萍
/// 时间:2020-4-1
/// </summary>
//存储过程单表查询
public PageData FindExecDataTable(string sql, SqlParameter[] sqlParameters)
{
try
{
using (SqlConnection con = new SqlConnection(dbStr))
{
con.Open();
SqlCommand com = new SqlCommand(sql, con);
//调用存储过程必须要加的一行
com.CommandType = CommandType.StoredProcedure;
if (sqlParameters != null&& sqlParameters.Length>0)
{
com.Parameters.AddRange(sqlParameters);
}
//获取存储过程的返回值
SqlParameter countpageOutPut = com.Parameters.Add("@totalPage", SqlDbType.Int);
countpageOutPut.Direction = ParameterDirection.Output;
SqlParameter countOutPut = com.Parameters.Add("@totalRow", SqlDbType.Int);
countOutPut.Direction = ParameterDirection.Output;
SqlDataAdapter sda = new SqlDataAdapter(com);
DataTable dt = new DataTable();
sda.Fill(dt);
//获取返回值
string count = countOutPut.Value.ToString();
string countpage = countpageOutPut.Value.ToString();
PageData pageData = new PageData();
pageData.DataTable = dt;
pageData.Count = int.Parse(count);
pageData.Countpage = int.Parse(countpage);
return pageData;
}
}
catch (Exception ex)
{
Console.WriteLine("错误信息:DBhelper:DataTable" + ex);
throw ex;
}
}
/// <summary>
/// 模块:DBhelper
/// 作者:杨萍
/// 时间:2020-4-1
/// </summary>
//查询创建多表的方法
public DataSet DataSet(string sql)
{
try
{
DataSet dst = new DataSet();
using (SqlConnection con = new SqlConnection(dbStr))
{
con.Open();
SqlCommand com = new SqlCommand(sql, con);
SqlDataAdapter sda = new SqlDataAdapter(com);
sda.Fill(dst);
}
return dst;
}
catch (Exception ex)
{
Console.WriteLine("错误信息:DBhelper:DataSet"+ex);
throw ex;
}
}
/// <summary>
/// 模块:DBhelper
/// 作者:杨萍
/// 时间:2020-4-1
/// </summary>
//创建查询多表的方法
public SqlDataAdapter SqlDataAdapter(string sql)
{
try
{
SqlDataAdapter sda = null;
SqlConnection con = new SqlConnection(dbStr);
con.Open();
SqlCommand com = new SqlCommand(sql, con);
sda = new SqlDataAdapter(com);
return sda;
}
catch (Exception ex)
{
Console.WriteLine("错误信息:DBhelper:SqlDataAdapter"+ex);
throw ex;
}
}
}
}
常用DBhelper封装方法的更多相关文章
- iOS常用的封装方法
做开发也有一段时间了,看了好多大神的代码,总体感觉他们写的代码简洁,好看,然而在对比下我写的代码,混乱,无序,简直不堪入目啊! 总体来说大神们的代码封装的都比较好,对一个项目要重复用到的代码他们都会封 ...
- JavaScrpt常用的封装方法
1.闭包封装.在这个封装方法中,所有的实例成员都共享属性和方法, 使得所有得方法和属性都私有且对象间共享 (function ($) { var Person = function(name) { r ...
- web前端常用的封装方法
1.放大镜 //页面加载完毕后执行 window.onload = function () { var oDemo = document.getElementById('demo'); var oMa ...
- Lua常用封装方法
Lua 获取随机值 --获取随机值,指定上限和下限 function getRandom(min,max) -- 接收一个整数n作为随即序列的种子 math.randomseed(os.time()) ...
- 封装常用的selenium方法
package com.yk.userlive.base; import java.net.MalformedURLException;import java.net.URL;import java. ...
- 在项目中常用的JS方法封装
使用方法简单,只需要放在你的 utils.js 工具文件中,直接export const 加上下面封装方法,在别的文件中使用 {方法1,方法2,方法3...}引用后直接使用即可. 01.输入一个值.返 ...
- AppDelegate减负之常用三方封装 - 友盟分享 / 三方登录篇
之前完成了 AppDelegate减负之常用三方封装 - 友盟推送篇: http://www.cnblogs.com/zhouxihi/p/7113511.html 今天接着来完成 - 友盟分享和三方 ...
- 【终结版】C#常用函数和方法集汇总
C#里面的常用的函数和方法非常重要,然而做题的时候会经常忘记这些封装好的方法,所以我总结一下 C#常用函数和方法集. [1]C#操作字符串的常用使用方法 在 C# 中,您可以使用字符数组来表示字符串, ...
- MP实战系列(十二)之封装方法详解(续二)
继续MP实战系列(十一)之封装方法详解(续一)这篇文章之后. 此次要讲的是关于查询. 查询是用的比较多的,查询很重要,好的查询,加上索引如鱼得水,不好的查询加再多索引也是无济于事. 1.selectB ...
随机推荐
- python的异常打印
在代码运行中有的代码可能会发生异常,但是奇怪的是异常信息并没有打印出来, 于是我们在代码中加入这个就能打印出来啦. try: #playsound(msg.file_name()) #playsoun ...
- Spring扩展点-v5.3.9
Spring 扩展点 **本人博客网站 **IT小神 www.itxiaoshen.com 官网地址****:https://spring.io/projects/spring-framework T ...
- doxygen相关命令
主要配置修改 整个程序配置分几个部分 Project related configuration options 项目相关,包括: 项目名 输出目录 输出语言 是否显示继承属性 是否对C.Java.F ...
- R语言与医学统计图形【8】颜色的选取
R语言基础绘图系统 基础绘图包之低级绘图函数--内置颜色. 1.内置颜色选取 功能657种内置颜色.colors() 调色板函数:palette(), rgb(), rainbow(). palett ...
- 【Redis】过期键删除策略和内存淘汰策略
Redis 过期键策略和内存淘汰策略 目录 Redis 过期键策略和内存淘汰策略 设置Redis键过期时间 Redis过期时间的判定 过期键删除策略 定时删除 惰性删除 定期删除 Redis过期删除策 ...
- C语言中的main函数的参数解析
main()函数既可以是无参函数,也可以是有参的函数.对于有参的形式来说,就需要向其传递参数.但是其它任何函数均不能调用main()函数.当然也同样无法向main()函数传递,只能由程序之外传递而来. ...
- day17 阶段测验
题目 1.找出/proc/meminfo文件中以s开头的行,至少用三种方式忽略大小写 有以下几种方法: [root@localhost ~]# grep -iE "^s" /pro ...
- [php反序列化] CVE-2020-15148(Yii2 反序列化漏洞) 漏洞复现
漏洞影响范围 Yii2 < 2.0.38 环境搭建 Yii2.0.37 漏洞分析 首先定位到漏洞起始点 为什么是这儿?我们该怎么发现是某个类的某个函数?为什么不是其他函数? 一般是__destr ...
- set、multiset深度探索
set/multiset的底层是rb_tree,因此它有自动排序特性.set中的元素不允许重复必须独一无二,key与value值相同,multiset中的元素允许重复. set的模板参数key即为关键 ...
- What happens when more restrictive access is given to a derived class method in C++?
We have discussed a similar topic in Java here. Unlike Java, C++ allows to give more restrictive acc ...