ADO.NET工具类(二)
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工具类(二)的更多相关文章
- java工具类(二)之java正则表达式表单验证
java正则表达式表单验证类工具类(验证邮箱.手机号码.qq号码等) 这篇文章主要介绍了java使用正则表达式进行表单验证工具类,可以验证邮箱.手机号码.qq号码等方法,需要的朋友可以参考下. jav ...
- Java学习笔记49(DBUtils工具类二)
上一篇文章是我们自己模拟的DBUtils工具类,其实有开发好的工具类 这里使用commons-dbutils-1.6.jar 事务的简单介绍: 在数据库中应用事务处理案例:转账案例 张三和李四都有有自 ...
- JDK1.8 LocalDate 使用方式;LocalDate 封装Util,LocalDate工具类(二)
未完待续 ........ java.time.*代替方案 1.Instant 代替 Date2.LocalDateTime 代替 Calendar3.DateTimeFormatter 代替 Sim ...
- ADO.NET工具类(三)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- ADO.NET工具类(一)
using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; usin ...
- 并发编程常用工具类(二) SymaPhore实现线程池
1.symaPhore简介 symaphore(信号量)用来控制同时访问某个资源的线程数量,一般用在并发流量控制.个人对它的理解相当于是接待室每次只能接待固定数量的人,当达到最高接待数的时候,其他人就 ...
- 自定义的jdbc连接工具类JDBCUtils【java 工具类】
JDBCUtils 类设计: 1. 创建私有的属性*(连接数据库必要的四个变量):dreiver url user password 2. 将构造函数私有化 3.将注册驱动写入静态代码块 4.外界只能 ...
- Collections集合工具类
一.Collection与Collections Collection 是所有单列集合的根接口 Collection 是操作集合的工具类 二.Collections中常见的方法:(大都是static方 ...
- 图片压缩java工具类
package com.net.util; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.Fil ...
随机推荐
- 域名打开没有加上“http://”,导致报错{"code":-32603,"message":"Cannot navigate to invalid URL"}
1.在robotframework中写用例 Open Browser 192.168.4.110:8880/jwzh Chrome 2.没有写http:// 3.导致报错 4.正确写法应该是 Op ...
- sendmail简单配置
yum -y install sendmail sendmail-cfservice sendmail startservice saslauthd statusif [ $? -ne 0 ];the ...
- macOS10.4后的刻盘新姿势
先sudo -s 输入密码 然后终端拖入createinstallmedia (在macOS Mojave.app显示包内容里面的resources里面的文件复制下来即可) 输入 --volume ...
- Spring Boot 之发送邮件
Spring Boot 之发送邮件 简介 API 配置 实战 引入依赖 配置邮件属性 Java 代码 完整示例 引申和引用 简介 Spring Boot 收发邮件最简便方式是通过 spring-boo ...
- 在Winform开发中使用FastReport创建报表
FastReport.Net是一款适用于Windows Forms, ASP.NET和MVC框架的功能齐全的报表分析解决方案.可用在Microsoft Visual Studio 2005到2015, ...
- 【愚人节快乐】拥抱Bootstrap,FineUI新版效果超炫!
鉴于 Bootstrap 备受欢迎,我们决定用 Bootstrap 完全替换掉 jQuery,得到了更加漂亮的界面,先睹为快: 注:网页背景图片来自百度网盘,版权归百度所有! 详情:http://fi ...
- [C#] LINQ之SelectMany
声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 一.第一种用法: public static IEnumerable<TResult> SelectMany<TSo ...
- Python股票分析系列——系列介绍和获取股票数据.p1
本系列转载自youtuber sentdex博主的教程视频内容 https://www.youtube.com/watch?v=19yyasfGLhk&index=4&list=PLQ ...
- 配置linux-Fedora系统下iptables防火墙
参考地址:https://blog.csdn.net/zhangjingyi111/article/details/78902820 本篇文章为实验课过程记录,较为简略. 1.查看系统是否安装ipta ...
- 05 Django REST Framework 分页
01-分页模式 rest framework中提供了三种分页模式: from rest_framework.pagination import PageNumberPagination, LimitO ...