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. [SVN] Branch and Tag

    在 SVN 中,如何建立分支以及如何标记Tag. 右键要处理的文件夹,选择 "TortoiseSVN" - "Branch/tag...",进入下面界面: To ...

  2. k8s statefulset controller源码分析

    statefulset controller分析 statefulset简介 statefulset是Kubernetes提供的管理有状态应用的对象,而deployment用于管理无状态应用. 有状态 ...

  3. Codeforces 702F - T-shirts(平衡树+势能分析)

    题面传送门 首先肯定将所有物品排个序. 考虑暴力做法,对于每个询问,枚举所有物品,能买就买.不过扫一眼就知道无法直接优化. 不妨换个角度,暴力做法是枚举询问,这次我们枚举物品.从左到右依次枚举所有物品 ...

  4. Codeforces 632F - Magic Matrix(暴力 bitset or Prim 求最小生成树+最小瓶颈路)

    题面传送门 开始挖老祖宗(ycx)留下来的东西.jpg 本来想水一道紫题作为 AC 的第 500 道紫题的,结果发现点开了道神题. 首先先讲一个我想出来的暴力做法.条件一和条件二直接扫一遍判断掉.先将 ...

  5. Small but Funny Tricks [Remember them all!]

    模数 1e9 的神奇求行列式: #include <bits/stdc++.h> using namespace std; const int maxn = 1e2, mod = 1e9; ...

  6. Discontinuous Galerkin method for steady transport problem

    下面讨论如何使用 Discontinuous Galerkin 求解恒定对流问题. 1.简介 恒定状态对流方程 \[\begin{equation} a\cdot \nabla \mathbf{u} ...

  7. 如何反向推断基因型文件中的参考碱基(REF/ALT)?

    目录 需求 解决 方法一 方法二 需求 客户随手丢来一个基因型文件,类似于hapmap格式,只是少了中间多余的那几列,像这种类hapmap格式文件,往往是芯片数据. 这样的数据因为缺乏等位基因:参考碱 ...

  8. python-django-分页处理

    每个模型都有一个管理器,Manager 通过分页可以先加载一部分的数据内容,然后避免大量的查询带来的等待时间 应用场景是,排行榜,前十条或者 for i in range(100): ...: ... ...

  9. 金蝶EAS——登录某个数据中心门户时报错“获取用户相关信息失败!请查看服务器日志,并确认是否数据库设置错误或者版本不匹配!”

    登录服务器后台,查看金蝶BOS控制台,选择数据中心中的目标数据中心,点击测试连接,提示报错如下: 说明是数据库问题,需要登录数据库服务器去检查数据库.详细操作见:

  10. git的使用理解(分支合并的使用理解,多人编程的解决方案)

    本文主要记录了对git日常使用的一些理解,主要是对git分支的一些感悟. git强大的版本控制系统,之前也使用过SVN,感觉上git对于多人开发的版本控制更加强大,特别是最近对git分支的使用,更是深 ...