DBHelper数据库操作类(二)
不错文章:http://www.codefans.net/articles/562.shtml
http://www.cnblogs.com/gaobing/p/3878342.html
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Reflection;
using System.Collections;
namespace DataHelp
{
#region ADO.NET 访问数据库辅助类 +SqlHelp
//Author:兵兵 +SqlHelp
public class SqlHelp
{
/// <summary>
/// DB连接字符串
/// </summary>
public static readonly string DB= ConfigurationManager.ConnectionStrings["DB"].ConnectionString; #region ExecuteDataReader +ExecuteDataReader(string cmdText, List<SqlParameter> parameters,string connString)
/// <summary>
/// ExecuteDataReader(执行有参存储过程)
/// </summary>
/// <param name="cmdText">存储过程名称</param>
/// <param name="parameters">参数列表</param>
/// <param name="connString">连接字符串</param>
/// <returns>SqlDataReader对象</returns>
public static SqlDataReader ExecuteDataReader(string cmdText, List<SqlParameter> parameters, string connString)
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand();
CommandBuilder(cmdText, cmd, conn, parameters);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return reader; }
#endregion #region ExecuteDataReader +ExecuteDataReader(string cmdText,string connString)
/// <summary>
/// ExecuteDataReader(执行无参存储过程)
/// </summary>
/// <param name="cmdText">存储过程</param>
/// <param name="connString">连接字符串</param>
/// <returns>SqlDataReader对象</returns>
public static SqlDataReader ExecuteDataReader(string cmdText, string connString)
{ SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand();
CommandBuilder(cmdText, cmd, conn);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return reader; }
#endregion #region ExecuteNonQuery +ExecuteNonQuery(string cmdText, List<SqlParameter> parameters, string connString)
/// <summary>
/// ExecuteNonQuery(执行有参存储过程)
/// </summary>
/// <param name="cmdText">存储过程名称</param>
/// <param name="parameters">参数列表</param>
/// <param name="connString">连接字符串</param>
/// <returns>数据库受影响的行数</returns>
public static int ExecuteNonQuery(string cmdText, List<SqlParameter> parameters, string connString)
{
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand();
CommandBuilder(cmdText, cmd, conn, parameters);
int result = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return result;
}
}
#endregion #region ExecuteNonQuery +ExecuteNonQuery(string cmdText, string connString)
/// <summary>
/// ExecuteNonQuery(执行无参存储过程)
/// </summary>
/// <param name="cmdText">存储过程名称</param>
/// <param name="connString">连接字符串</param>
/// <returns>数据库受影响的行数</returns>
public static int ExecuteNonQuery(string cmdText, string connString)
{ using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand();
CommandBuilder(cmdText, cmd, conn);
int result = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return result;
} }
#endregion #region ExecuteScalar +ExecuteScalar(string cmdText, List<SqlParameter> parameters, string connString)
/// <summary>
/// ExecuteScalar(执行有参存储过程)
/// </summary>
/// <param name="cmdText">存储过程名称</param>
/// <param name="parameters">参数列表</param>
/// <param name="connString">连接字符串</param>
/// <returns>object</returns>
public static object ExecuteScalar(string cmdText, List<SqlParameter> parameters, string connString)
{ using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand();
CommandBuilder(cmdText, cmd, conn, parameters);
object o = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return o;
} }
#endregion #region ExecuteScalar +ExecuteScalar(string cmdText, string connString)
/// <summary>
/// ExecuteScalar(执行无参存储过程)
/// </summary>
/// <param name="cmdText">存储过程名称</param>
/// <param name="connString">连接字符串</param>
/// <returns>object</returns>
public static object ExecuteScalar(string cmdText, string connString)
{ using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand();
CommandBuilder(cmdText, cmd, conn);
object o = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return o;
} }
#endregion #region ExecuteDataTable +ExecuteDataTable(string cmdText, List<SqlParameter> parameters, string connString)
/// <summary>
/// ExecuteDataTable(用适配器执行有参存储过程)
/// </summary>
/// <param name="cmdText">存储过程名称</param>
/// <param name="parameters">参数列表</param>
/// <param name="connString">连接字符串</param>
/// <returns>DataTable</returns>
public static DataTable ExecuteDataTable(string cmdText, List<SqlParameter> parameters, string connString)
{ using (SqlConnection conn = new SqlConnection(connString))
{
SqlDataAdapter da = new SqlDataAdapter(cmdText, conn);
//命令类型为存储过程
da.DeleteCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.AddRange(parameters.ToArray());
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
} }
#endregion #region ExecuteDataTable +ExecuteDataTable(string cmdText, string connString)
/// <summary>
/// ExecuteDataTable(用适配器执行无参存储过程)
/// </summary>
/// <param name="cmdText">存储过程名称</param>
/// <param name="connString">连接字符串</param>
/// <returns>DataTable</returns>
public static DataTable ExecuteDataTable(string cmdText, string connString)
{ using (SqlConnection conn = new SqlConnection(connString))
{
SqlDataAdapter da = new SqlDataAdapter(cmdText, conn);
//命令类型为存储过程
da.DeleteCommand.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
#endregion #region ExecuteDataTableProc(命令+适配器) +ExecuteDataTableProc(string cmdText, List<SqlParameter> parameters, string connString)
/// <summary>
/// ExecuteDataTableProc(执行有参存储过程)
/// </summary>
/// <param name="cmdText">存储过程名称</param>
/// <param name="parameters">参数列表</param>
/// <param name="connString">连接字符串</param>
/// <returns>DataTable</returns>
public static DataTable ExecuteDataTableProc(string cmdText, List<SqlParameter> parameters, string connString)
/// <summary>
{ using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand();
CommandBuilder(cmdText, cmd, conn, parameters);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Parameters.Clear();
return dt; } }
#endregion #region ExecuteDataTableProc(命令+适配器) +ExecuteDataTableProc(string cmdText, string connString)
/// <summary>
/// ExecuteDataTableProc(执行无参存储过程)
/// </summary>
/// <param name="parameters">参数列表</param>
/// <param name="connString">连接字符串</param>
/// <returns>DataTable</returns>
public static DataTable ExecuteDataTableProc(string cmdText, string connString)
{ using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand();
CommandBuilder(cmdText, cmd, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Parameters.Clear();
return dt; } }
#endregion #region 准备命令对象 -CommandBuilder(string cmdText, SqlCommand cmd, SqlConnection conn, List<SqlParameter> parameters)
/// <summary>
/// 准备命令对象(执行有参存储过程)
/// </summary>
/// <param name="cmdText">存储过程名称</param>
/// <param name="cmd">命令对象</param>
/// <param name="conn">连接对象</param>
/// <param name="parameters">参数列表</param>
private static void CommandBuilder(string cmdText, SqlCommand cmd, SqlConnection conn, List<SqlParameter> parameters)
{ if (conn.State == System.Data.ConnectionState.Closed)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.CommandType = System.Data.CommandType.StoredProcedure;
if (parameters.Count > )
cmd.Parameters.AddRange(parameters.ToArray()); }
#endregion #region 准备命令对象 -CommandBuilder(string cmdText, SqlCommand cmd, SqlConnection conn)
/// <summary>
/// 准备命令对象(执行无参存储过程)
/// </summary>
/// <param name="cmdText">存储过程名称</param>
/// <param name="cmd">命令对象</param>
/// <param name="conn">连接对象</param>
private static void CommandBuilder(string cmdText, SqlCommand cmd, SqlConnection conn)
{ if (conn.State == System.Data.ConnectionState.Closed)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.CommandType = System.Data.CommandType.StoredProcedure; }
#endregion #region 批插入 void BulkInsert( DataTable dt, string tableName, string connStr)
/// <summary>
/// 批插入 void BulkInsert( DataTable dt, string tableName, string connStr)
/// </summary>
/// <param name="dt">所有数据的表格</param>
/// <param name="tableName">表名</param>
public static int BulkInsert(DataTable dt, string tableName, string connStr)
{
int result = -;
if (string.IsNullOrEmpty(tableName))
throw new Exception("请指定你要插入的表名");
var count = dt.Rows.Count;
if (count == )
return result;
SqlTransaction sqlBulkTran = null;
try
{
using (SqlConnection conn = new SqlConnection(connStr))
{
if (conn.State == System.Data.ConnectionState.Closed)
conn.Open();
sqlBulkTran = conn.BeginTransaction();
using (SqlBulkCopy copy = new SqlBulkCopy(conn, SqlBulkCopyOptions.CheckConstraints, sqlBulkTran))
{
copy.DestinationTableName = tableName;//指定目标表
copy.WriteToServer(dt);//将dt中的所有行复制到SqlBulkCopy对象的DestinationTableName指定的目标表中
if (sqlBulkTran != null)
{
sqlBulkTran.Commit();
}
result = ;
} } }
catch (Exception)
{
if (sqlBulkTran != null)
{
sqlBulkTran.Rollback();
}
}
finally
{
sqlBulkTran = null;
} return result;
}
#endregion }
#endregion
}
#region list 扩展方法 Author:高兵兵
public static class IListUtil
{
/// <summary>
/// 将集合类转换成DataTable
/// </summary>
/// <param name="list">集合</param>
/// <returns></returns>
public static DataTable AsDataTable<T>(this IList<T> list)
{
DataTable result = new DataTable();
if (list.Count > )
{
PropertyInfo[] propertys = typeof(T).GetProperties();
foreach (PropertyInfo pi in propertys)
{
result.Columns.Add(pi.Name, pi.PropertyType);
} for (int i = ; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (var item in propertys)
{
object obj = item.GetValue(list[i], null);
tempList.Add(obj);
} object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
} }
#endregion
DBHelper数据库操作类(二)的更多相关文章
- Android打造属于自己的数据库操作类。
1.概述 开发Android的同学都知道sdk已经为我们提供了一个SQLiteOpenHelper类来创建和管理SQLite数据库,通过写一个子类去继承它,就可以方便的创建.管理数据库.但是当我们需要 ...
- PHP 数据库操作类:ezSQL
EZSQL类介绍: 下载地址:http://www.jb51.net/codes/26393.htmlezsql是一个小型的快速的数据库操作类,可以让你很容易地用PHP操作各种数据库( MySQL.o ...
- 我的DbHelper数据操作类
其实,微软的企业库中有一个非常不错的数据操作类了.但是,不少公司(起码我遇到的几个...),对一些"封装"了些什么的东西不太敢用,虽然我推荐过微软的企业库框架了...但是还是要&q ...
- php : mysql数据库操作类演示
设计目标: 1,该类一实例化,就可以自动连接上mysql数据库: 2,该类可以单独去设定要使用的连接编码(set names XXX) 3,该类可以单独去设定要使用的数据库(use XXX): 4,可 ...
- DbHelper数据操作类
摘要:本文介绍一下DbHelper数据操作类 微软的企业库中有一个非常不错的数据操作类.但是,不少公司(起码我遇到的几个...),对一些"封装"了些什么的东西不太敢用,虽然我推荐过 ...
- 我的DbHelper数据操作类(转)
其实,微软的企业库中有一个非常不错的数据操作类了.但是,不少公司(起码我遇到的几个...),对一些"封装"了些什么的东西不太敢用,虽然我推荐过微软的企业库框架了...但是还是要&q ...
- PDO数据库操作类
<?php include 'common_config.php'; /** * Class Mysql * PDO数据库操作类 */ class Mysql { protected stati ...
- MySQL数据库操作类(PHP实现,支持连贯操作)
<?php /** * Author: suvan * CreateTime: 2018/2/27 * description: 数据库操作类(仅对接MySQL数据库,主要利用MySQLi函数) ...
- C#全能数据库操作类及调用示例
C#全能数据库操作类及调用示例 using System; using System.Data; using System.Data.Common; using System.Configuratio ...
随机推荐
- 【HDOJ】4345 Permutation
即求P1^n1+P2^n2 + ... + Pk^nk <= n,其中Pk为素数的所有可能组合.思路是DP.1~1000的素数就不到200个.dp[i][j]表示上式和不超过且当前最小素数为P[ ...
- [ffmpeg 扩展第三方库编译系列] 关于需要用到cmake 创建 mingw32编译环境问题
我在这里给出我编译的例子 cmake -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=& ...
- Where is Vasya?
Where is Vasya? Vasya stands in line with number of people p (including Vasya), but he doesn't know ...
- Shell中判断字符串是否为数字的6种方法分享
#!/bin/bash ## 方法1 a=1234;echo "$a"|[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ] &&a ...
- poj3308Paratroopers(dinic)
http://poj.org/problem?id=3308 给两个定义 最小割:对于图中的两个点(一般为源点和汇点)来说,如果把图中的一些边去掉,如果它们之间无法连通的话,则这些边组成的集合就叫为割 ...
- OpenSSH 'child_set_env()'函数安全绕过漏洞
漏洞版本: OpenSSH 6.x 漏洞描述: Bugtraq ID:66355 CVE ID:CVE-2014-2532 OpenSSH是一种开放源码的SSH协议的实现. OpenSSH " ...
- textview的上下滑动效果
1.xml文件中 <TextView … android:scrollbars="vertical" ../> 2.java文件中 textview.se ...
- Mac: the original hosts
## # Host Database # # localhost is used to configure the loopback interface # when the system is bo ...
- android应用分析之apk文件结构
实际上,一个APK文件就是一个.zip格式的压缩包,我们可以用解压缩工具打开任何一个APK文件,由于代码混淆和加密,通过普通解压缩工具打开里面的文件或目录会看到各种乱码.一个典型的ap ...
- java泛型小总结
一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: public class GenericTest { public static void main(String[] a ...