以下是我编写的DataAccess通用数据库访问类,简单易用,支持:内联式创建多个参数、支持多事务提交、支持参数复用、支持更换数据库类型,希望能帮到大家,若需支持查出来后转换成实体,可以自行扩展datarow转实体类,也可以搭配dapper.net实现更强大的功能。

    /// <summary>
/// 通用数据库访问类,支持多种数据库,无直接依赖某个数据库组件
/// 作者:左文俊
/// 日期:2016-6-3(修订:2017-6-1)
/// </summary>
public class DataAccess : IDisposable
{
[ThreadStatic]
private static DbProviderFactory _dbProviderFactory = null; [ThreadStatic]
private static string _connectionString = string.Empty; [ThreadStatic]
public static string ConnectionStringName = string.Empty; private DbProviderFactory dbProviderFactory = null;
private string connectionString = string.Empty; private DbConnection dbConnection = null;
private DbTransaction dbTransaction = null;
private bool useTransaction = false;
private bool disposed = false;
private bool committed = false; private ParameterHelperClass paramHelper = null; public DataAccess()
: this("default")
{ } public DataAccess(string cnnStringName, Func<string, string> DecryptCnnStringFunc = null)
{
if (!string.Equals(ConnectionStringName, cnnStringName, StringComparison.OrdinalIgnoreCase) ||
_dbProviderFactory == null || _connectionString == null)
{
ConnectionStringName = cnnStringName;
var cnnStringSection = ConfigurationManager.ConnectionStrings[cnnStringName];
_dbProviderFactory = DbProviderFactories.GetFactory(cnnStringSection.ProviderName);
_connectionString = cnnStringSection.ConnectionString;
if (DecryptCnnStringFunc != null)
{
_connectionString = DecryptCnnStringFunc(_connectionString);
}
}
dbProviderFactory = _dbProviderFactory;
connectionString = _connectionString; paramHelper = new ParameterHelperClass(this);
} public DataAccess(string cnnString, string providerName)
{
if (!string.Equals(_connectionString, cnnString, StringComparison.OrdinalIgnoreCase) || _dbProviderFactory == null)
{
ConnectionStringName = string.Empty;
_connectionString = cnnString;
_dbProviderFactory = DbProviderFactories.GetFactory(providerName);
}
dbProviderFactory = _dbProviderFactory;
connectionString = _connectionString; paramHelper = new ParameterHelperClass(this);
} #region 私有方法 private DbConnection GetDbConnection()
{ if (dbConnection == null)
{
dbConnection = dbProviderFactory.CreateConnection();
dbConnection.ConnectionString = connectionString;
} if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
} if (useTransaction && dbTransaction == null)
{
dbTransaction = dbConnection.BeginTransaction();
committed = false;
} return dbConnection;
} private DbCommand BuildDbCommand(string sqlCmdText, CommandType cmdType = CommandType.Text, DbParameter[] parameters = null)
{
var dbCmd = dbProviderFactory.CreateCommand();
var dbConn = GetDbConnection();
dbCmd.Connection = dbConn;
dbCmd.CommandText = sqlCmdText;
dbCmd.CommandType = cmdType;
dbCmd.CommandTimeout = * ;
if (useTransaction)
{
dbCmd.Transaction = dbTransaction;
} if (parameters != null)
{
dbCmd.Parameters.AddRange(parameters);
} return dbCmd;
} private DbCommand BuildDbCommand(string sqlCmdText, CommandType cmdType = CommandType.Text, IDictionary<string, object> paramNameValues = null)
{
List<DbParameter> parameters = new List<DbParameter>();
if (paramNameValues != null)
{
foreach (var item in paramNameValues)
{
parameters.Add(BuildDbParameter(item.Key, item.Value));
}
}
return BuildDbCommand(sqlCmdText, cmdType, parameters.ToArray());
} private DbCommand BuildDbCommand(string sqlCmdText, CommandType cmdType = CommandType.Text, params object[] paramObjs)
{
if (paramObjs != null && paramObjs.Length > )
{
if (paramObjs[] is IDictionary<string, object>)
{
return BuildDbCommand(sqlCmdText, cmdType, paramObjs[] as IDictionary<string, object>);
}
else if (paramObjs is DbParameter[])
{
return BuildDbCommand(sqlCmdText, cmdType, paramObjs as DbParameter[]);
}
else
{
List<DbParameter> parameters = new List<DbParameter>();
for (int i = ; i < paramObjs.Length; i++)
{
parameters.Add(BuildDbParameter("@p" + i.ToString(), paramObjs[i]));
}
return BuildDbCommand(sqlCmdText, cmdType, parameters.ToArray());
}
}
else
{
return BuildDbCommand(sqlCmdText, cmdType, parameters: null);
}
} private void ClearCommandParameters(DbCommand cmd)
{
bool canClear = true;
if (cmd.Connection != null && cmd.Connection.State != ConnectionState.Open)
{
foreach (DbParameter commandParameter in cmd.Parameters)
{
if (commandParameter.Direction != ParameterDirection.Input)
{
canClear = false;
break;
}
}
}
if (canClear)
{
cmd.Parameters.Clear();
}
} #endregion #region 公共方法 public void UseTransaction()
{
useTransaction = true;
} public void Commit()
{
if (dbTransaction != null && useTransaction)
{
dbTransaction.Commit();
dbTransaction.Dispose();
dbTransaction = null;
committed = true;
useTransaction = false;
}
} public DbParameter BuildDbParameter(string name, object value)
{
DbParameter parameter = dbProviderFactory.CreateParameter();
parameter.ParameterName = name;
parameter.Value = value != null ? value : DBNull.Value;
return parameter;
} public DbParameter BuildDbParameter(string name, object value, DbType dbType, int size = -, ParameterDirection direction = ParameterDirection.Input)
{
DbParameter parameter = dbProviderFactory.CreateParameter();
parameter.ParameterName = name;
parameter.Value = value != null ? value : DBNull.Value;
parameter.DbType = dbType;
parameter.Direction = direction;
if (size != -)
{
parameter.Size = size;
}
return parameter;
} public DbParameter BuildDbParameter(string name, DbType dbType, string srcColumn, int size = -, bool srcColumnNullMapping = true, ParameterDirection direction = ParameterDirection.Input)
{
DbParameter parameter = dbProviderFactory.CreateParameter();
parameter.ParameterName = name;
parameter.SourceColumn = srcColumn;
parameter.SourceColumnNullMapping = srcColumnNullMapping;
parameter.DbType = dbType;
parameter.Direction = direction;
if (size != -)
{
parameter.Size = size;
}
return parameter;
} public DbDataReader ExecuteReader(string sqlCmdText, CommandType cmdType = CommandType.Text, params object[] paramObjs)
{
var dbCmd = BuildDbCommand(sqlCmdText, cmdType, paramObjs);
var dr = dbCmd.ExecuteReader();
ClearCommandParameters(dbCmd);
return dr;
} public T ExecuteScalar<T>(string sqlCmdText, CommandType cmdType = CommandType.Text, params object[] paramObjs)
{
T returnValue = default(T);
var dbCmd = BuildDbCommand(sqlCmdText, cmdType, paramObjs);
object result = dbCmd.ExecuteScalar();
try
{
returnValue = (T)Convert.ChangeType(result, typeof(T));
}
catch
{ }
ClearCommandParameters(dbCmd);
return returnValue;
} public DataSet ExecuteDataSet(string sqlCmdText, CommandType cmdType = CommandType.Text, params object[] paramObjs)
{
var dbCmd = BuildDbCommand(sqlCmdText, cmdType, paramObjs);
var dbAdapter = dbProviderFactory.CreateDataAdapter();
dbAdapter.SelectCommand = dbCmd;
DataSet returnDataSet = new DataSet();
dbAdapter.Fill(returnDataSet);
ClearCommandParameters(dbCmd);
return returnDataSet;
} public DataTable ExecuteDataTable(string sqlCmdText, CommandType cmdType = CommandType.Text, params object[] paramObjs)
{
DataTable returnTable = new DataTable();
DataSet resultDataSet = ExecuteDataSet(sqlCmdText, cmdType, paramObjs);
if (resultDataSet != null && resultDataSet.Tables.Count > )
{
returnTable = resultDataSet.Tables[];
}
return returnTable;
} public int ExecuteCommand(string sqlCmdText, CommandType cmdType = CommandType.Text, params object[] paramObjs)
{
var dbCmd = BuildDbCommand(sqlCmdText, cmdType, paramObjs);
int execResult = dbCmd.ExecuteNonQuery();
ClearCommandParameters(dbCmd);
return execResult;
} public int BatchExecuteCommand(string sqlCmdText, DbParameter[] sqlParams, DataTable srcTable, int batchSize = , bool needAllReChange = true)
{
var dbCmd = BuildDbCommand(sqlCmdText, CommandType.Text, sqlParams);
dbCmd.UpdatedRowSource = System.Data.UpdateRowSource.None;
var dbAdapter = dbProviderFactory.CreateDataAdapter();
dbAdapter.AcceptChangesDuringUpdate = false;
sqlCmdText = sqlCmdText.Trim();
string bathExecType = System.Text.RegularExpressions.Regex.Match(sqlCmdText, @"^\w+\b").Value.ToUpper();
if (needAllReChange)
{
srcTable.AcceptChanges();
} switch (bathExecType)
{
case "INSERT":
{
dbAdapter.InsertCommand = dbCmd;
if (needAllReChange)
{
foreach (DataRow row in srcTable.Rows) row.SetAdded();
}
break;
}
case "UPDATE":
{
dbAdapter.UpdateCommand = dbCmd;
if (needAllReChange)
{
foreach (DataRow row in srcTable.Rows) row.SetModified();
}
break;
}
case "DELETE":
{
dbAdapter.DeleteCommand = dbCmd;
if (needAllReChange)
{
for (int r = srcTable.Rows.Count - ; r >= ; r--) srcTable.Rows[r].Delete();
}
break;
}
default:
{
throw new ArgumentException("无效的SQL命令!", "sqlCmdText");
}
} dbAdapter.UpdateBatchSize = batchSize;
int execResult = dbAdapter.Update(srcTable); ;
ClearCommandParameters(dbCmd);
return execResult;
} public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
} #endregion private void Dispose(bool disposing)
{
if (!disposed)
{
try
{
if (disposing)
{
//释放托管资源
} if (dbTransaction != null)
{
if (!committed)
{
dbTransaction.Rollback();
}
dbTransaction.Dispose();
} if (dbConnection != null)
{
if (dbConnection.State != ConnectionState.Closed)
{
dbConnection.Close();
}
dbConnection.Dispose();
}
}
catch
{ } disposed = true;
}
} ~DataAccess()
{
Dispose(false);
} public ParameterHelperClass ParameterHelper
{
get
{
return paramHelper;
}
} public class ParameterHelperClass
{
private List<DbParameter> parameterList = null;
private DataAccess parent = null; public ParameterHelperClass(DataAccess da)
{
parent = da;
parameterList = new List<DbParameter>();
} public ParameterHelperClass AddParameter(string name, object value)
{
parameterList.Add(parent.BuildDbParameter(name, value));
return this;
} public ParameterHelperClass AddParameter(string name, object value, DbType dbType, int size = -, ParameterDirection direction = ParameterDirection.Input)
{
parameterList.Add(parent.BuildDbParameter(name, value, dbType, size, direction));
return this;
} public ParameterHelperClass AddParameter(string name, DbType dbType, string srcColumn, int size = -, bool srcColumnNullMapping = true, ParameterDirection direction = ParameterDirection.Input)
{
parameterList.Add(parent.BuildDbParameter(name, dbType, srcColumn, size, srcColumnNullMapping, direction));
return this;
} public ParameterHelperClass AddParametersWithValue(params object[] paramValues)
{
for (int i = ; i < paramValues.Length; i++)
{
parameterList.Add(parent.BuildDbParameter("@p" + i.ToString(), paramValues[i]));
}
return this;
} public DbParameter[] ToParameterArray()
{
var paramList = parameterList;
parameterList = new List<DbParameter>();
return paramList.ToArray();
} } }

  

多种灵活用法,使用示例代码如下:

用法一:采用内联式创建参数数组对象,然后执行SQL命令

            using (DataAccess da = new DataAccess())
{
var programInfo = new ProgramInfo() { Name="test", Version="1.0", InstalledLocation=AppDomain.CurrentDomain.BaseDirectory }; var parameters = da.ParameterHelper.AddParameter("@Mbno", "188231670**")
.AddParameter("@Msg", string.Format("程序名:{0},版本:{1},安装路径:{2},已停止运行了,请尽快处理!",
programInfo.Name, programInfo.Version, programInfo.InstalledLocation))
.AddParameter("@SendTime", DateTime.Now)
.AddParameter("@KndType", "监控异常通知")
.ToParameterArray(); da.ExecuteCommand("insert into OutBox(Mbno,Msg,SendTime,KndType) values(@Mbno,@Msg,@SendTime,@KndType)", paramObjs: parameters); }

用法二:在用法一基础上使用事务来进行提交

            using (DataAccess da = new DataAccess())
{
var programInfo = new ProgramInfo() { Name = "test", Version = "1.0", InstalledLocation = AppDomain.CurrentDomain.BaseDirectory }; var parameters = da.ParameterHelper.AddParameter("@Mbno", "188231670**")
.AddParameter("@Msg", string.Format("程序名:{0},版本:{1},安装路径:{2},已停止运行了,请尽快处理!",
programInfo.Name, programInfo.Version, programInfo.InstalledLocation))
.AddParameter("@SendTime", DateTime.Now)
.AddParameter("@KndType", "监控异常通知")
.ToParameterArray(); da.UseTransaction(); da.ExecuteCommand("insert into OutBox(Mbno,Msg,SendTime,KndType) values(@Mbno,@Msg,@SendTime,@KndType)", paramObjs: parameters); da.Commit();
}

用法三:在用法二基础上使用事务一次性执行多个SQL命令

            using (DataAccess da = new DataAccess())
{
var programInfo = new ProgramInfo() { Name = "test", Version = "1.0", InstalledLocation = AppDomain.CurrentDomain.BaseDirectory }; var parameters = da.ParameterHelper.AddParameter("@Mbno", "188231670**")
.AddParameter("@Msg", string.Format("程序名:{0},版本:{1},安装路径:{2},已停止运行了,请尽快处理!",
programInfo.Name, programInfo.Version, programInfo.InstalledLocation))
.AddParameter("@SendTime", DateTime.Now)
.AddParameter("@KndType", "监控异常通知")
.ToParameterArray(); da.UseTransaction(); da.ExecuteCommand("insert into OutBox(Mbno,Msg,SendTime,KndType) values(@Mbno,@Msg,@SendTime,@KndType)", paramObjs: parameters); da.ExecuteCommand("insert into OutBox(Mbno,Msg,SendTime,KndType) values(@Mbno,@Msg,@SendTime,@KndType)", paramObjs: parameters); da.Commit();
}

用法四:在用法一基础上使用多个事务来执行多个SQL并进行多次提交

            using (DataAccess da = new DataAccess())
{
var programInfo = new ProgramInfo() { Name = "test", Version = "1.0", InstalledLocation = AppDomain.CurrentDomain.BaseDirectory }; var parameters = da.ParameterHelper.AddParameter("@Mbno", "188231670**")
.AddParameter("@Msg", string.Format("程序名:{0},版本:{1},安装路径:{2},已停止运行了,请尽快处理!",
programInfo.Name, programInfo.Version, programInfo.InstalledLocation))
.AddParameter("@SendTime", DateTime.Now)
.AddParameter("@KndType", "监控异常通知")
.ToParameterArray(); da.UseTransaction(); da.ExecuteCommand("insert into OutBox(Mbno,Msg,SendTime,KndType) values(@Mbno,@Msg,@SendTime,@KndType)", paramObjs: parameters); da.Commit(); da.UseTransaction(); da.ExecuteCommand("insert into OutBox(Mbno,Msg,SendTime,KndType) values(@Mbno,@Msg,@SendTime,@KndType)", paramObjs: parameters); da.Commit();
}

用法五:事务提交+SQL命令查询

            using (DataAccess da = new DataAccess())
{
var programInfo = new ProgramInfo() { Name = "test", Version = "1.0", InstalledLocation = AppDomain.CurrentDomain.BaseDirectory }; var parameters = da.ParameterHelper.AddParameter("@Mbno", "188231670**")
.AddParameter("@Msg", string.Format("程序名:{0},版本:{1},安装路径:{2},已停止运行了,请尽快处理!",
programInfo.Name, programInfo.Version, programInfo.InstalledLocation))
.AddParameter("@SendTime", DateTime.Now)
.AddParameter("@KndType", "监控异常通知")
.ToParameterArray(); da.UseTransaction(); da.ExecuteCommand("insert into OutBox(Mbno,Msg,SendTime,KndType) values(@Mbno,@Msg,@SendTime,@KndType)", paramObjs: parameters); da.Commit(); parameters = da.ParameterHelper.AddParameter("@Mbno", "188231670**").ToParameterArray();
var table = da.ExecuteDataTable("select Mbno,Msg,SendTime,KndType from OutBox where Mbno=@Mbno", paramObjs: parameters);
System.Windows.Forms.MessageBox.Show(table.Rows.Count.ToString());
}

用法六:不采用内联方式创建参数,而是执行SQL命令时直接传入各类型的参数

            using (DataAccess da = new DataAccess())
{
var programInfo = new ProgramInfo() { Name = "test", Version = "1.0", InstalledLocation = AppDomain.CurrentDomain.BaseDirectory }; da.ExecuteCommand("insert into OutBox(Mbno,Msg,SendTime,KndType) values(@Mbno,@Msg,@SendTime,@KndType)",
System.Data.CommandType.Text,
new Dictionary<string, object> {
{"@Mbno", "188231670**"},
{"@Msg", string.Format("程序名:{0},版本:{1},安装路径:{2},已停止运行了,请尽快处理!",
programInfo.Name, programInfo.Version, programInfo.InstalledLocation)},
{"@SendTime", DateTime.Now},
{"@KndType", "监控异常通知"}
}); var table = da.ExecuteDataTable("select Mbno,Msg,SendTime,KndType from OutBox where Mbno=@p0",
System.Data.CommandType.Text,
"188231670**"//若采用直接是输入值数组,那么SQL命令中的参数占位符必需定义成:@p0,@p1...
);
System.Windows.Forms.MessageBox.Show(table.Rows.Count.ToString());
}

用法七:除了上面使用DataAccess.ParameterHelper属性的AddParameter(string name, object value)方法来创建参数,还可以使用AddParameter(string name, object value, DbType dbType, ParameterDirection direction = ParameterDirection.Input)来创建指定输入输出及类型的参数,还有AddParametersWithValue(params object[] paramValues)来根据值数组创建参数

若需要更换数据库类型,只需要在配置文件的connectionStrings节点加入相关的连接子节点,注意providerName特性,providerName常用的如下:

Aceess数据库:providerName="System.Data.OleDb"

Oracle 数据库:providerName="System.Data.OracleClient"或者providerName="Oracle.DataAccess.Client"

SQLite数据库:providerName="System.Data.SQLite"

SQL SERVER数据库:providerName="System.Data.SqlClient"

MYSQL数据库:providerName="MySql.Data.MySqlClient"

ODBC连接数据库:providerName="System.Data.Odbc"

【补充说明】

由于目前大部份都是需要返回Model对象,故我借鉴网上的资源整合了一个基于DataTable、DataRow 通过Lambda表达式树快速转换成实体的类,代码如下:

    /// <summary>
/// 实体转换工具类(将DataRow,DataTable,DataReader以高效方式转换为指定实体对象或实体对象列表)
/// </summary>
public static class EntityConverter
{
private static Action<T, object> GetSetter<T>(PropertyInfo property)
{
Type type = typeof(T);
string key = type.AssemblyQualifiedName + "_set_" + property.Name; var result = MemoryCacheUtil.GetOrAddCacheItem<Action<T, object>>(key, () =>
{
//创建 对实体 属性赋值的expression
ParameterExpression parameter = Expression.Parameter(type, "t");
ParameterExpression value = Expression.Parameter(typeof(object), "propertyValue");
MethodInfo setter = type.GetMethod("set_" + property.Name);
MethodCallExpression call = Expression.Call(parameter, setter, Expression.Convert(value, property.PropertyType, GetConvertMethodInfo(property.PropertyType)));
var lambda = Expression.Lambda<Action<T, object>>(call, parameter, value);
var setterAction = lambda.Compile();
return setterAction;
}, null, DateTime.UtcNow.AddDays(1)); return result;
} private static MethodInfo GetConvertMethodInfo(Type propertyType)
{
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(propertyType);
propertyType = nullableConverter.UnderlyingType;
}
var convertMethod = typeof(Convert).GetMethod("ToString", new[] { typeof(object) });
if (new[] { typeof(int), typeof(long), typeof(decimal), typeof(float), typeof(double) }.Contains(propertyType))
{
convertMethod = typeof(Convert).GetMethod("To" + propertyType.Name, new[] { typeof(object) });
}
else if (typeof(bool).Equals(propertyType))
{
convertMethod = typeof(Convert).GetMethod("ToBoolean", new[] { typeof(object) });
}
else if (typeof(DateTime).Equals(propertyType))
{
convertMethod = typeof(Convert).GetMethod("ToDateTime", new[] { typeof(object) });
} return convertMethod;
} public static T To<T>(DataRow row, PropertyInfo[] props = null) where T : new()
{
T model = new T();
if (props == null)
{
props = typeof(T).GetProperties();
} foreach (PropertyInfo prop in props)
{
if (row.Table.Columns.IndexOf(prop.Name) >= 0)
{
var setterAction = GetSetter<T>(prop);
if (prop != null && row[prop.Name] != DBNull.Value)
setterAction(model, row[prop.Name]);
}
} return model;
} public static T To<T>(DataRow row, Func<string, object, object> beforePropFunc, PropertyInfo[] props = null) where T : new()
{
T model = new T();
if (props == null)
{
props = typeof(T).GetProperties();
} foreach (PropertyInfo prop in props)
{
if (row.Table.Columns.IndexOf(prop.Name) >= 0)
{
var setterAction = GetSetter<T>(prop);
if (prop != null && row[prop.Name] != DBNull.Value)
{
object propValue = beforePropFunc(prop.Name, row[prop.Name]);
setterAction(model, propValue);
}
}
} return model;
} public static List<T> ToList<T>(this DataTable dt) where T : new()
{
List<T> list = new List<T>();
if (dt == null || dt.Rows.Count == 0)
{
return list;
} var props = typeof(T).GetProperties();
foreach (DataRow dr in dt.Rows)
{
var t = To<T>(dr, props);
list.Add(t);
} return list;
} public static List<T> ToList<T>(this DataTable dt, Func<T, PropertyInfo[], T> afterFunc) where T : new()
{
List<T> list = new List<T>();
if (dt == null || dt.Rows.Count == 0)
{
return list;
}
var props = typeof(T).GetProperties();
foreach (DataRow dr in dt.Rows)
{
var t = To<T>(dr, props);
t = afterFunc(t,props);
list.Add(t);
} return list;
} public static List<T> ToList<T>(IDataReader dr) where T : new()
{
List<T> list = new List<T>();
var props = typeof(T).GetProperties();
while (dr.Read())
{
T t = new T();
foreach (PropertyInfo prop in props)
{
if (prop != null && dr[prop.Name] != DBNull.Value)
GetSetter<T>(prop)(t, dr[prop.Name]);
}
list.Add(t);
}
return list;
} public static List<T> ToList<T>(IEnumerable<DataRow> rows) where T : new()
{
List<T> list = new List<T>();
if (rows == null || rows.Count() == 0)
{
return list;
} var props = typeof(T).GetProperties();
foreach (DataRow dr in rows)
{
var t = To<T>(dr, props);
list.Add(t);
} return list;
}
}

用法很简单,直接在查询结果后面使用扩展方法,示例如下:

var table = da.ExecuteDataTable("select Mbno,Msg,SendTime,KndType from OutBox where Mbno=@p0",
System.Data.CommandType.Text,
"188231670**"//若采用直接是输入值数组,那么SQL命令中的参数占位符必需定义成:@p0,@p1...
);
//第一种将上面的table再次使用扩展方法进行转换
List<ResultModel> resultList= table.ToList<ResultModel>(); //第二种在执行后直接使用扩展方法进行转换(其实本质都是一样的,只是这里只需一步)
var resultList= da.ExecuteDataTable("select Mbno,Msg,SendTime,KndType from OutBox where Mbno=@p0",
System.Data.CommandType.Text,
"188231670**"//若采用直接是输入值数组,那么SQL命令中的参数占位符必需定义成:@p0,@p1...
).ToList<ResultModel>();

  

DataAccess通用数据库访问类,简单易用,功能强悍的更多相关文章

  1. 一个通用数据库访问类(C#,SqlClient)

    本文转自:http://www.7139.com/jsxy/cxsj/c/200607/114291.html使用ADO.NET时,每次数据库操作都要设置connection属性.建立connecti ...

  2. ADO.NET通用数据库访问类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. C# 通用数据访问类(SqlHelper)

    [转]C# 通用数据访问类(SqlHelper) 注:本文转自http://www.tzwhx.com/newOperate/html/3/31/312/13080.htmlVisual C# 动态操 ...

  4. [转]DbHelper通用数据库访问帮助类

    之前我一直都是在博客园中查看各位高手的博文,确实学到了不少知识,不过比较少写博客,现在就把我自己在项目实施过程中使用到比较好的技术框架写出来,希望能让更多的人了解和学习. 通常我们在开发使用数据库访问 ...

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

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

  6. 我也来写:数据库访问类DBHelper(转)

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

  7. 学习实践:使用模式,原则实现一个C++数据库访问类

    一.概述 在我参与的多个项目中,大家使用libMySQL操作MySQL数据库,而且是源码即复用,在多个项目中有多套相同或相似的源码,这样的复用方式给开发带来了不变,而且libMySQL的使用比较麻烦, ...

  8. 通用数据库操作类,前端easyui-datagrid,form

    实现功能:     左端datagrid显示简略信息,右侧显示选中行详细信息,数据库增删改 (1)点击选中行,右侧显示详细信息,其中[新增].[修改].[删除]按钮可用,[保存]按钮禁用 (2)点击[ ...

  9. C#.NET数据库访问类DBHelper

    这是一个与C# .NET通用的数据库访问类,包含了工厂模式.事务处理等安全机制. 调用方式: DBHelper db = new DBHelper(); DbCommand cmd = db.GetS ...

随机推荐

  1. Laravel 5.x 请求的生命周期(附源码)

    Laravel最早接触是刚开始实习的时候,那时通过网上的学习资料很快便上手,开发模块接口.后来没有什么深入和总结,但是当我刚开始学Laravel的时候,我对Laravel最大的认识就是,框架除了路由. ...

  2. App你真的需要么

    随着智能手机.移动路联网的普及,APP火的一塌糊涂,APP应用可谓五花八门,街上经常看到各种推广:扫码安装送东西,送优惠券.仿佛一夜之间一个企业没有自己的APP就跟不上时代了. 有时我在想:APP,你 ...

  3. 通过 floating IP 访问 VIP - 每天5分钟玩转 OpenStack(126)

    前面我们是直接用 curl 测试 VIP,在更为真实的场景中通常会使用 floating IP 访问 VIP. 下面我们给 VIP 关联一个 floating IP,再进行测试. 访问 Project ...

  4. Linux上如何查看物理CPU个数,核数,线程数

    首先,看看什么是超线程概念 超线程技术就是利用特殊的硬件指令,把两个逻辑内核模拟成两个物理芯片,让单个处理器都能使用线程级并行计算,进而兼容多线程操作系统和软件,减少了CPU的闲置时间,提高的CPU的 ...

  5. Javascript实现HashTable类

    散列算法可以尽快在数据结构中找出指定的一个值,因为可以通过Hash算法求出值的所在位置,存储和插入的时候都按照Hash算法放到指定位置. <script> function HashTab ...

  6. RabbitMQ + PHP (三)案例演示

    今天用一个简单的案例来实现 RabbitMQ + PHP 这个消息队列的运行机制. 主要分为两个部分: 第一:发送者(publisher) 第二:消费者(consumer) (一)生产者 (创建一个r ...

  7. Javascript中的valueOf与toString

    基本上,javascript中所有数据类型都拥有valueOf和toString这两个方法,null除外.它们俩解决javascript值运算与显示的问题,本文将详细介绍,有需要的朋友可以参考下. t ...

  8. Spark的StandAlone模式原理和安装、Spark-on-YARN的理解

    Spark是一个内存迭代式运算框架,通过RDD来描述数据从哪里来,数据用那个算子计算,计算完的数据保存到哪里,RDD之间的依赖关系.他只是一个运算框架,和storm一样只做运算,不做存储. Spark ...

  9. JS高级前端开发群加群说明及如何晋级

    JS高级前端开发群加群说明 一.文章背景: 二. 高级群: 三. 加入方式: 四. 说明:   一.文章背景: 去年年初建了几个群,在不经意间火了,一直排在“前端开发”关键字搜索结果第一名.当然取得这 ...

  10. C#(或者说.NET/Mono)能做的那些事

    不做语言之争,只陈述事实: 1.桌面软件与服务 不仅是在Windows上,有了开源的Mono,在Apple Mac和Linux(如:Ubuntu)上也有C#的施展天地.并且还可以通过mkbundle工 ...