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封装方法的更多相关文章

  1. iOS常用的封装方法

    做开发也有一段时间了,看了好多大神的代码,总体感觉他们写的代码简洁,好看,然而在对比下我写的代码,混乱,无序,简直不堪入目啊! 总体来说大神们的代码封装的都比较好,对一个项目要重复用到的代码他们都会封 ...

  2. JavaScrpt常用的封装方法

    1.闭包封装.在这个封装方法中,所有的实例成员都共享属性和方法, 使得所有得方法和属性都私有且对象间共享 (function ($) { var Person = function(name) { r ...

  3. web前端常用的封装方法

    1.放大镜 //页面加载完毕后执行 window.onload = function () { var oDemo = document.getElementById('demo'); var oMa ...

  4. Lua常用封装方法

    Lua 获取随机值 --获取随机值,指定上限和下限 function getRandom(min,max) -- 接收一个整数n作为随即序列的种子 math.randomseed(os.time()) ...

  5. 封装常用的selenium方法

    package com.yk.userlive.base; import java.net.MalformedURLException;import java.net.URL;import java. ...

  6. 在项目中常用的JS方法封装

    使用方法简单,只需要放在你的 utils.js 工具文件中,直接export const 加上下面封装方法,在别的文件中使用 {方法1,方法2,方法3...}引用后直接使用即可. 01.输入一个值.返 ...

  7. AppDelegate减负之常用三方封装 - 友盟分享 / 三方登录篇

    之前完成了 AppDelegate减负之常用三方封装 - 友盟推送篇: http://www.cnblogs.com/zhouxihi/p/7113511.html 今天接着来完成 - 友盟分享和三方 ...

  8. 【终结版】C#常用函数和方法集汇总

    C#里面的常用的函数和方法非常重要,然而做题的时候会经常忘记这些封装好的方法,所以我总结一下 C#常用函数和方法集. [1]C#操作字符串的常用使用方法 在 C# 中,您可以使用字符数组来表示字符串, ...

  9. MP实战系列(十二)之封装方法详解(续二)

    继续MP实战系列(十一)之封装方法详解(续一)这篇文章之后. 此次要讲的是关于查询. 查询是用的比较多的,查询很重要,好的查询,加上索引如鱼得水,不好的查询加再多索引也是无济于事. 1.selectB ...

随机推荐

  1. WebRTC与CSS滤镜(CSS filter)

    我们知道了如何使用WebRTC打开摄像头,可以截取视频帧并且用canvas显示出来. 本文将滤镜与视频结合.给视频加上一层滤镜.主要使用到的是filter属性. canvas与滤镜 先来看filter ...

  2. UOJ #228 - 基础数据结构练习题(势能线段树+复杂度分析)

    题面传送门 神仙题. 乍一看和经典题 花神游历各国有一点像,只不过多了一个区间加操作.不过多了这个区间加操作就无法再像花神游历各国那样暴力开根直到最小值为 \(1\) 为止的做法了,稍微感性理解一下即 ...

  3. Linux 安装和使用 RAR工具

    RAR 安装 方法一.通过apt命令安装 rar 和 unrar 未安装 unrar 的情况下,提取 RAR 文件会报出"未能提取"错误 Ubuntu 安装 rar和 unrar( ...

  4. perl练习——计算点突变

    题目来源:http://rosalind.info/problems/hamm/ 一.程序目的:计算序列点突变(Point Mutations) 输入: GAGCCTACTAACGGGAT CATCG ...

  5. SpringBoot Logback 日志配置

    目录 前言 日志格式 日志输出 日志轮替 日志级别 日志分组 小结 前言 之前使用 SpringBoot 的时候,总是习惯于将日志框架切换为 Log4j2,可能是觉得比较靠谱,也可能年龄大了比较排斥新 ...

  6. Redis6 新特性

    Redis6新特性 ACL安全策略 ACL(access control list): 访问控制列表,可以设置多个用户,并且给每个用户单独设置命令权限和数据权限 default用户和使用require ...

  7. navicate连接Mysql5.7时,显示Access denied for user 'root'@'localhost' (using password: YES) 错误

    最近新装了Mysql5.7,按如下设置好了允许远程连接    (1)找到mysql配置文件并修改 sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf 将bind-ad ...

  8. Linux基础命令---alias别名

    alias Alias不带参数或使用-p选项在标准输出上以"name=value"的形式打印别名列表.当提供参数时,为其值给定的每个名称定义一个别名.值中的尾随空格将导致在扩展别名 ...

  9. 【编程思想】【设计模式】【结构模式Structural】享元模式flyweight

    Python版 https://github.com/faif/python-patterns/blob/master/structural/flyweight.py #!/usr/bin/env p ...

  10. Vue中的8种组件通信方式

    Vue是数据驱动视图更新的框架,所以对于vue来说组件间的数据通信非常重要. 常见使用场景可以分为三类: 父子组件通信: props / $emit $parent / $children provi ...