using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration; namespace AjaxFinder
{
public class SQLHelper
{ //public static string connectionString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
public static string connectionString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
public SQLHelper()
{ } #region 执行带参数的SQL语句 #region ExecuteNonQuery
/// <summary>
/// 在指定的连接上用指定的参数执行SQL命令(不返回任何行)
/// 一般用语执行执行 UPDATE、INSERT 或 DELETE 语句。
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="commandType">命令类型(存储过程、文本等等)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <returns>该命令影响的行数</returns>
public static int ExecuteNonQuery(CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
if (connectionString == null || connectionString.Length == )
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == )
throw new ArgumentNullException("commandText"); using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open(); SqlCommand command = new SqlCommand();
PrepareCommand(command, connection, commandType, commandText, commandParameters);
int retval = command.ExecuteNonQuery(); connection.Close();
//connection.Dispose();
return retval; }
}
#endregion #region ExecuteReader
/// <summary>
/// 在指定的连接上用指定的参数执行SQL命令
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="commandType">命令类型(存储过程、文本等等)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="commandParameters">命令的参数</param>
/// <returns>返回一个SqlDataReader对象</returns>
public static SqlDataReader ExecuteReader(CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
if (connectionString == null || connectionString.Length == )
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == )
throw new ArgumentNullException("commandText"); SqlConnection connection = null;
try
{
connection = new SqlConnection(connectionString);
connection.Open(); SqlCommand command = new SqlCommand();
PrepareCommand(command, connection, commandType, commandText, commandParameters);
SqlDataReader dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);
return dataReader;
}
catch
{
if (connection != null)
connection.Close();
//connection.Dispose();
return null;
}
} #endregion #region 执行查询语句,返回DataSet
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString, params SqlParameter[] cmdParms)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
//在此处编写执行Sql语句的返回DataSet的通用代码
FillDataSet(CommandType.Text, SQLString, ds, cmdParms); //填充数据集
return ds;
}
}
#endregion #region ExecuteScalar
/// <summary>
/// 在指定的连接上用指定的参数执行SQL命令
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="commandType">命令类型(存储过程、文本等等)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="commandParameters">命令的参数</param>
/// <returns>返回一个单值</returns>
public static object ExecuteScalar(CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
if (connectionString == null || connectionString.Length == )
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == )
throw new ArgumentNullException("commandText"); using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open(); SqlCommand command = new SqlCommand();
PrepareCommand(command, connection, commandType, commandText, commandParameters);
object obj = command.ExecuteScalar();
connection.Close();
//connection.Dispose();
return obj;
}
}
#endregion #region FillDataSet
/// <summary>
/// 填充数据集
/// </summary>
/// <param name="commandType">命令类型(存储过程、文本等等)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="dataSet">要填充的数据集</param>
/// <param name="tableNames">数据集中的表名</param>
/// <param name="commandParameters">命令的参数</param>
public static void FillDataSet(CommandType commandType,
string commandText, DataSet dataSet, params SqlParameter[] commandParameters)
{
if (connectionString == null || connectionString.Length == )
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == )
throw new ArgumentNullException("commandText");
if (dataSet == null)
throw new ArgumentNullException("dataSet"); using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open(); SqlCommand command = new SqlCommand();
PrepareCommand(command, connection, commandType, commandText, commandParameters); SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(dataSet);
connection.Close();
//connection.Dispose();
}
}
#endregion #region UpdateDataSet
/// <summary>
/// 更新数据集
/// </summary>
/// <param name="insertCommand">插入命令对象</param>
/// <param name="deleteCommand">删除命令对象</param>
/// <param name="updateCommand">更新命令对象</param>
/// <param name="dataSet">用作更新数据源的数据集</param>
/// <param name="tableName">用作更新数据源的数据表</param>
public static void UpdateDataSet(SqlCommand insertCommand, SqlCommand deleteCommand,
SqlCommand updateCommand, DataSet dataSet, string tableName)
{
if (insertCommand == null) throw new ArgumentNullException("insertCommand");
if (deleteCommand == null) throw new ArgumentNullException("deleteCommand");
if (updateCommand == null) throw new ArgumentNullException("updateCommand");
if (tableName == null || tableName.Length == ) throw new ArgumentNullException("tableName"); using (SqlDataAdapter dataAdapter = new SqlDataAdapter())
{
dataAdapter.UpdateCommand = updateCommand;
dataAdapter.InsertCommand = insertCommand;
dataAdapter.DeleteCommand = deleteCommand; dataAdapter.Update(dataSet, tableName); dataSet.AcceptChanges();
}
}
#endregion #region PrepareCommand
/// <summary>
/// 设置Command对象的属性
/// </summary>
/// <param name="command">命令对象</param>
/// <param name="connection">连接对象</param>
/// <param name="commandType">命令类型(存储过程、文本等等)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="commandParameters">命令的参数</param>
private static void PrepareCommand(SqlCommand command, SqlConnection connection, CommandType commandType, string commandText, SqlParameter[] commandParameters)
{
if (command == null)
throw new ArgumentNullException("command");
if (connection == null)
throw new ArgumentNullException("connection");
if (commandText == null || commandText.Length == )
throw new ArgumentNullException("commandText"); command.Connection = connection;
command.CommandType = commandType;
command.CommandText = commandText;
if (commandParameters != null)
foreach (SqlParameter para in commandParameters)
command.Parameters.Add(para);
}
#endregion #region ExeDelSqlTransaction
/// <summary>
/// 执行事务
/// </summary>
/// <param name="commandType"></param>
/// <param name="paramsWhere">条件</param>
/// <param name="sqlCommands">sql语句</param>
public static bool ExeSqlTransaction(CommandType commandType, string sqlCommands, params SqlParameter[] paramsWhere)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlTransaction st = conn.BeginTransaction();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = commandType;
cmd.Connection = conn;
cmd.Transaction = st;
try
{
cmd.CommandText = sqlCommands;
if (paramsWhere != null)
foreach (SqlParameter parameter in paramsWhere)
{
cmd.Parameters.Add(parameter);
}
cmd.ExecuteNonQuery();
st.Commit(); //提交事务
return true;
}
catch
{
st.Rollback(); //回滚事务
return false;
}
}
}
#endregion #region MD5加密
/// <summary>
/// MD5加密
/// </summary>
/// <param name="md5Str"></param>
/// <returns></returns>
public static string GetMD5(string md5Str)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(md5Str, "MD5");
}
#endregion
#endregion
}
}

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

  1. java工具类(二)之java正则表达式表单验证

    java正则表达式表单验证类工具类(验证邮箱.手机号码.qq号码等) 这篇文章主要介绍了java使用正则表达式进行表单验证工具类,可以验证邮箱.手机号码.qq号码等方法,需要的朋友可以参考下. jav ...

  2. Java学习笔记49(DBUtils工具类二)

    上一篇文章是我们自己模拟的DBUtils工具类,其实有开发好的工具类 这里使用commons-dbutils-1.6.jar 事务的简单介绍: 在数据库中应用事务处理案例:转账案例 张三和李四都有有自 ...

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

    未完待续 ........ java.time.*代替方案 1.Instant 代替 Date2.LocalDateTime 代替 Calendar3.DateTimeFormatter 代替 Sim ...

  4. ADO.NET工具类(三)

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

  5. ADO.NET工具类(一)

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

  6. 并发编程常用工具类(二) SymaPhore实现线程池

    1.symaPhore简介 symaphore(信号量)用来控制同时访问某个资源的线程数量,一般用在并发流量控制.个人对它的理解相当于是接待室每次只能接待固定数量的人,当达到最高接待数的时候,其他人就 ...

  7. 自定义的jdbc连接工具类JDBCUtils【java 工具类】

    JDBCUtils 类设计: 1. 创建私有的属性*(连接数据库必要的四个变量):dreiver url user password 2. 将构造函数私有化 3.将注册驱动写入静态代码块 4.外界只能 ...

  8. Collections集合工具类

    一.Collection与Collections Collection 是所有单列集合的根接口 Collection 是操作集合的工具类 二.Collections中常见的方法:(大都是static方 ...

  9. 图片压缩java工具类

    package com.net.util; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.Fil ...

随机推荐

  1. nginx + tomcat = http && https

    Tomcat版块配置: vim /to/path/conf/server.xml <Server port="" shutdown="SHUTDOWN"& ...

  2. 为什么C++11引入了std::ref?

    C++本身有引用(&),为什么C++11又引入了std::ref? 主要是考虑函数式编程(如std::bind)在使用时,是对参数直接拷贝,而不是引用.如下例子: #include <f ...

  3. router-link 绑定事件的方式

    实现方法如下  使用.native 实现原生事件 <router-link to="" @click.native="dialogWithdraw=true&quo ...

  4. 深入浅出经典面试题:从浏览器中输入URL到页面加载发生了什么 - Part 3

    备注: 因为文章太长,所以将它分为三部分,本文是第三部分. 第一部分:深入浅出经典面试题:从浏览器中输入URL到页面加载发生了什么 - Part 1 第二部分:深入浅出经典面试题:从浏览器中输入URL ...

  5. mybatis源码- 反射模块一(跟着MyBatis学反射):类级别信息的封装

    目录 1 JavaBean 规范 2 Reflector和ReflectorFactory 2.1 Reflector 属性 2.1.1 属性 2.1.2 Invoker 接口 2.2 Reflect ...

  6. LeetCode 961. N-Repeated Element in Size 2N Array

    In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeate ...

  7. RPM包制作过程(一)

    本机环境:centos7,64位 1. 首先安装工具,rpmbuild可能在rpmdevtools里已经包含 #yum install rpm-devel.x86_64 #yum install rp ...

  8. ICPC青岛站网络赛-C-高效模拟

    嗯这道辣鸡题,当时我队友写了错误的代码,我稍微改动了,思路基本上是对了,但是就是超时,我第一直觉是我这个算法思路是没有任何问题的,但是就是TLE,我感觉这个算法已经优化的不能再优化了啊...后面就怀疑 ...

  9. Division and Union CodeForces - 1101C (排序后处理)

    There are nn segments [li,ri][li,ri] for 1≤i≤n1≤i≤n. You should divide all segments into two non-emp ...

  10. 小小知识点(一)——利用电脑自带的BitLocker对磁盘加密

    1.利用电脑自带的BitLocker可以对固定的或移动的磁盘加密 网上有很多的使用方法步骤,可参考百度经验:https://jingyan.baidu.com/article/636f38bb4fac ...