一个很好用的SqlHelper类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.OracleClient;
using System.Diagnostics;
using System.Configuration;
using System.Collections; namespace ManagerMent.DbHelper
{
public class DbHelper
{
#region 数据库连接字段(web.config来配置)
public string ConnectString { get; set; }
public string ConnectStringSql { get; set; }
public string ConnectStringOrl { get; set; } public DbHelper()
{
ConnectString = ConfigurationManager.AppSettings["ConnectionString"];
ConnectStringSql = ConfigurationManager.AppSettings["ConnectionSql"];
ConnectStringOrl = ConfigurationManager.AppSettings["ConnectStringOrl"]; }
#endregion #region 创建Connection
private IDbConnection GetConnection(string DbType)
{
switch (DbType)
{
case "SqlServer":
return new SqlConnection(ConnectStringSql);
case "Oracle":
return new OracleConnection(ConnectStringOrl);
case "Access":
return new OleDbConnection(ConnectString);
default:
return new SqlConnection(ConnectStringSql);
}
}
#endregion #region 创建Command
private IDbCommand GetCommand(string DbType, string sql, IDbConnection iConn)
{
switch (DbType)
{
case "SqlServer":
return new SqlCommand(sql, (SqlConnection)iConn);
case "Oracle":
return new OracleCommand(sql, (OracleConnection)iConn);
case "Access":
return new OleDbCommand(sql, (OleDbConnection)iConn);
default:
return new SqlCommand(sql, (SqlConnection)iConn);
}
}
private IDbCommand GetCommand(string DbType)
{
switch (DbType)
{
case "SqlServer":
return new SqlCommand();
case "Oracle":
return new OracleCommand();
case "Access":
return new OleDbCommand();
default:
return new SqlCommand();
}
}
#endregion #region 创建Adapter
private IDataAdapter GetAdapter(string DbType, string sql, IDbConnection iConn)
{
switch (DbType)
{
case "SqlServer":
return new SqlDataAdapter(sql, (SqlConnection)iConn);
case "Oracle":
return new OracleDataAdapter(sql, (OracleConnection)iConn);
case "Access":
return new OleDbDataAdapter(sql, (OleDbConnection)iConn);
default:
return new SqlDataAdapter(sql, (SqlConnection)iConn);
}
}
private IDataAdapter GetAdapter(string DbType, IDbCommand iCmd)
{
switch (DbType)
{
case "SqlServer":
return new SqlDataAdapter((SqlCommand)iCmd);
case "Oracle":
return new OracleDataAdapter((OracleCommand)iCmd);
case "Access":
return new OleDbDataAdapter((OleDbCommand)iCmd);
default:
return new SqlDataAdapter((SqlCommand)iCmd);
}
}
#endregion #region 执行数据库表操作
public IDataReader ExecuteReader(string DbType, string SqlString)
{
IDbConnection iConn = GetConnection(DbType);
{
iConn.Open();
IDbCommand iCmd = GetCommand(DbType);
{
try
{
PrepareCommand(DbType, out iCmd, iConn, null, SqlString, null);
IDataReader iReader = iCmd.ExecuteReader();
return iReader;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
iCmd.Dispose(); }
}
} } private void PrepareCommand(string DbType, out IDbCommand iCmd, IDbConnection iConn, IDbTransaction iTrans, string cmdText, IDataParameter[] iParms)
{
if (iConn.State != ConnectionState.Open)
{
iConn.Open();
}
iCmd = GetCommand(DbType);
iCmd.Connection = iConn;
iCmd.CommandText = cmdText;
if (iTrans != null)
{
iCmd.Transaction = iTrans;
}
iCmd.CommandType = CommandType.Text; //cmdType
if (iParms != null)
{
foreach (IDataParameter parm in iParms)
{
iCmd.Parameters.Add(parm);
}
} } //执行查询语句,返回DataSet,重点用于Oracle数据库
public DataSet Query(string DbType, string SqlString)
{
using (IDbConnection iConn = GetConnection(DbType))
{
DataSet Ds = new DataSet();
iConn.Open();
try
{
IDataAdapter iAdapter = GetAdapter(DbType, SqlString, iConn);
iAdapter.Fill(Ds);
return Ds;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
} //执行查询语句,返回DataTable,重点用于Oracle数据库
public DataTable ExcuteQuery(string DbType, string SqlString)
{
using (IDbConnection iConn = GetConnection(DbType))
{
DataSet Ds = new DataSet();
iConn.Open();
try
{
IDataAdapter iAdapter = GetAdapter(DbType, SqlString, iConn);
iAdapter.Fill(Ds);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
return Ds.Tables[];
}
} //执行数据库表操作,重点用于SqlServer数据库
public void ExcuteSqlScalar(string DbType, string SqlString)
{
using (IDbConnection iConn = GetConnection(DbType))
{
using (IDbCommand iCmd = GetCommand(DbType, SqlString, iConn))
{
iConn.Open();
iCmd.ExecuteNonQuery();
}
}
} public int ExcuteSqlNum(string DbType, string SqlString)
{
using (IDbConnection iConn = GetConnection(DbType))
{
using (IDbCommand iCmd = GetCommand(DbType, SqlString, iConn))
{
iConn.Open();
int i = (int)iCmd.ExecuteScalar();
return i;
}
}
} public bool ExcuteSql(string DbType, string SqlString)
{
using (IDbConnection iConn = GetConnection(DbType))
{
using (IDbCommand iCmd = GetCommand(DbType, SqlString, iConn))
{
iConn.Open();
try
{
int Rows = (int)iCmd.ExecuteScalar();
return false;
}
catch (Exception ex)
{
return true;
}
finally
{
if (iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
} }
} //删除数据
public void DeleGet(string DbType, string SqlString)
{
using (IDbConnection iConn = GetConnection(DbType))
{
using (IDbCommand iCmd = GetCommand(DbType, SqlString, iConn))
{
iConn.Open();
iCmd.ExecuteNonQuery();
}
}
}
#endregion #region 执行多条SQL语句,实现数据库事物
/// <summary>
///
/// </summary>
/// <param name="DbType"></param>
/// <param name="SqlStringList">多条SQL语句</param>
/// <returns></returns>
public bool ExcuteSqlTran(string DbType, ArrayList SqlStringList)
{
using (IDbConnection iConn = GetConnection(DbType))
{
iConn.Open();
using (IDbCommand iCmd = GetCommand(DbType))
{
iCmd.Connection = iConn;
using (IDbTransaction iDbTran = iConn.BeginTransaction())
{
iCmd.Transaction = iDbTran;
try
{
for (int n = ; n < SqlStringList.Count; n++)
{
string StrSql = SqlStringList[n].ToString();
if (StrSql.Trim().Length > )
{
iCmd.CommandText = StrSql;
iCmd.ExecuteNonQuery();
}
}
iDbTran.Commit();
}
catch (Exception ex)
{
Debug.WriteLine("ExcuteSqlTran" + ex.Message);
iDbTran.Rollback();
return false;
}
finally
{
if (iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
return true;
}
} #endregion #region SQL数据库解析格式
private IDbDataParameter DbPara(string DbType, string ParaName, string DataType)
{
switch (DbType)
{
case "SqlServer":
return GetSqlPara(ParaName, DataType);
case "Oracle":
return GetOraclePara(ParaName, DbType);
case "Access":
return GetOledbPara(ParaName, DbType);
default:
return GetSqlPara(ParaName, DbType);
}
} private IDbDataParameter GetSqlPara(string ParaName, string DataType)
{
switch (DataType)
{
case "Decimal":
return new SqlParameter(ParaName, SqlDbType.Decimal);
case "VarChar":
return new SqlParameter(ParaName, SqlDbType.VarChar);
case "DateTime":
return new SqlParameter(ParaName, SqlDbType.DateTime);
case "Image":
return new SqlParameter(ParaName, SqlDbType.Image);
case "Int":
return new SqlParameter(ParaName, SqlDbType.Int);
case "Text":
return new SqlParameter(ParaName, SqlDbType.Text);
default:
return new SqlParameter(ParaName, SqlDbType.VarChar);
}
} //Oracle数据库解析数据格式
private IDbDataParameter GetOraclePara(string ParaName, string DbType)
{
switch (DbType)
{
case "Decimal":
return new OracleParameter(ParaName, SqlDbType.Decimal);
case "VarChar":
return new OracleParameter(ParaName, SqlDbType.VarChar);
case "DateTime":
return new OracleParameter(ParaName, SqlDbType.DateTime);
case "Image":
return new OracleParameter(ParaName, SqlDbType.Image);
case "Int":
return new OracleParameter(ParaName, SqlDbType.Int);
case "Text":
return new OracleParameter(ParaName, SqlDbType.Text);
default:
return new OracleParameter(ParaName, SqlDbType.VarChar);
}
} //Access(OLEDB)数据库数据解析格式
private IDbDataParameter GetOledbPara(string ParaName, string DbType)
{
switch (DbType)
{
case "Decimal":
return new OleDbParameter(ParaName, SqlDbType.Decimal);
case "VarChar":
return new OleDbParameter(ParaName, SqlDbType.VarChar);
case "DateTime":
return new OleDbParameter(ParaName, SqlDbType.DateTime);
case "Image":
return new OleDbParameter(ParaName, SqlDbType.Image);
case "Int":
return new OleDbParameter(ParaName, SqlDbType.Int);
case "Text":
return new OleDbParameter(ParaName, SqlDbType.Text);
default:
return new OleDbParameter(ParaName, SqlDbType.VarChar);
}
} #endregion #region 执行存储过程
public DataSet RunProcedure(string DbType, string StoreProcName, IDataParameter[] Parameters, DataSet dataSet, string TableName)
{
using (IDbConnection iConn = GetConnection(DbType))
{
iConn.Open();
IDataAdapter iDa = GetAdapter1(DbType, BuildQueryCommand(DbType, iConn, StoreProcName, Parameters));
((SqlDataAdapter)iDa).Fill(dataSet, TableName);
if(iConn.State!=ConnectionState.Closed)
{
iConn.Close();
}
return dataSet;
}
} private IDbCommand BuildQueryCommand(string dbType, IDbConnection iConn, string storeProcName, IDataParameter[] parameters)
{
IDbCommand iCmd = GetCommand(dbType,storeProcName,iConn);
iCmd.CommandType = CommandType.StoredProcedure;
if(parameters==null)
{
return iCmd;
}
foreach(IDataParameter parameter in parameters)
{
iCmd.Parameters.Add(parameter);
}
return iCmd;
} private IDataAdapter GetAdapter1(string DbType, IDbCommand iCmd)
{
switch(DbType)
{
case "SqlServer":
return new SqlDataAdapter((SqlCommand)iCmd);
case "Oracle":
return new OracleDataAdapter((OracleCommand)iCmd);
case "Oledb":
return new OleDbDataAdapter((OleDbCommand)iCmd);
default:
return new SqlDataAdapter((SqlCommand)iCmd);
}
} /// <summary>
/// 执行存储过程
/// </summary>
/// <param name="DbType"></param>
/// <param name="storeProcName"></param>
/// <param name="parameters"></param>
/// <param name="TableName"></param>
/// <returns></returns>
public DataSet RunProcedure(string DbType,string storeProcName,IDbDataParameter[] parameters,string TableName)
{
using (IDbConnection iConn=GetConnection(DbType))
{
DataSet Ds = new DataSet();
iConn.Open();
IDataAdapter iDa = GetAdapter(DbType,BuildQueryCommand(DbType,iConn,storeProcName,parameters));
((SqlDataAdapter)iDa).Fill(Ds,TableName);
if(iConn.State!=ConnectionState.Closed)
{
iConn.Close();
}
return Ds;
}
} public DataSet RunProcedure(string DbType,string storeProcName,IDbDataParameter[] parameters)
{
using (IDbConnection iConn=GetConnection(DbType))
{
DataSet Ds = new DataSet();
IDataAdapter iDa = GetAdapter(DbType,BuildQueryCommand(DbType,iConn,storeProcName,parameters));
((SqlDataAdapter)iDa).Fill(Ds);
if(iConn.State!=ConnectionState.Closed)
{
iConn.Close();
}
return Ds;
}
}
#endregion
}
}
一个很好用的SqlHelper类的更多相关文章
- 一个很棒的PHP缓存类,收藏下
<?php class Cache { /** 缓存目录 **/ var $CacheDir = './cache'; /** 缓存的文件 **/ var $CacheFile = ''; /* ...
- 整理出一个比较实用的SqlHelper类 满足大多数情况的使用
/// <summary> /// SqlHelper类 by zy 2016-3-11 /// </summary> public sealed class SqlHelpe ...
- c#中sqlhelper类的编写(二)
上一篇文章讲了简易版的SqlHelper类的编写,我们在这里就上一篇文章末尾提出的问题写出解决方案. sql语句注入攻击已经是众所周知的了.我们如何在C#中保护自己的数据库不被这样的方式攻击呢? 不用 ...
- JAVA WEB SQLHelper类的封装
在这次做项目中,我对自己最满意的就是封装了一下SQLHelper类,我对自己感到骄傲主要是 我是初学者,我刚开始不知道可以这样做,我只是想着试着去这样做了,结果真的可以,所以我 在我的模块就自己封装了 ...
- ADO.NET复习——自己编写SqlHelper类
今天复习了一次ADO.NET基础,整理一下自己的认为的重点: 编写SqlHelper类,方便我们执行数据库语句,这时可以直接调用封装在SqlHelper类的方法.现在大多数公司面试的时候,给你的面试题 ...
- 用CIL写程序:定义一个叫“慕容小匹夫”的类
前文回顾: <用CIL写程序:你好,沃尔德> <用CIL写程序:写个函数做加法> 前言: 今天是乙未羊年的第一天,小匹夫先在这里给各位看官拜个年了.不知道各位看官是否和匹夫一样 ...
- 最新的SqlHelper 类
最新的SqlHelper 类 摘自:http://www.cnblogs.com/sufei/archive/2010/01/14/1648026.html using System; using S ...
- c#中sqlhelper类的编写(一)
在.net平台的项目开发中,凡是用到数据库交互的,都有必要了解SqlHelper类的原理. 步骤一: 我就拿WPF项目开发作为例子.首先要新建一个App.config(应用程序配置文件).注意,在VS ...
- [置顶] 自己写sqlhelper类
自己写sqlhelper类 using System; using System.Collections.Generic; using System.Configuration; using Syst ...
随机推荐
- 【SPOJ10707】 COT2 Count on a tree II
SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...
- 网络流——最大流Dinic算法
前言 突然发现到了新的一年什么东西好像就都不会了凉凉 算法步骤 建残量网络图 在残量网络图上跑增广路 重复1直到没有增广路(注意一个残量网络图要尽量把价值都用完,不然会浪费建图的时间) 代码实现 #i ...
- 一些浩辰设置及它的bug?
gcad执行快捷键有问题?尝试修改Setvar("autocompletemode", "19");原因是’输入按键时显示建议列表’这个项打钩了,这里首先捕捉的 ...
- Akka(0):聊聊对Akka的初步了解和想法
前一段时间一直沉浸在函数式编程模式里,主要目的之一是掌握一套安全可靠的并发程序编程方法(concurrent programming),最终通过开源项目FunDA实现了单机多核CPU上程序的并行运算. ...
- linux上安装redis4.0.9
redis安装从3.0的版本到现在4.0的版本,现在装一个4.0的版本供大家学习使用. 先yum安装gcc yum -y install gcc 已加载插件:fastestmirror, langpa ...
- VSTO:C#获取文档控件的值
基础知识准备: VSTO入门 创建Excel解决方案 string[] inputfileNames = { @"C:\1.xls", @"C:\2.xls" ...
- [Spring]@Autowired,@Required,@Qualifier注解
@Required注解 @Required注解用于setter方法,表明这个属性是必要的,不可少的,必须注入值 假设有个测试类,里面有name和password两个属性 我给两个属性的setter方法 ...
- 【learning】 扩展lucas定理
首先说下啥是lucas定理: $\binom n m \equiv \binom {n\%P} {m\%P} \times \binom{n/P}{m/P} \pmod P$ 借助这个定理,求$\bi ...
- 使用webpack和react搭建项目
看了N多博客,日志,一边迷茫一边摸索.本文记录流程.我怕自己忘了...并且修复了博客园首页推荐那个日志中遇到的bug 1.webstorm新建一个空白项目,比如webpack_demo 2.因为要用r ...
- MethodImplOptions.Synchronized的一点讨论
Review代码发现有一个方法加了[MethodImpl(MethodImplOptions.Synchronized)] 属性,这个属性的目的,从名字上就可以看出,是要对所有线程进行同步执行. 对方 ...