- public static int QueryForCount(string statementName, object paramObject) {
- ISqlMapper mapper = Mapper.Instance();
- IMappedStatement statement = mapper.GetMappedStatement(statementName);
- IDbCommand cmd = null;
- try {
- if (!mapper.IsSessionStarted) {
- mapper.OpenConnection();
- }
- RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramObject, mapper.LocalSession);
- statement.PreparedCommand.Create(scope, mapper.LocalSession, statement.Statement, paramObject);
- cmd = scope.IDbCommand;
- cmd.Connection = scope.Session.Connection;
- cmd.CommandText = string.Format("select count(*) c from ({0}) t ", cmd.CommandText);
- using (IDataReader dr = cmd.ExecuteReader()) {
- dr.Read();
- return dr.GetInt32(0);
- }
- } finally {
- if( cmd != null && cmd.Connection != null && cmd.Connection.State != ConnectionState.Closed)
- cmd.Connection.Close();
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2204201512357.html
来源: http://www.codesnippet.cn/detail/2204201512357.html