///-------------------------------------------------------------------------------- /// <summary>This method executes the command and returns the first column of the /// first row of the results set. All other columns and rows are ignored.</summary> /// /// <param name="sprocName">Name of the stored procedure.</param> /// <param name="sprocParams">The input stored procedure parameter values.</param> /// /// <returns>The results of the operation.</returns> ///-------------------------------------------------------------------------------- public object ExecuteScalar(string sprocName, NameObjectCollection sprocParams) { DbCommand command = null; object scalar = null; try { // build the command and execute scalar for simple results command = SetupSprocCommand(sprocName, sprocParams); scalar = _database.ExecuteScalar(command); // put output/return values into sproc params foreach (DbParameter loopParameter in command.Parameters) { sprocParams[SqlHelper.GetParameterNameFromSqlParameterName(loopParameter.ParameterName)] = loopParameter.Value; } } catch (Exception ex) { bool reThrow = ExceptionHandler.HandleException(ex); if (reThrow) { throw; } } return(scalar); }
/// <summary> /// for getting integer result form database /// </summary> /// <param name="dom"></param> /// <returns></returns> public static int ExecuteScalarforInt(XDocument dom, string ConnectionStr) { try { Database db = new Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase(ConnectionStr); DbParameter parameter; DbCommand cmd; IEnumerable <XElement> elems; IEnumerable <XElement> elem_list; string Procname = GetProcName(dom); cmd = db.GetStoredProcCommand(Procname); SetCommandTimeOut(cmd); elems = dom.Descendants(); elem_list = from elem in elems select elem; foreach (XElement element in elem_list) { parameter = cmd.CreateParameter(); if (element.Name.ToString() != "BIC" && element.Name.ToString() != "XsdName" && element.Name.ToString() != "procName" && element.Name.ToString() != "pERRORXML") { parameter.ParameterName = "@" + element.Name.ToString(); parameter.Value = element.Value; cmd.Parameters.Add(parameter); } } int retval = Convert.ToInt32(db.ExecuteScalar(cmd)); return(retval); } catch (Exception ex) { throw ex; } }