/// <summary> /// Return a <see cref="System.Object"/> /// based on the DBMS query. /// </summary> /// <param name="connection">The object implementing <see cref="System.Data.IDbConnection"/>.</param> /// <param name="query">The SELECT SQL statement.</param> /// <param name="parameterCollection">The parameters.</param> public static object GetObject(IDbConnection connection, string query, IEnumerable parameterCollection) { if (connection == null) { throw new ArgumentNullException("connection", "The Common Connection object is null."); } if (string.IsNullOrEmpty(query)) { throw new ArgumentException("The DBMS query was not specified."); } object o = null; using (IDbCommand cmd = connection.CreateCommand()) { if (parameterCollection != null) { IDataParameter[] paramArray = CommonParameterUtility.GetParameters(cmd, parameterCollection); foreach (IDataParameter p in paramArray) { cmd.Parameters.Add(p); } } cmd.CommandType = CommandType.Text; cmd.CommandText = query; o = cmd.ExecuteScalar(); } return(o); }
/// <summary> /// Returns an instance of <see cref="System.Data.IDataReader"/> /// based on the object implementing <see cref="System.Data.IDbConnection"/>. /// </summary> /// <param name="connection">The object implementing <see cref="System.Data.IDbConnection"/>.</param> /// <param name="query">The SELECT SQL statement.</param> /// <param name="parameterCollection">A list of parameters.</param> /// <param name="timeout">Command timeout in seconds.</param> /// <param name="ambientTransaction">The ambient <see cref="System.Data.IDbTransaction"/> implementation.</param> public static IDataReader GetReader(IDbConnection cnn, string query, IEnumerable parameterCollection, int timeout, IDbTransaction ambientTransaction) { if (cnn == null) { throw new ArgumentNullException("connection", "The implementing Connection object is null."); } if (string.IsNullOrEmpty(query)) { throw new ArgumentException("The DBMS query was not specified."); } IDataReader r = null; using (IDbCommand selectCommand = cnn.CreateCommand()) { var parameters = CommonParameterUtility.GetParameters(selectCommand, parameterCollection); selectCommand.CommandType = (query.ToLower().Contains("select")) ? CommandType.Text : CommandType.StoredProcedure; selectCommand.CommandText = query; selectCommand.CommandTimeout = timeout; if (parameters != null) { foreach (IDataParameter p in parameters) { selectCommand.Parameters.Add(p); } } if (ambientTransaction != null) { selectCommand.Transaction = ambientTransaction; } r = selectCommand.ExecuteReader(CommandBehavior.Default); } return(r); }
/// <summary> /// Return a <see cref="System.String"/> /// based on the DBMS query. /// </summary> /// <param name="connection">The object implementing <see cref="System.Data.IDbConnection"/>.</param> /// <param name="query">The SQL statement.</param> /// <param name="parameterCollection">The parameters.</param> /// <param name="returnXmlForEmptySet">When <c>true</c> <see cref="Songhay.Xml.XmlUtility.GetInternalMessage(string,string[])"/> is used for empty sets.</param> public static string GetString(IDbConnection connection, string query, IEnumerable parameterCollection, bool returnXmlForEmptySet) { if (connection == null) { throw new ArgumentNullException("connection", "The Common Connection object is null."); } if (string.IsNullOrEmpty(query)) { throw new ArgumentException("The DBMS query was not specified."); } string s = null; if (connection.State != ConnectionState.Open) { throw new DataException(string.Format("The Connection to the DBMS is not open.")); } using (IDbCommand cmd = connection.CreateCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandText = query; if (parameterCollection != null) { IDataParameter[] parameters = CommonParameterUtility.GetParameters(cmd, parameterCollection); foreach (IDataParameter p in parameters) { cmd.Parameters.Add(p); } } object o = cmd.ExecuteScalar(); if (o is string) { s = o.ToString(); } if (string.IsNullOrEmpty(s) && returnXmlForEmptySet) { string[] sa = new string[] { "The query executed against the DBMS connection returned an empty set." }; s = XmlUtility.GetInternalMessage("The query Returned No Data", sa); } } return(s); }
/// <summary> /// Executes a SQL sqlStatement for the current instance of <see cref="IDbConnection"/>. /// </summary> /// <param name="connection">The object implementing <see cref="IDbConnection"/>.</param> /// <param name="ambientTransaction">An instance of the explicit, server <see cref="IDbTransaction"/>.</param> /// <param name="sqlStatement">The SQL statement.</param> /// <param name="parameterCollection">The parameters.</param> /// <returns>Returns the number of records affected.</returns> public static int DoCommand(IDbConnection connection, IDbTransaction ambientTransaction, string sqlStatement, IEnumerable parameterCollection) { if (connection == null) { throw new ArgumentNullException("connection", "The implementing Connection object is null."); } if (string.IsNullOrEmpty(sqlStatement)) { throw new ArgumentException("The DBMS SQL Statement was not specified."); } int i = 0; using (IDbCommand cmd = connection.CreateCommand()) { if (ambientTransaction != null) { cmd.Transaction = ambientTransaction; } cmd.CommandType = CommandType.Text; cmd.CommandText = sqlStatement; if (parameterCollection != null) { IDataParameter[] paramArray = CommonParameterUtility.GetParameters(cmd, parameterCollection); foreach (IDataParameter p in paramArray) { cmd.Parameters.Add(p); } } i = cmd.ExecuteNonQuery(); } return(i); }