Exemplo n.º 1
0
        /// <summary>
        /// you can supply your own SQL object and force the audit to go to that database.  Used by GT
        /// </summary>
        /// <param name="status"></param>
        /// <param name="CompletionMsg"></param>
        /// <param name="SQL"></param>
        /// <returns></returns>
        public long Save(CompletionStatus status, string CompletionMsg, SqlUtil SQL)
        {
            //save it - just in case of recovery
            mCompletionStatus = status;
            mCompletionMsg = CompletionMsg;

            string sql;

            SqlParameterCollection oParameters = new SqlCommand().Parameters;
            long ID = -1;

            oParameters.AddWithValue("@iDataType", DataUtils.DBNullIfNull(mDataType));
            oParameters.AddWithValue("@iDataID", DataUtils.DBNullIfNull(mDataID));
            oParameters.AddWithValue("@iCorrespondenceID", DataUtils.DBNullIfNull(mCorrespondenceID));
            oParameters.AddWithValue("@iEventID", DataUtils.DBNullIfNull(mEventID));
            oParameters.AddWithValue("@iEventDescription", DataUtils.DBNullIfNull(mEventDescription));
            oParameters.AddWithValue("@iCompletionMsg", DataUtils.DBNullIfNull(CompletionMsg));
            oParameters.AddWithValue("@iCompletionStatus", DataUtils.DBNullIfNull(status));
            if (mLogTime == DateTime.MinValue)
                mLogTime = DateTimeUtility.ServerDateNoRefresh();

            oParameters.AddWithValue("@iTime", mLogTime);

            sql = "set ansi_warnings off;INSERT INTO AuditHeader (DataType,DataID,CorrespondenceID,EventID,EventDescription,CompletionMsg,CompletionStatus,LogTime)"
                + " VALUES (@iDataType,@iDataID,@iCorrespondenceID,@iEventID,@iEventDescription,@iCompletionMsg,@iCompletionStatus,@iTime)";
            sql += "; select @@IDENTITY as ID";

            DataTable oTable;
            try {
                oTable = SQL.ExecuteSingleResultSetSQLQuery(sql, oParameters);
            } catch (Exception x) {
                if (mRecover && SqlRecover.SaveForRecovery(x, true, this, "auditheader"))
                    return 0;
                else
                    throw new Exception("sql error", x);
            }

            if (oTable.Rows.Count == 0)
                return -1;

            ID = DataUtils.LongZeroIfNull(oTable.Rows[0]["ID"]);
            if (ID < 1)
                return -1;

            if (mFieldChanges != null) {
                string o;
                string n;
                oParameters = new SqlCommand().Parameters;
                foreach (string field in mFieldChanges.UpdateList.Keys) {
                    mFieldChanges.GetAuditValue(field, out o, out n);

                    oParameters.AddWithValue("@iAuditID", ID);
                    oParameters.AddWithValue("@iField", DataUtils.DBNullIfNull(field));
                    oParameters.AddWithValue("@iOldVal", DataUtils.DBNullIfNull(o));
                    oParameters.AddWithValue("@iNewVal", DataUtils.DBNullIfNull(n));

                    sql = "set ansi_warnings off;INSERT INTO AuditDetail(AuditID,Field, OldValue,NewValue) " +
                         " VALUES (@iAuditID,@iField, @iOldVal,@iNewVal);";

                    SQL.ExecuteNoResultSetSQLQuery(sql, oParameters);
                    oParameters.Clear();
                }
            }

            return ID;
        }