using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data; namespace Common
{
public class DBHelper
{
private string strConn;
private SqlConnection conn;
public DBHelper(string ConnectionName)
{
strConn = GetConfigString(ConnectionName);
conn = new SqlConnection(strConn); }
public static string GetConfigString(string ConnectionName)
{
string strConfig = ConfigurationManager.AppSettings[ConnectionName];
return strConfig; }
public DataTable GetDataTable(string strSql)
{
DataTable dt = new DataTable();
try
{
System.Data.SqlClient.SqlDataAdapter dr = new SqlDataAdapter(strSql, conn);
conn.Open();
dr.Fill(dt);
return dt;
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
} }
public void ExecuteNonQuery(string strSql)
{
try
{
SqlCommand comd = new SqlCommand(strSql, conn);
conn.Open();
comd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
} }
public bool SqlBulkCopy(DataTable dtData, string strDataTableName)
{
try
{
using (SqlBulkCopy Tsbc = new SqlBulkCopy(strConn))
{
Tsbc.DestinationTableName = strDataTableName; Tsbc.WriteToServer(dtData);
}
return true; }
catch (Exception ex)
{
return false;
}
finally
{
}
} }
}

C#-DBHelper的更多相关文章

  1. 不该活着的SqlHelper和DBHelper

    前言: 还记得刚学ADO.NET的情景么? 还记得当年是怎么从ADO.NET被忽悠到用SqlHelper的么? 话说从入门到走上工作岗位那些年,我们就一直被纯纯地教导或引导,ADO.NET太原始,得封 ...

  2. 兼容SQLSERVER、Oracle、MYSQL、SQLITE的超级DBHelper

    本示例代码的关键是利用.net库自带的DbProviderFactory来生产数据库操作对象. 从下图中,可以看到其的多个核心方法,这些方法将在我们的超级DBHelper中使用. 仔细研究,你会发现每 ...

  3. C#/ASP.NET完善的DBHelper,配套Model生成器

    支持Oracle.MSSQL.MySQL.SQLite四种数据库,支持事务,支持对象关系映射:已在多个项目中实际使用. 没有语法糖,学习成本几乎为0,拿来即用. DBHelper类完整代码: usin ...

  4. C# DBHelper 第二版

    1. [代码][C#]代码     跳至 [1] [全屏预览] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

  5. 我也来写:数据库访问类DBHelper

    一.前言 相信许多人都百度过:“.net 数据库访问类”.然后就出来一大堆SqlHelper.我也用过这些SqlHelper,也自己写过,一堆静态方法,开始使用起来感觉很不错,它们也确实在很多时候可以 ...

  6. DbHelper为什么要用Using?

    我们分析一下DbHelper做什么事情,大家都知道它用于数据库的连接操作,这里的数据库连接会创建非托管资源,c#的垃圾回收机制不会对它处理,需要实现IDisposable接口手动释放.   手动释放的 ...

  7. NHibernate3快速上手教程FluentNHibernate配置与DBHelper

    很多学习NHibernate的新手很容易卡在配置文件这一关,正所谓万事开头难,上手后再配合官方文档就比较容易了. 网上关于配置文件的资料非常多,但由于版本的问题,许多老的教程中都没有明确指出类库的版本 ...

  8. 数据库助手类 DBHelper

    using System; using System.Collections.Generic; using System.Text; using System.Configuration; using ...

  9. 收集C#常用类:自己写的一个DBHelper类

    随着学的东西越来越多,一点点的完善吧! using System; using System.Collections.Generic; using System.Linq; using System. ...

  10. Ado.net中简单的DBHelper类(增删改查)

    private static string connString = "server=.;database=hotel;uid=aa;pwd=123";//最好从配置文件中取出 p ...

随机推荐

  1. 教孩子学编程 Python

    教孩子学编程   Python 目录 第1 章 Python 基础:认识环境 111 认识Python 312 用Python 编写程序 513 运行Python 程序 514 本章小结 615 编程 ...

  2. 报错:The specified datastore driver ("com.mysql.jdbc.Driver") was not found in the CLASSPATH. Please check your CLASSPATH specification, and the name of the driver.

    报错背景: CDH中集成hive插件,启动报错. 报错现象: [main]: Metastore Thrift Server threw an exception... javax.jdo.JDOFa ...

  3. 【翻译】Flink Table Api & SQL ——Streaming 概念

    本文翻译自官网:Streaming 概念  https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/streamin ...

  4. Vue cli4.0 代理配置

    proxy: { '/service': { target: 'http://192.168.40.243:3000/', //对应自己的接口 changeOrigin: true, ws: true ...

  5. Data - 【转】数据分析的道与术

    简要说明 本文来自网络流传的"百度内部培训PPT - 数据分析的道与术",版权属于"百度",如有冒犯,即刻删除. PDF下载 - 数据分析的道与术 什么是数据分 ...

  6. websockify文档

    一.官网地址 地址:https://github.com/novnc/websockify 二.开启代理 1.单台服务器 python /usr/local/websockify/websockify ...

  7. 【作业】Mental Rotation (模拟)

    题目链接:https://vjudge.net/contest/345791#problem/L [问题描述] Mental rotation is a difficult thing to mast ...

  8. Ubuntu中Qt Creator无法启动调试

    Ubuntu下安装Qt creator后无法启动调试,报错为Ptrace:Operation not permitted. 产生原因: 在Ubuntu 11.04("Natty Narwha ...

  9. MySQL面试 - 读写分离

    MySQL面试 - 读写分离 面试题 你们有没有做 MySQL 读写分离?如何实现 MySQL 的读写分离?MySQL 主从复制原理的是啥?如何解决 MySQL 主从同步的延时问题? 面试官心理分析 ...

  10. 稀疏数组(java实现)

    1.稀疏数组 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组. 稀疏数组的处理方法是: 1.1记录数组一共有几行几列,有多少个不同的值 1.2把具有不同值的元素的行列 ...