我通过使用来自ISesssion of NHibernate的连接来实现查询ado.net概念的类。我想要从ado.net(例如DataTable或DataReader)返回的地图结果 class by apply classmap configuration。
请帮助解决方案 谢谢。
我的代码
public class DbQuery : IDbQuery
{
private readonly Dictionary<string, IDbDataParameter> _containerParameters;
private readonly IDbSession _session;
private IDbCommand _command;
public DbQuery(IDbSession session)
{
_session = session;
_containerParameters = new Dictionary<string, IDbDataParameter>();
}
public IDbDataParameter this[string parameterName]
{
get { return _containerParameters[parameterName]; }
}
public void CreateCommand(string commandText, CommandType commandType)
{
_command = _session.CurrentSession.Connection.CreateCommand();
_command.CommandText = commandText;
_command.CommandType = commandType;
}
public void AddParameter(IDbDataParameter parameter)
{
if (parameter == null)
{
return;
}
_command.Parameters.Add(parameter);
_containerParameters[parameter.ParameterName] = parameter;
}
public void AddParameter(string parameterName, object value)
{
AddParameter(_session.CreateParameter(parameterName, value));
}
public void AddParameter(string parameterName, object value, ParameterDirection direction)
{
AddParameter(_session.CreateParameter(parameterName, value, direction));
}
public void AddParameter(string parameterName, object value, ParameterDirection direction, int size)
{
AddParameter(_session.CreateParameter(parameterName, value, direction, size));
}
public DataTable ExecuteDataTable()
{
var reader = ExecuteReader();
var table = new DataTable("Data");
table.Locale = CultureInfo.InvariantCulture;
table.Load(reader);
return table;
}
public int ExecuteNonQuery()
{
var nonQuery = _command.ExecuteNonQuery();
ClearCommand();
return nonQuery;
}
public object ExecuteScalar()
{
var scalar = _command.ExecuteScalar();
ClearCommand();
return scalar;
}
public DataRow ExecuteSingleRow()
{
var reader = _command.ExecuteReader(CommandBehavior.SingleRow);
DataRow row = null;
using (var table = new DataTable("Data"))
{
table.Locale = CultureInfo.InvariantCulture;
table.Load(reader);
if (table != null && table.Rows.Count != 0)
{
row = table.Rows[0];
}
}
ClearCommand();
return row;
}
public T ExecuteScalar<T>()
{
var scalar = ExecuteScalar();
return (T)scalar;
}
public IDataReader ExecuteReader()
{
var reader = _command.ExecuteReader();
ClearCommand();
return reader;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void ClearCommand()
{
if (_command == null)
{
return;
}
_command.Dispose();
_command = null;
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
ClearCommand();
}
}
}