常用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 ...
随机推荐
- Jmeter——变量嵌套函数使用(__V)案例分析
jmeter版本:5.3 __V官方函数解释: (https://jmeter.apache.org/usermanual/functions.html#__V) 图1-1 解决问题:实现字符串拼接 ...
- NOIp2021 原地退役记
JS-0013 Day -2 - 2021.11.17 打了场 cmd_blk 出的模拟赛,T2 T3 都是做过的原题(AGC010C & ARC092D),于是直接摆烂交暴力垫底,成功为我的 ...
- DP 优化方法大杂烩 & 做题记录 I.
标 * 的是推荐阅读的部分 / 做的题目. 1. 动态 DP(DDP)算法简介 动态动态规划. 以 P4719 为例讲一讲 ddp: 1.1. 树剖解法 如果没有修改操作,那么可以设计出 DP 方案 ...
- sar 系统活动情况报告
sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告, 包括:文件的读写情况.系统调用的使用 ...
- nginx_location
Nginx location 配置语法 1. location [ = | ~ | ~* | ^~ ] uri { ... } 2. location @name { ... } location 配 ...
- 18-Rotate Array-Leetcode
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- APP工程师接入Telink Mesh流程 -3
加密是为了使网络更加的安全.健壮,若由于login.加密等流程 严重影响了 开发进程,也可以通过 修改SDK 固件 将login.加密 环节取消 1.发送数据.接受数据加密,解密去掉 mesh_sec ...
- Excel-计算年龄、工龄 datedif()
函数名称:DATEDIF 主要功能:计算返回两个日期参数的差值. 使用格式:=DATEDIF(date1,date2,"y").=DATEDIF(date1,date2," ...
- UE4打包启动失败:RunUAT.bat ERROR: AutomationTool failed to compile.
打包配置正常的情况下,出现下面Log: RunUAT.bat ERROR: AutomationTool failed to compile. 基本上,可以先排查下任务管理器中是不是有UE4Edito ...
- Spark(二十)【SparkSQL将CSV导入Kudu】
目录 SparkSql 将CSV导入kudu pom 依赖 scala 代码 启动脚本 SparkSql 将CSV导入kudu pom 依赖 <properties> <spark. ...