TS ScalarQuery <TS>(DbManager db, Expression expr, object[] parameters) { var dispose = db == null; if (db == null) { db = new DbManager(); } try { var commands = SetCommand(db, expr, parameters, 0); IDbDataParameter idparam = null; if (SqlProvider.IsIdentityParameterRequired) { var sql = Queries[0].SqlQuery; if (sql.QueryType == QueryType.Insert && sql.Set.WithIdentity) { var pname = DataProvider.Convert("IDENTITY_PARAMETER", ConvertType.NameToQueryParameter).ToString(); idparam = db.OutputParameter(pname, DbType.Decimal); DataProvider.AttachParameter(db.Command, idparam); } } if (commands.Length == 1) { var ret = db.ExecuteScalar <TS>(); return(idparam != null ? (TS)idparam.Value : ret); } db.ExecuteNonQuery(); return(db.SetCommand(commands[1]).ExecuteScalar <TS>()); } finally { if (dispose) { db.Dispose(); } } }