.net c# datatable和dataset对象详解
a、对于1.6.1之前的版本,获得命令的方式可以通过requestscope的idbcommand属性,但是1.6.1版本的idbcommand属性返回的是ibatisnet.datamapper.commands.dbcommanddecorator对象,您可以注释代码验证一下。
b、网上有些文章贴的方法返回的dbcommand对象都是对于拼接sql语句而言,没有实现获取存储过程的dbcommand(有参数无参数的都要考虑)。本文在原有资料的基础上,尝试着做出改进,目前支持sql语句和存储过程。
/// <summary>
/// 获取dbcommand,主要是针对存储过程
/// </summary>
/// <param name="sqlmapper"></param>
/// <param name="statementname"></param>
/// <param name="paramobject">参数</param>
/// <param name="dictparam">参数字段</param>
/// <param name="dictparmdirection">parameterdirection字典</param>
/// <param name="cmdtype"></param>
/// <returns></returns>
protected virtual idbcommand getdbcommand(isqlmapper sqlmapper, string statementname, object paramobject, idictionary dictparam, idictionary<string, parameterdirection> dictparmdirection, commandtype cmdtype)
{
if (cmdtype == commandtype.text)
{
return getdbcommand(sqlmapper, statementname, paramobject);
}istatement statement = sqlmapper.getmappedstatement(statementname).statement;
imappedstatement maps教程tatement = sqlmapper.getmappedstatement(statementname);
isqlmapsession session = new sqlmapsession(sqlmapper);if (sqlmapper.localsession != null)
{
session = sqlmapper.localsession;
}
else
{
session = sqlmapper.openconnection();
}requestscope request = statement.sql.getrequestscope(mapstatement, paramobject, session);
mapstatement.preparedcommand.create(request, session as isqlmapsession, statement, paramobject);
idbcommand cmd = session.createcommand(cmdtype);
cmd.commandtext = request.idbcommand.commandtext;
if (cmdtype != commandtype.storedprocedure || dictparam == null)
{
return cmd;
}
foreach (dictionaryentry de in dictparam) //存储过程
{
string key = de.key.tostring();
idbdataparameter dbparam = cmd.createparameter();
dbparam.parametername = key;
dbparam.value = de.value;if (dictparmdirection != null && dictparmdirection.containskey(key))
{
dbparam.direction = dictparmdirection[key]; //parameterdirection
}
cmd.parameters.add(dbparam);
}
return cmd;
}
首页 1 2 3 末页