C#.NET万能数据库访问封装类(ACCESS、SQLServer、Oracle)
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.OracleClient;
using System.Configuration;
using System.Reflection;
using BloodWeb.Common;
namespace BloodWeb.DAL
{
/**//// <summary>
/// All rights reserved
/// 数据访问基础类
/// 用户可以修改满足自己项目的需要。
/// </summary>
public class DataBaseLayer
{
//数据库连接字符串(web.config来配置)
//<add key="ConnectionString" value="server=127.0.0.1;database=DATABASE;uid=sa;pwd=" />
private string connectionString;
public string ConntionString
{
get
{
return connectionString ;
}
set
{
connectionString = value;
}
}
public DataBaseLayer(string strConnect,string dataType)
{
this.ConntionString = strConnect;
this.DbType = dataType;
}
public DataBaseLayer()
{
this.connectionString =ConfigurationBase.ConnectionString;// ConfigurationSettings.AppSettings["ConnectionString"] ;
this.dbType =ConfigurationBase.DataType;// ConfigurationSettings.AppSettings["DataType"] ;
//this.connectionString = "data source=192.168.1.43;user id=sa;pwd=sa;database=temphrdb";
//this.dbType = "SqlServer";
}
/**//// <summary>
/// 数据库类型
/// </summary>
private string dbType;
public string DbType
{
get
{
if ( dbType == string.Empty || dbType == null )
{
return "Access";
}
else
{
return dbType;
}
}
set
{
if ( value != string.Empty && value != null )
{
dbType = value;
}
if (dbType ==string.Empty || dbType == null)
{
dbType = ConfigurationBase.DataType; //ConfigurationSettings.AppSettings["DataType"];
}
if ( dbType == string.Empty || dbType == null )
{
dbType = "Access";
}
}
}
#region 转换参数
private System.Data.IDbDataParameter iDbPara(string ParaName,string DataType)
{
switch(this.DbType)
{
case "SqlServer":
return GetSqlPara(ParaName,DataType);
case "Oracle":
return GetOleDbPara(ParaName,DataType);
case "Access":
return GetOleDbPara(ParaName,DataType);
default :
return GetSqlPara(ParaName,DataType);
}
}
private System.Data.SqlClient.SqlParameter GetSqlPara( string ParaName , string DataType)
{
switch(DataType)
{
case "Decimal":
return new System.Data.SqlClient.SqlParameter ( ParaName, System.Data.SqlDbType.Decimal );
case "Varchar":
return new System.Data.SqlClient.SqlParameter ( ParaName, System.Data.SqlDbType.VarChar );
case "DateTime":
return new System.Data.SqlClient.SqlParameter ( ParaName, System.Data.SqlDbType.DateTime );
case "Iamge":
return new System.Data.SqlClient.SqlParameter ( ParaName, System.Data.SqlDbType.Image );
case "Int":
return new System.Data.SqlClient.SqlParameter ( ParaName, System.Data.SqlDbType.Int );
case "Text":
return new System.Data.SqlClient.SqlParameter ( ParaName, System.Data.SqlDbType.NText );
default :
return new System.Data.SqlClient.SqlParameter ( ParaName, System.Data.SqlDbType.VarChar );
}
}
private System.Data.OracleClient.OracleParameter GetOraclePara( string ParaName , string DataType)
{
switch(DataType)
{
case "Decimal":
return new System.Data.OracleClient.OracleParameter( ParaName, System.Data.OracleClient.OracleType.Double);
case "Varchar":
return new System.Data.OracleClient.OracleParameter ( ParaName, System.Data.OracleClient.OracleType.VarChar );
case "DateTime":
return new System.Data.OracleClient.OracleParameter ( ParaName, System.Data.OracleClient.OracleType.DateTime );
case "Iamge":
return new System.Data.OracleClient.OracleParameter ( ParaName, System.Data.OracleClient.OracleType.BFile );
case "Int":
return new System.Data.OracleClient.OracleParameter ( ParaName, System.Data.OracleClient.OracleType.Int32 );
case "Text":
return new System.Data.OracleClient.OracleParameter ( ParaName, System.Data.OracleClient.OracleType.LongVarChar );
default:
return new System.Data.OracleClient.OracleParameter ( ParaName, System.Data.OracleClient.OracleType.VarChar );
}
}
private System.Data.OleDb.OleDbParameter GetOleDbPara( string ParaName , string DataType)
{
switch(DataType)
{
case "Decimal":
return new System.Data.OleDb.OleDbParameter( ParaName, System.Data.DbType.Decimal);
case "Varchar":
return new System.Data.OleDb.OleDbParameter ( ParaName, System.Data.DbType.String );
case "DateTime":
return new System.Data.OleDb.OleDbParameter ( ParaName, System.Data.DbType.DateTime );
case "Iamge":
return new System.Data.OleDb.OleDbParameter( ParaName, System.Data.DbType.Binary );
case "Int":
return new System.Data.OleDb.OleDbParameter ( ParaName, System.Data.DbType.Int32 );
case "Text":
return new System.Data.OleDb.OleDbParameter ( ParaName, System.Data.DbType.String );
default:
return new System.Data.OleDb.OleDbParameter ( ParaName, System.Data.DbType.String );
}
}
#endregion
#region 创建 Connection 和 Command
private IDbConnection GetConnection()
{
switch(this.DbType)
{
case "SqlServer":
return new System.Data.SqlClient.SqlConnection(this.ConntionString);
case "Oracle":
return new System.Data.OracleClient.OracleConnection(this.ConntionString);
case "Access":
return new System.Data.OleDb.OleDbConnection(this.ConntionString);
default:
return new System.Data.SqlClient.SqlConnection(this.ConntionString);
}
}
private IDbCommand GetCommand(string Sql,IDbConnection iConn)
{
switch(this.DbType)
{
case "SqlServer":
return new System.Data.SqlClient.SqlCommand(Sql,(SqlConnection)iConn);
case "Oracle":
return new System.Data.OracleClient.OracleCommand(Sql,(OracleConnection)iConn);
case "Access":
return new System.Data.OleDb.OleDbCommand(Sql,(OleDbConnection)iConn);
default:
return new System.Data.SqlClient.SqlCommand(Sql,(SqlConnection)iConn);
}
}
private IDbCommand GetCommand()
{
switch(this.DbType)
{
case "SqlServer":
return new System.Data.SqlClient.SqlCommand();
case "Oracle":
return new System.Data.OracleClient.OracleCommand();
case "Access":
return new System.Data.OleDb.OleDbCommand();
default:
return new System.Data.SqlClient.SqlCommand();
}
}
private IDataAdapter GetAdapater(string Sql,IDbConnection iConn)
{
switch(this.DbType)
{
case "SqlServer":
return new System.Data.SqlClient.SqlDataAdapter(Sql,(SqlConnection)iConn);
case "Oracle":
return new System.Data.OracleClient.OracleDataAdapter(Sql,(OracleConnection)iConn);
case "Access":
return new System.Data.OleDb.OleDbDataAdapter(Sql,(OleDbConnection)iConn);
default:
return new System.Data.SqlClient.SqlDataAdapter(Sql,(SqlConnection)iConn);;
}
}
private IDataAdapter GetAdapater()
{
switch(this.DbType)
{
case "SqlServer":
return new System.Data.SqlClient.SqlDataAdapter();
case "Oracle":
return new System.Data.OracleClient.OracleDataAdapter();
case "Access":
return new System.Data.OleDb.OleDbDataAdapter();
default:
return new System.Data.SqlClient.SqlDataAdapter();
}
}
private IDataAdapter GetAdapater(IDbCommand iCmd)
{
switch(this.DbType)
{
case "SqlServer":
return new System.Data.SqlClient.SqlDataAdapter((SqlCommand)iCmd);
case "Oracle":
return new System.Data.OracleClient.OracleDataAdapter((OracleCommand)iCmd);
case "Access":
return new System.Data.OleDb.OleDbDataAdapter((OleDbCommand)iCmd);
default:
return new System.Data.SqlClient.SqlDataAdapter((SqlCommand)iCmd);
}
}
#endregion
#region 执行简单SQL语句
/**//// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public int ExecuteSql(string SqlString)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
using (System.Data.IDbCommand iCmd = GetCommand(SqlString,iConn))
{
iConn.Open();
try
{
int rows=iCmd.ExecuteNonQuery();
return rows;
}
catch(System.Exception E)
{
throw new Exception(E.Message);
}
finally
{
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
}
/**//// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">多条SQL语句</param>
public void ExecuteSqlTran(ArrayList SQLStringList)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
iConn.Open();
using(System.Data.IDbCommand iCmd = GetCommand())
{
iCmd.Connection=iConn;
using(System.Data.IDbTransaction iDbTran = iConn.BeginTransaction())
{
iCmd.Transaction=iDbTran;
try
{
for(int n=0;n<SQLStringList.Count;n++)
{
string strsql = SQLStringList[n].ToString();
if ( strsql.Trim().Length>1)
{
iCmd.CommandText = strsql;
iCmd.ExecuteNonQuery();
}
}
iDbTran.Commit();
}
catch(System.Exception E)
{
iDbTran.Rollback();
throw new Exception(E.Message);
}
finally
{
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
}
}
/**//// <summary>
/// 执行带一个存储过程参数的的SQL语句。
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
/// <returns>影响的记录数</returns>
public int ExecuteSql(string SqlString,string content)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
using(System.Data.IDbCommand iCmd = GetCommand(SqlString,iConn))
{
System.Data.IDataParameter myParameter = this.iDbPara( "@content", "Text");
myParameter.Value = content ;
iCmd.Parameters.Add(myParameter);
iConn.Open();
try
{
int rows = iCmd.ExecuteNonQuery();
return rows;
}
catch( System.Exception e )
{
throw new Exception(e.Message);
}
finally
{
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
}
/**//// <summary>
/// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
/// </summary>
/// <param name="strSQL">SQL语句</param>
/// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
/// <returns>影响的记录数</returns>
public int ExecuteSqlInsertImg(string SqlString,byte[] fs)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
using(System.Data.IDbCommand iCmd = GetCommand(SqlString,iConn))
{
System.Data.IDataParameter myParameter = this.iDbPara( "@content", "Image");
myParameter.Value = fs ;
iCmd.Parameters.Add(myParameter);
iConn.Open();
try
{
int rows = iCmd.ExecuteNonQuery();
return rows;
}
catch( System.Exception e )
{
throw new Exception(e.Message);
}
finally
{
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
}
/**//// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object)。
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <returns>查询结果(object)</returns>
public object GetSingle(string SqlString)
{
using (System.Data.IDbConnection iConn = GetConnection())
{
using (System.Data.IDbCommand iCmd = GetCommand(SqlString,iConn))
{
iConn.Open();
try
{
object obj = iCmd.ExecuteScalar();
if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch(System.Exception e)
{
throw new Exception(e.Message);
}
finally
{
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
}
/**//// <summary>
/// 执行查询语句,返回IDataAdapter
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns>IDataAdapter</returns>
public IDataAdapter ExecuteReader(string strSQL)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
iConn.Open();
try
{
System.Data.IDataAdapter iAdapter = this.GetAdapater(strSQL,iConn);
return iAdapter;
}
catch(System.Exception e)
{
throw new Exception(e.Message);
}
finally
{
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
/**//// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public DataSet Query(string sqlString)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
using(System.Data.IDbCommand iCmd = GetCommand(sqlString,iConn))
{
DataSet ds = new DataSet();
iConn.Open();
try
{
System.Data.IDataAdapter iAdapter = this.GetAdapater(sqlString,iConn);
iAdapter.Fill(ds);
return ds;
}
catch(System.Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
}
/**//// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="sqlString">查询语句</param>
/// <param name="dataSet">要填充的DataSet</param>
/// <param name="tableName">要填充的表名</param>
/// <returns>DataSet</returns>
public DataSet Query(string sqlString,DataSet dataSet,string tableName)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
using(System.Data.IDbCommand iCmd = GetCommand(sqlString,iConn))
{
iConn.Open();
try
{
System.Data.IDataAdapter iAdapter = this.GetAdapater(sqlString,iConn);
((OleDbDataAdapter)iAdapter).Fill(dataSet,tableName);
return dataSet;
}
catch(System.Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
}
/**//// <summary>
/// 执行SQL语句 返回存储过程
/// </summary>
/// <param name="sqlString">Sql语句</param>
/// <param name="dataSet">要填充的DataSet</param>
/// <param name="startIndex">开始记录</param>
/// <param name="pageSize">页面记录大小</param>
/// <param name="tableName">表名称</param>
/// <returns>DataSet</returns>
public DataSet Query(string sqlString , DataSet dataSet ,int startIndex ,int pageSize, string tableName )
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
iConn.Open();
try
{
System.Data.IDataAdapter iAdapter = this.GetAdapater(sqlString,iConn);
((OleDbDataAdapter)iAdapter).Fill(dataSet,startIndex,pageSize,tableName);
return dataSet;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
/**//// <summary>
/// 执行查询语句,向XML文件写入数据
/// </summary>
/// <param name="sqlString">查询语句</param>
/// <param name="xmlPath">XML文件路径</param>
public void WriteToXml(string sqlString,string xmlPath)
{
Query(sqlString).WriteXml(xmlPath);
}
/**//// <summary>
/// 执行查询语句
/// </summary>
/// <param name="SqlString">查询语句</param>
/// <returns>DataTable </returns>
public DataTable ExecuteQuery(string sqlString)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
//System.Data.IDbCommand iCmd = GetCommand(sqlString,iConn);
DataSet ds = new DataSet();
try
{
System.Data.IDataAdapter iAdapter = this.GetAdapater(sqlString,iConn);
iAdapter.Fill(ds);
}
catch(System.Exception e)
{
throw new Exception(e.Message);
}
finally
{
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
return ds.Tables[0];
}
}
/**//// <summary>
/// 执行查询语句
/// </summary>
/// <param name="SqlString">查询语句</param>
/// <returns>DataTable </returns>
public DataTable ExecuteQuery(string SqlString,string Proc)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
using(System.Data.IDbCommand iCmd = GetCommand(SqlString,iConn))
{
iCmd.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
try
{
System.Data.IDataAdapter iDataAdapter = this.GetAdapater(SqlString,iConn);
iDataAdapter.Fill(ds);
}
catch(System.Exception e)
{
throw new Exception(e.Message);
}
finally
{
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
return ds.Tables[0];
}
}
}
/**//// <summary>
///
/// </summary>
/// <param name="Sql"></param>
/// <returns></returns>
public DataView ExeceuteDataView(string Sql)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
using(System.Data.IDbCommand iCmd = GetCommand(Sql,iConn))
{
DataSet ds = new DataSet();
try
{
System.Data.IDataAdapter iDataAdapter = this.GetAdapater(Sql,iConn);
iDataAdapter.Fill(ds);
return ds.Tables[0].DefaultView;
}
catch(System.Exception e)
{
throw new Exception(e.Message);
}
finally
{
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
}
#endregion
#region 执行带参数的SQL语句
/**//// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public int ExecuteSql(string SQLString,params IDataParameter[] iParms)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
System.Data.IDbCommand iCmd = GetCommand();
{
try
{
PrepareCommand(out iCmd, iConn, null , SQLString, iParms );
int rows=iCmd.ExecuteNonQuery();
iCmd.Parameters.Clear();
return rows;
}
catch(System.Exception E)
{
throw new Exception( E.Message );
}
finally
{
iCmd.Dispose();
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
}
/**//// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
public void ExecuteSqlTran(Hashtable SQLStringList)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
iConn.Open();
using (IDbTransaction iTrans = iConn.BeginTransaction())
{
System.Data.IDbCommand iCmd = GetCommand();
try
{
//循环
foreach ( DictionaryEntry myDE in SQLStringList)
{
string cmdText = myDE.Key.ToString();
IDataParameter[] iParms=( IDataParameter[] ) myDE.Value;
PrepareCommand( out iCmd , iConn , iTrans , cmdText , iParms );
int val = iCmd.ExecuteNonQuery();
iCmd.Parameters.Clear();
}
iTrans.Commit();
}
catch
{
iTrans.Rollback();
throw;
}
finally
{
iCmd.Dispose();
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
}
/**//// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object)。
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <returns>查询结果(object)</returns>
public object GetSingle(string SQLString,params IDataParameter[] iParms)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
System.Data.IDbCommand iCmd = GetCommand();
{
try
{
PrepareCommand( out iCmd, iConn, null , SQLString, iParms );
object obj = iCmd.ExecuteScalar();
iCmd.Parameters.Clear();
if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch(System.Exception e)
{
throw new Exception(e.Message);
}
finally
{
iCmd.Dispose();
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
}
/**//// <summary>
/// 执行查询语句,返回IDataReader
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns> IDataReader </returns>
public IDataReader ExecuteReader(string SQLString,params IDataParameter[] iParms)
{
System.Data.IDbConnection iConn = this.GetConnection();
{
System.Data.IDbCommand iCmd = GetCommand();
{
try
{
PrepareCommand(out iCmd, iConn , null , SQLString , iParms);
System.Data.IDataReader iReader = iCmd.ExecuteReader();
iCmd.Parameters.Clear();
return iReader;
}
catch(System.Exception e)
{
throw new Exception(e.Message);
}
finally
{
iCmd.Dispose();
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
}
/**//// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public DataSet Query(string sqlString,params IDataParameter[] iParms)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
IDbCommand iCmd = GetCommand();
{
PrepareCommand(out iCmd , iConn , null , sqlString , iParms );
try
{
IDataAdapter iAdapter = this.GetAdapater(sqlString,iConn);
DataSet ds = new DataSet();
iAdapter.Fill(ds);
iCmd.Parameters.Clear();
return ds;
}
catch(System.Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
iCmd.Dispose();
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
}
/**//// <summary>
/// 初始化Command
/// </summary>
/// <param name="iCmd"></param>
/// <param name="iConn"></param>
/// <param name="iTrans"></param>
/// <param name="cmdText"></param>
/// <param name="iParms"></param>
private void PrepareCommand(out IDbCommand iCmd,IDbConnection iConn,System.Data.IDbTransaction iTrans, string cmdText, IDataParameter[] iParms)
{
if (iConn.State != ConnectionState.Open)
iConn.Open();
iCmd = this.GetCommand();
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);
}
}
#endregion
#region 存储过程操作
/**//// <summary>
/// 执行存储过程
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlDataReader</returns>
public SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters )
{
System.Data.IDbConnection iConn = this.GetConnection();
{
iConn.Open();
using(SqlCommand sqlCmd = BuildQueryCommand(iConn,storedProcName, parameters))
{
return sqlCmd.ExecuteReader(CommandBehavior.CloseConnection);
}
}
}
/**//// <summary>
/// 执行存储过程
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <param name="tableName">DataSet结果中的表名</param>
/// <returns>DataSet</returns>
public DataSet RunProcedure(string storedProcName, IDataParameter[] parameters ,string tableName)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
DataSet dataSet = new DataSet();
iConn.Open();
System.Data.IDataAdapter iDA = this.GetAdapater();
iDA = this.GetAdapater( BuildQueryCommand(iConn, storedProcName, parameters ) );
((SqlDataAdapter)iDA).Fill( dataSet,tableName);
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
return dataSet;
}
}
/**//// <summary>
/// 执行存储过程
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <param name="tableName">DataSet结果中的表名</param>
/// <param name="startIndex">开始记录索引</param>
/// <param name="pageSize">页面记录大小</param>
/// <returns>DataSet</returns>
public DataSet RunProcedure(string storedProcName, IDataParameter[] parameters ,int startIndex,int pageSize,string tableName)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
DataSet dataSet = new DataSet();
iConn.Open();
System.Data.IDataAdapter iDA = this.GetAdapater();
iDA = this.GetAdapater( BuildQueryCommand(iConn, storedProcName, parameters ) );
((SqlDataAdapter)iDA).Fill( dataSet,startIndex,pageSize,tableName);
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
return dataSet;
}
}
/**//// <summary>
/// 执行存储过程 填充已经存在的DataSet数据集
/// </summary>
/// <param name="storeProcName">存储过程名称</param>
/// <param name="parameters">存储过程参数</param>
/// <param name="dataSet">要填充的数据集</param>
/// <param name="tablename">要填充的表名</param>
/// <returns></returns>
public DataSet RunProcedure(string storeProcName,IDataParameter[] parameters,DataSet dataSet,string tableName)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
iConn.Open();
System.Data.IDataAdapter iDA = this.GetAdapater();
iDA = this.GetAdapater(BuildQueryCommand(iConn,storeProcName,parameters));
((SqlDataAdapter)iDA).Fill(dataSet,tableName);
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
return dataSet;
}
}
/**//// <summary>
/// 执行存储过程并返回受影响的行数
/// </summary>
/// <param name="storedProcName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public int RunProcedureNoQuery(string storedProcName, IDataParameter[] parameters )
{
int result = 0;
using (System.Data.IDbConnection iConn = this.GetConnection())
{
iConn.Open();
using(SqlCommand scmd = BuildQueryCommand(iConn,storedProcName,parameters))
{
result = scmd.ExecuteNonQuery();
}
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
return result ;
}
public string RunProcedureExecuteScalar(string storeProcName,IDataParameter[] parameters)
{
string result = string.Empty;
using (System.Data.IDbConnection iConn = this.GetConnection())
{
iConn.Open();
using(SqlCommand scmd = BuildQueryCommand(iConn,storeProcName,parameters))
{
object obj = scmd.ExecuteScalar();
if(obj == null)
result = null;
else
result = obj.ToString();
}
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
return result;
}
/**//// <summary>
/// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlCommand</returns>
private SqlCommand BuildQueryCommand(IDbConnection iConn,string storedProcName, IDataParameter[] parameters)
{
IDbCommand iCmd = GetCommand(storedProcName,iConn);
iCmd.CommandType = CommandType.StoredProcedure;
if (parameters == null)
{
return (SqlCommand)iCmd;
}
foreach (IDataParameter parameter in parameters)
{
iCmd.Parameters.Add( parameter );
}
return (SqlCommand)iCmd;
}
/**//// <summary>
/// 执行存储过程,返回影响的行数
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <param name="rowsAffected">影响的行数</param>
/// <returns></returns>
public int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected )
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
int result;
iConn.Open();
using(SqlCommand sqlCmd = BuildIntCommand(iConn,storedProcName, parameters ))
{
rowsAffected = sqlCmd.ExecuteNonQuery();
result = (int)sqlCmd.Parameters["ReturnValue"].Value;
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
return result;
}
}
}
/**//// <summary>
/// 创建 SqlCommand 对象实例(用来返回一个整数值)
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlCommand 对象实例</returns>
private SqlCommand BuildIntCommand(IDbConnection iConn,string storedProcName, IDataParameter[] parameters)
{
SqlCommand sqlCmd = BuildQueryCommand(iConn,storedProcName, parameters );
sqlCmd.Parameters.Add( new SqlParameter ( "ReturnValue",
SqlDbType.Int,4,ParameterDirection.ReturnValue,
false,0,0,string.Empty,DataRowVersion.Default,null ));
return sqlCmd;
}
#endregion
}
}
C#.NET万能数据库访问封装类(ACCESS、SQLServer、Oracle)的更多相关文章
- 添加数据库的Maven依赖(SqlServer,Oracle)
oracle: 1.在Oracle官网下载ojdbc的jar包 例:ojdbc7.jar,版本是12.1.0.2,存储地址/home/peng/下载 2.dos中进入存储地址执行如下命令行(注意各项对 ...
- 《万能数据库查询分析器》实现使用SQL语句直接高效地访问文本文件
<万能数据库查询分析器>实现使用SQL语句直接高效地访问文本文件 马根峰 (广东联合电子服务股份有限公司, 广州 510300) 摘要 用SQL语句来直接访问文本文件?是在做梦吗? ...
- 怎样从外网访问内网SQLServer数据库?
本地安装了一个SQLServer数据库,只能在局域网内访问到,怎样从外网也能访问到本地的SQLServer数据库呢?本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动SQLServer数据 ...
- ASP.net分别连接SQLserver数据库与连接Access数据库精英版
-------------------------连接access2003 字符串------------------------- Provider=Microsoft.Jet.OLEDB.4.0; ...
- 使用MIDAS访问远程Access数据库
使用MIDAS访问远程Access数据库 Allen Tao(http://blog.csdn.net/allentao/) 2005-5-3 本文源码下载 访问远程数据库常用的办法是 ...
- “万能数据库查询分析器” 5.03发布,访问EXCEL将自动为表名前后加上中括弧
"万能数据库查询分析器" 5.03发布,访问EXCEL将自动为表名前后加上中括弧 1 引言 中国本土程序员马根峰推出的个人作品----万能数据库查询 ...
- 关于PHP建立数据库访问类的封装以及操作php单例模式连接数据库封装类
建立数据库访问类的封装 <?php class DBDA { public $host = "localhost"; //服务器地址 public $ui ...
- C#访问SQLServer数据库访问帮助类
SQLServer数据库访问帮助类 这里有一个Mysql帮助类的使用例子可以参考(大同小异) 写了一个Mysql的例子,SQLServer的差不多,一样的 C#简单三层结构设计UI.BLL.DAL 1 ...
- C#---数据库访问通用类、Access数据库操作类、mysql类 .[转]
原文链接 //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using Sy ...
随机推荐
- android 布局 使用 viewPager 时,如何解决 和 子页面 长按滑动 冲突问题
使用 viewPager 时,如何解决 和 子页面 长按滑动 冲突问题. 我的问题原型: 这个问题,我相信遇到的人会比较少,我是在 一个 viewPager 中,其中 一个 fragment 中实现了 ...
- java 大数据处理类 BigDecimal 解析
这两天,由于我的必修课概率论里经常要用到排列组合的计算,感觉很麻烦,加上现代智能手机的计算器是没有这方面功能的. 所以,就自己动手写了个安卓的 排列组合 计算器,用了一天,发现有很大的问题,阶乘达百亿 ...
- ZOJ Problem Set - 1383 Binary Numbers
水题,输出的时候注意下 #include <stdio.h> #include <math.h> int main() { int d; scanf("%d" ...
- Buffer cache hit ratio性能计数器真的可以作为内存瓶颈的判断指标吗?
Buffer cache hit ratio官方是这么解释的:“指示在缓冲区高速缓存中找到而不需要从磁盘中读取的页的百分比.” Buffer cache hit ratio被很多人当做判断内存的性能指 ...
- 3分钟干货学会使用node-inspector调试NodeJS代码
使用node-inspector调试NodeJS代码 任何一门完备的语言技术栈都少不了健壮的调试工具,对于NodeJS平台同样如此,笔者研究了几种调试NodeJS代码的方式,通过对比,还是觉得node ...
- mongodb安装配置
下载安装 官网下载地址:https://www.mongodb.com/download-center 执行msi文件. 环境配置 可以参照我的步骤来进行配置,目录根据个人需要设置. 解压安装包到D: ...
- jQuery-1.9.1源码分析系列(十) 事件系统——事件委托
jQuery的事件绑定有几个比较优秀的特点: 1. 可以绑定不限数量的处理函数 2. 事件可以委托到祖先节点,不必一定要绑到对应的节点,这样后添加的节点也照样能被处理. 3. 链式操作 下面主要分析事 ...
- SQL Server时间粒度系列----第7节日历数据表详解
本文目录列表: 1.时间粒度有关描述 2.时间维度有关功能函数3.日历数据表 4.日历数据表数据填充 5.总结语 6.参考清单列表 时间粒度有关描述 将该系列涉及到的时间粒度以及分钟以下的粒度 ...
- Jquery.Qrcode在客户端动态生成二维码并添加自定义Logo
0 Jquery.Qrcode简介 Jquery.Qrcode.js是一个在浏览器端基于Jquery动态生成二维码的插件,支持Canvas和Table两种渲染方式,它的优点是在客户端动态生成,减轻了服 ...
- 第 31 章 项目实战-PC 端固定布局[5]
学习要点: 1.底部区域 2.说明区域 3.版权及证件区 主讲教师:李炎恢 本章主要开始使用学习用 HTML5 和 CSS3 来构建 Web 页面,第一个项目采用 PC 端固定布局来实现. 一.底部区 ...