Argentini.Halide.H3DataRow.Exec C# (CSharp) Method

Exec() public method

Execute the specified stored procedure. Parameters used in the stored procedure must match column names in the currently loaded data row. If so, this method will automatically send the appropriate data to the procedure.

This method is useful for saving the record back to the database, marking a record for deletion, and more.

public Exec ( String spName, System.Boolean sendAll, System.Boolean returnsValue ) : String
spName String Name of the stored procedure.
sendAll System.Boolean Send all columns to the stored procedure as parameters, /// instead of auto-detecting which parameters the procedure wants. This is /// useful when you construct the class using a reader that has disabled the /// feature whereby it loads the schema.
returnsValue System.Boolean If the stored procedure returns a value, set to true, /// otherwise, set to false. When false, the number of rows affected is returned as a string.
return String
        public String Exec(String spName, Boolean sendAll, Boolean returnsValue)
        {
            String returnVal = "";

            SqlConnection con = new SqlConnection(ConnectionString);
            SqlCommand cmd = new SqlCommand(spName, con);
            cmd.CommandType = CommandType.StoredProcedure;

            String[,] _params = H3Sql.GetSprocParams(spName, ConnectionStringName);

            try
            {
                if (sendAll)
                {
                    for (Int32 x = 0; x < this.FieldCount; x++)
                    {
                        String ParamVal = null;

                        if (this[x].Value != null)
                        {
                            ParamVal = H3Text.HtmlEncodeSymbols(this[x].Value.ToString());
                        }

                        cmd.Parameters.AddWithValue("@" + this[x].ColumnName, (ParamVal == null ? (object)DBNull.Value : (object)ParamVal));
                    }
                }

                else
                {
                    for (Int32 x = 0; x < _params.Length / 2; x++)
                    {
                        String ParamVal = null;

                        if (ColumnExists(_params[x, 0].Replace("@", "")))
                        {
                            if (this[_params[x, 0].Replace("@", "").ToLower()].Value != null)
                            {
                                ParamVal = H3Text.HtmlEncodeSymbols(this[_params[x, 0].Replace("@", "").ToLower()].Value.ToString());
                            }
                        }

                        cmd.Parameters.AddWithValue(_params[x, 0], (ParamVal == null ? (object)DBNull.Value : (object)ParamVal));
                    }
                }

                con.Open();

                if (returnsValue)
                {
                    returnVal = cmd.ExecuteScalar().ToString();
                }

                else
                {
                    returnVal = cmd.ExecuteNonQuery().ToString();
                }

                con.Close();
            }

            catch (Exception e)
            {
                throw new Exception("Halide.H3DataRow Error: If your stored procedure does not return a value, be sure to use the overloaded version of Exec which has a parameter to specify this; " + e.ToString());
            }

            return returnVal;
        }

Same methods

H3DataRow::Exec ( String spName ) : String
H3DataRow::Exec ( String spName, System.Boolean sendAll ) : String