一
既然說
關於上圖圖示中的
二
代碼
using System
using System
using System
namespace AdoNetDataaccess
{
public interface IDbOperation
{
DbCommand CreateDbCommd(DbConnection sqlConn
DbParameter CreateDbPRameter(string paramName
DbDataReader ExecuteReader(string sqlStr
DataTable FillDataTable(string sqlStr
DataSet FillDataSet(string sqlStr
object ExecuteScalar(string sqlStr
int ExecuteNonQuery(string sqlStr
/// <summary>
/// 批量插入
/// </summary>
/// <param name=
/// <param name=
/// <returns></returns>
bool ExecuteBatchInsert(string tableName
void OpenConnection();
void CloseConnection();
}
}
上面的接口包括增刪改查
底層的數據操作接口定義好後
代碼
using System;
using System
using System
using System
using System
using System
namespace AdoNetDataAccess
{
using AdoNetDataAccess
public class SqlServer : IDbOperation
{
private int cmdTimeOut =
private DbConnection sqlConn = null;
private DbCommand cmd = null;
private SqlServer()
{
}
public SqlServer(string sqlConStr)
{
sqlConn = new SqlConnection(sqlConStr);
cmdTimeOut = sqlConn
}
public SqlServer(string sqlConStr
{
sqlConn = new SqlConnection(sqlConStr);
if (timeOut <
{
timeOut = sqlConn
}
cmdTimeOut = timeOut;
}
#region contract method
public DbCommand CreateDbCommd(DbConnection sqlConn
{
DbCommand cmd = new SqlCommand();
cmd
cmd
cmd
if (transaction != null)
{
cmd
}
if (listParams != null && listParams
{
cmd
}
cmd
OpenConnection();
return cmd;
}
public DbParameter CreateDbPrameter(string paramName
{
SqlParameter sp = new SqlParameter(paramName
return sp;
}
public DbDataReader ExecuteReader(string sqlStr
{
DbDataReader rdr = null;
try
{
OpenConnection();
cmd = CreateDbCommd(sqlConn
rdr = cmd
}
catch (Exception ex)
{
throw ex;
}
return rdr;
}
public DataTable FillDataTable(string sqlStr
{
OpenConnection();
DbTransaction trans = sqlConn
DbCommand cmd = CreateDbCommd(sqlConn
SqlDataAdapter sqlDataAdpter = new SqlDataAdapter(cmd as SqlCommand);
DataTable dt = new DataTable();
try
{
sqlDataAdpter
trans
}
catch (Exception e)
{
trans
throw new Exception(
}
finally
{
sqlDataAdpter
cmd
trans
CloseConnection();
}
return dt;
}
public DataSet FillDataSet(string sqlStr
{
OpenConnection();
DbTransaction trans = sqlConn
DbCommand cmd = CreateDbCommd(sqlConn
SqlDataAdapter sqlDataAdpter = new SqlDataAdapter(cmd as SqlCommand);
DataSet ds = new DataSet();
try
{
sqlDataAdpter
trans
}
catch (Exception e)
{
trans
throw new Exception(
}
finally
{
sqlDataAdpter
cmd
trans
CloseConnection();
}
return ds;
}
public object ExecuteScalar(string sqlStr
{
object result = null;
OpenConnection();
DbTransaction trans = sqlConn
try
{
cmd = CreateDbCommd(sqlConn
result = cmd
trans
}
catch (Exception e)
{
trans
throw new Exception(
}
finally
{
trans
CloseConnection();
}
return result;
}
public int ExecuteNonQuery(string sqlStr
{
int result =
OpenConnection();
DbTransaction trans = sqlConn
try
{
cmd = CreateDbCommd(sqlConn
result = cmd
trans
}
catch (Exception e)
{
trans
throw new Exception(
}
finally
{
trans
CloseConnection();
}
return result;
}
/// <summary>
/// 批量插入
/// </summary>
/// <param name=
/// <param name=
/// <param name=
/// <param name=
/// <returns></returns>
public bool ExecuteBatchInsert(string tableName
{
bool flag = false;
try
{
using (TransactionScope scope = new TransactionScope())
{
OpenConnection();
using (SqlBulkCopy sbc = new SqlBulkCopy(sqlConn as SqlConnection))
{
//服務器上目標表的名稱
sbc
sbc
sbc
for (int i =
{
//列映射定義數據源中的列和目標表中的列之間的關系
sbc
}
sbc
flag = true;
scope
}
}
}
catch (Exception ex)
{
throw ex;
}
return flag;
}
public void OpenConnection()
{
if (sqlConn
sqlConn
}
public void CloseConnection()
{
sqlConn
}
#endregion
#region dispose method
/// <summary>
/// dispose接口方法
/// </summary>
public void Dispose()
{
}
#endregion
}
}
到這裡
三
在第二步中
代碼
using System;
using System
using System
using System
using System
using System
using System
namespace AdoNetDataAccess
{
using AdoNetDataAccess
public sealed class ModelConverter
{
private static readonly object objSync = new object();
#region query for list
/// <summary>
/// 查詢數據表項並轉換為對應實體
/// </summary>
/// <typeparam name=
/// <param name=
/// <param name=
/// <returns></returns>
public static IList<T> QueryForList<T>(string sqlStr
where T : class
{
IDataReader rdr = dbOperation
IList<T> listModels = new List<T>();
try
{
Monitor
Hashtable ht = CreateHashColumnName(rdr);
while (rdr
{
Object obj = Activator
PropertyInfo[] properties = objType
foreach (PropertyInfo propInfo in properties)
{
string columnName = propInfo
if (ht
{
continue;
}
int index = rdr
object columnValue = rdr
if (columnValue != System
{
SetValue(propInfo
}
}
T model = default(T);
model = obj as T;
listModels
}
}
finally
{
rdr
rdr
Monitor
}
return listModels;
}
#endregion
#region query for dictionary
/// <summary>
/// 查詢數據表項並轉換為對應實體
/// </summary>
/// <typeparam name=
/// <typeparam name=
/// <param name=
/// <param name=
/// <param name=
/// <returns></returns>
public static IDictionary<K
where T : class
{
IDataReader rdr = dbOperation
IDictionary<K
try
{
Monitor
Hashtable ht = CreateHashColumnName(rdr);
while (rdr
{
Object obj = Activator
PropertyInfo[] properties = objType
object dictKey = null;
foreach (PropertyInfo propInfo in properties)
{
string columnName = propInfo
if (ht
{
continue;
}
int index = rdr
object columnValue = rdr
if (columnValue != System
{
SetValue(propInfo
if (string
{
dictKey = columnValue;
}
}
}
T model = default(T);
model = obj as T;
K objKey = (K)dictKey;
dictModels
}
}
finally
{
rdr
rdr
Monitor
}
return dictModels;
}
#endregion
#region internal util
private static Hashtable CreateHashColumnName(IDataReader rdr)
{
int len = rdr
Hashtable ht = new Hashtable(len);
for (int i =
{
string columnName = rdr
string columnRealName = rdr
if (ht
{
ht
}
}
return ht;
}
private static void SetValue(PropertyInfo propInfo
{
try
{
propInfo
}
catch
{
object realValue = null;
try
{
realValue = Convert
propInfo
}
catch (Exception ex)
{
string err = ex
//throw ex; //在數據庫數據有不符合規范的情況下應該及時拋出異常
}
}
}
#endregion
}
}
到這裡
四
代碼
using System;
using System
using System
using System
using System
namespace AdoNetDataAccess
{
using AdoNetDataAccess
public abstract class BaseMapper
{
public IDbOperation CurrentDbOperation;
#region query for list
public abstract IList<T> QueryForList<T>(string sqlStr)
where T : class
public abstract IList<T> QueryForList<T>(string sqlStr
where T : class
public abstract IList<T> QueryForList<T>(string sqlStr
where T : class
public abstract IList<T> QueryForList<T>(string sqlStr
where T : class
#endregion
#region query for dictionary
public abstract IDictionary<K
where T : class
public abstract IDictionary<K
where T : class
public abstract IDictionary<K
where T : class
public abstract IDictionary<K
where T : class
#endregion
#region dataset datatable
public abstract DataTable FillDataTable(string sqlStr
public abstract DataSet FillDataSet(string sqlStr
#endregion
#region ExecuteScalar
public abstract object ExecuteScalar(string sqlStr
#endregion
#region insert
public abstract int Insert(string sqlStr);
public abstract int Insert(string sqlStr
public abstract bool BatchInsert(string tableName
#endregion
#region delete
public abstract int Delete(string sqlStr);
public abstract int Delete(string sqlStr
#endregion
#region update
public abstract int Update(string sqlStr);
public abstract int Update(string sqlStr
#endregion
}
}
上面代碼中的方法您是不是很熟悉呢? 呵呵
代碼
using System;
using System
using System
using System
namespace AdoNetDataAccess
{
using AdoNetDataAccess
using AdoNetDataAccess
public class SqlMapper : BaseMapper
{
private SqlMapper()
{
}
public SqlMapper(IDbOperation dbOperation)
{
this
}
#region query for list
public override IList<T> QueryForList<T>(string sqlStr)
{
return QueryForList<T>(sqlStr
}
public override IList<T> QueryForList<T>(string sqlStr
{
return QueryForList<T>(sqlStr
}
public override IList<T> QueryForList<T>(string sqlStr
{
return QueryForList<T>(sqlStr
}
public override IList<T> QueryForList<T>(string sqlStr
{
return ModelConverter
}
#endregion
#region query for dictionary
public override IDictionary<K
{
return QueryForDictionary<K
}
public override IDictionary<K
{
return QueryForDictionary<K
}
public override IDictionary<K
{
return QueryForDictionary<K
}
public override IDictionary<K
{
return ModelConverter
}
#endregion
#region dataset datatable
public override DataTable FillDataTable(string sqlStr
{
return this
}
public override DataSet FillDataSet(string sqlStr
{
return this
}
#endregion
#region ExecuteScalar
public override object ExecuteScalar(string sqlStr
{
return this
}
#endregion
#region insert
public override int Insert(string sqlStr)
{
object obj = ExecuteScalar(sqlStr
int id = obj == null ?
return id;
}
public override int Insert(string sqlStr
{
object obj = ExecuteScalar(sqlStr
int id = obj == null ?
return id;
}
/// <summary>
/// 批量插入
/// </summary>
/// <param name=
/// <param name=
/// <param name=
/// <param name=
/// <returns></returns>
public override bool BatchInsert(string tableName
{
return this
}
#endregion
#region delete
public override int Delete(string sqlStr)
{
return CommitSql(sqlStr
}
public override int Delete(string sqlStr
{
return CommitSql(sqlStr
}
#endregion
#region update
public override int Update(string sqlStr)
{
return CommitSql(sqlStr
}
public override int Update(string sqlStr
{
return CommitSql(sqlStr
}
#endregion
#region commit and execute sql
private int CommitSql(string sqlStr
{
return this
}
#endregion
#region dbparameter
public DbParameter CreateParameter(string paraName
{
return this
}
public List<DbParameter> CreateParameterList(string[] paraNames
{
List<DbParameter> listParams = new List<DbParameter>();
try
{
if (paraNames
{
throw new Exception(
}
for (int i =
{
DbParameter param = CreateParameter(paraNames[i]
listParams
}
}
catch (Exception ex)
{
throw ex;
}
return listParams;
}
#endregion
}
}
上面的方法豐富實現了CRUD的常見操作
From:http://tw.wingwit.com/Article/program/ASP/201311/21746.html