using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration; namespace AppUtility
{
public class ADOTools
{
//操作数据库的API
public static readonly string CONNECTION_STR = ConfigurationManager.ConnectionStrings["TestStr"].ToString(); /// <summary>
/// 执行SQL操作,ExcuteNoQuery
/// </summary>
/// <param name="connectionStr">数据库链接字符串</param>
/// <param name="strSql">执行的SQL语句</param>
/// <returns>影响行数</returns>
public static int ExcuteNoQuery(string connectionStr, string strSql)
{
int flag = ;
SqlConnection conn = null;
SqlCommand cmd = null;
try
{
conn = new SqlConnection(connectionStr);
conn.Open();
cmd = new SqlCommand(strSql, conn);
flag = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
conn.Dispose();
}
return flag;
} /// <summary>
/// 执行SQL操作,ExecuteScalar
/// </summary>
/// <param name="connectionStr">数据库链接字符串</param>
/// <param name="strSql">执行的SQL语句</param>
/// <returns>返回唯一值</returns>
public static object ExecuteScalar(string connectionStr, string strSql)
{
object flag = ;
SqlConnection conn = null;
SqlCommand cmd = null;
try
{
conn = new SqlConnection(connectionStr);
conn.Open();
cmd = new SqlCommand(strSql, conn);
flag = cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
conn.Dispose();
}
return flag;
} /// <summary>
/// Reader查询
/// </summary>
/// <param name="connectionStr">数据库链接字符串</param>
/// <param name="strSql">执行的SQL语句</param>
/// <returns>影响行数</returns>
public static SqlDataReader ExcuteReader(string connectionStr, string strSql)
{
SqlConnection conn = new SqlConnection(connectionStr);
conn.Open();
SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
} /// <summary>
/// DataTable查询
/// </summary>
/// <param name="connectionStr">数据库链接字符串</param>
/// <param name="strSql">执行的SQL语句</param>
/// <returns>返回DataTable数据源</returns>
public static DataTable ExcuteDataTable(string connectionStr, string strSql)
{
DataTable dt = null;
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataAdapter adapter = null;
try
{
conn = new SqlConnection(connectionStr);
cmd = new SqlCommand(strSql, conn);
adapter = new SqlDataAdapter(cmd);
dt = new DataTable();
adapter.Fill(dt);
}
catch (Exception ex)
{
dt = null;
throw ex;
}
finally
{
adapter.Dispose();
cmd.Dispose();
conn.Dispose();
}
return dt;
} /// <summary>
/// DataTable参数化查询
/// </summary>
/// <param name="connectionStr">数据库链接字符串</param>
/// <param name="strSql">执行的SQL语句</param>
/// <param name="CommandType">存储过程或者SQL</param>
/// <returns>返回DataTable数据源</returns>
public static DataTable ExcuteDataTable(string connectionStr, string strSql, CommandType cd, SqlParameter[] sp)
{
DataTable dt = null;
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataAdapter adapter = null;
try
{
conn = new SqlConnection(connectionStr);
cmd = new SqlCommand(strSql, conn);
cmd.CommandType = cd;
cmd.Parameters.AddRange(sp);
adapter = new SqlDataAdapter(cmd);
dt = new DataTable();
adapter.Fill(dt);
}
catch (Exception ex)
{
dt = null;
throw ex;
}
finally
{
adapter.Dispose();
cmd.Dispose();
conn.Dispose();
}
return dt;
} /// <summary>
/// 参数化执行SQL
/// </summary>
/// <param name="connectionStr">连接字符串</param>
/// <param name="strSql">执行的SQL语句</param>
/// <param name="cd">存储过程或者SQL</param>
/// <param name="sp">参数数组</param>
/// <returns>影响的行数</returns>
public static int ExcuteNoQuery(string connectionStr, string strSql, CommandType cd, SqlParameter[] sp)
{
int flag = ;
SqlConnection conn = null;
SqlCommand cmd = null;
try
{
conn = new SqlConnection(connectionStr);
conn.Open();
cmd = new SqlCommand(strSql, conn);
cmd.CommandType = cd;
cmd.Parameters.AddRange(sp);
flag = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
conn.Dispose();
}
return flag;
} /// <summary>
/// 参数化执行SQL,返回插入后自增ID
/// </summary>
/// <param name="connectionStr">连接字符串</param>
/// <param name="strSql">执行的SQL语句</param>
/// <param name="cd">存储过程或者SQL</param>
/// <param name="sp">参数数组</param>
/// <returns>影响的行数</returns>
public static int ExcuteNoQueryByReturnID(string connectionStr, string strSql, CommandType cd, SqlParameter[] sp)
{
int flag = ;
SqlConnection conn = null;
SqlCommand cmd = null;
try
{
conn = new SqlConnection(connectionStr);
conn.Open();
cmd = new SqlCommand(strSql, conn);
cmd.CommandType = cd;
cmd.Parameters.AddRange(sp);
cmd.ExecuteNonQuery();
flag = Convert.ToInt32(sp[].Value);
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
conn.Dispose();
}
return flag;
} /// <summary>
/// 执行SQL事务,ExcuteNoQuery
/// </summary>
/// <param name="transaction">事务对象</param>
/// <param name="strSql">执行脚本</param>
/// <returns></returns>
public static int ExcuteNoQuery(SqlTransaction transaction,string strSql)
{
int flag = ;
SqlCommand cmd = null;
try
{
cmd = new SqlCommand(strSql, transaction.Connection, transaction);
flag = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
}
return flag;
} /// <summary>
/// 执行SQL事务,ExecuteScalar
/// </summary>
/// <param name="transaction">事务对象</param>
/// <param name="strSql">执行脚本</param>
/// <returns></returns>
public static object ExecuteScalar(SqlTransaction transaction, string strSql)
{
object flag = ;
SqlCommand cmd = null;
try
{
cmd = new SqlCommand(strSql, transaction.Connection, transaction);
flag = cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
}
return flag;
}
}
}

ADO.NET工具类(三)的更多相关文章

  1. java工具类(三)之生成若干位随机数

    java 生成若干位随机数的问题 在一次编程的过程中偶然碰到一个小问题,就是需要生成一个4位数的随机数,如果是一个不到4位大的数字,前面可以加0来显示.因为要求最后是一个4位的整数,不带小数点.当时就 ...

  2. JDK1.8 LocalDate 使用方式;LocalDate 封装Util,LocalDate工具类(三)

    未完待续 ........ 前言: 大企鹅的日常分享,第三步,最近一直在想策略设计模式和工厂模式结合优化ifelse的写法,看了很多资料,终于写出了自己要写的东西,在这段时间里,也有求助小伙伴,但是, ...

  3. ADO.NET工具类(二)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  4. ADO.NET工具类(一)

    using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; usin ...

  5. 【转】java缩放图片、java裁剪图片代码工具类

    一首先看下效果 二工具类 三测试类 在系统的上传图片功能中,我们无法控制用户上传图片的大小,用户可能会上传大到几十M小到1k的的图片,一方面图片太大占据了太多的空间,另一方面,我们没办法在页面上显示统 ...

  6. java中常用的工具类(三)

    继续分享java中常用的一些工具类.前两篇的文章中有人评论使用Apache 的lang包和IO包,或者Google的Guava库.后续的我会加上的!谢谢支持IT江湖 一.连接数据库的综合类       ...

  7. ADO.NET复习总结(5)--工具类SqlHelper 实现登录

    工具类SqlHelper 即:完成常用数据库操作的代码封装 一.基础知识1.每次进行操作时,不变的代码: (1)连接字符串:(2)往集合存值:(3)创建连接对象.命令对象:(4)打开连接:(5)执行命 ...

  8. 浅析Android Camera开发中的三个尺寸和三种变形 (贡献一个自适配Picturesize和Previewsize的工具类)

    转至 (http://blog.csdn.net/yanzi1225627/article/details/17652643) 经常听人问Camera开发中,各种变形问题,今天有空就在此梳理总结下. ...

  9. 一、JDBC的概述 二、通过JDBC实现对数据的CRUD操作 三、封装JDBC访问数据的工具类 四、通过JDBC实现登陆和注册 五、防止SQL注入

    一.JDBC的概述###<1>概念 JDBC:java database connection ,java数据库连接技术 是java内部提供的一套操作数据库的接口(面向接口编程),实现对数 ...

随机推荐

  1. gogs仓库管理软件 exec: "git-upload-pack": executable file not found in $PATH

    当配置完个人中心的ssh公钥的时候,在客户端拉取代码的时候,提示如下错误: Cloning into 'comix-b2m'... Gogs: Internal error fatal: Could ...

  2. 如何理解render: h => h(App)

    学习vuejs的时候对这句代码不理解 new Vue({ el: '#app', router, store, i18n, render: h => h(App) }) 查找了有关资料,以下为结 ...

  3. 二维数组遍历的方式(for普通循环遍历、foreach循环遍历、toString方式遍历)

    package com.Summer_0421.cn; import java.lang.reflect.Array; import java.util.Arrays; /** * @author S ...

  4. AI VGG16

    VGG(Visual Geometry Group) 16 参考链接: https://arxiv.org/abs/1409.1556

  5. Linux笔记-top命令和load average

    参考资料 top相关: http://blog.csdn.net/zhangchenglikecc/article/details/52103737参考资料 cpu核数: https://www.cn ...

  6. Linux进程管理 (1)进程的诞生

    专题:Linux进程管理专题 目录: Linux进程管理 (1)进程的诞生 Linux进程管理 (2)CFS调度器 Linux进程管理 (3)SMP负载均衡 Linux进程管理 (4)HMP调度器 L ...

  7. a2dp播放流程源码分析

    之前分析了a2dp profile 的初始化的流程,这篇文章分析一下,音频流在bluedroid中的处理流程. 上层的音频接口是调用a2dp hal 里面的接口来进行命令以及数据的发送的. 关于控制通 ...

  8. 我的微信小程序第一篇(入门)

    前言 微信小程序出来已经有一段时间了,已经有很多大牛都写过相关教程了,那么我为啥还要写这篇文章呢?其实仅仅是个人对微信开发的一个兴趣吧,觉得是个很有意思的事情,后面有时间可能会发更多关于小程序的文章, ...

  9. Python-类的特性(property)

    什么是特性property property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 例一:BMI指数(bmi是计算而来的,但很明显它听起来像是一个属性而非方法,如果我们将其做成一个 ...

  10. 实现数据结构与算法需要掌握的C语言

    我使用C语言并不频繁,一般都是用来实现数据结构与算法,因为面向过程的编程方式容易理解算法的原理,但是呢,如果很长时间没写算法,那么就意味着C语言的某些语法就生疏了,但是总有那么一些,在写算法的时候,特 ...