// 10 20 30 // 123456789012345678901234567890123456 // 61FFB6F0-AE23-4588-A673-BA6674A7EB17 public static void LogActivity(string pUserID, string pPageName, LogActivityCategory pCategory, string pEventName, long pTimeElapsed, string pDetails, DbTask pDbt) { StringBuilder sb = new StringBuilder(); sb.AppendLine("INSERT INTO [ActivityLog] ([UserID],[PageName],[Category],[EventName],[TimeElapsed],[Details],[ConnString]) "); sb.AppendLine("VALUES ("); sb.Append(DbUtility.DelimitText(pUserID) + ","); sb.Append(DbUtility.DelimitText(pPageName) + ","); sb.Append("'" + pCategory.ToString() + "',"); sb.Append("'" + pEventName + "',"); sb.Append(DbUtility.DelimitNonText(pTimeElapsed) + ","); sb.Append(DbUtility.DelimitText(pDetails) + ","); sb.Append(DbUtility.DelimitText(pDbt.ConnectionName)); sb.Append(")"); string queryString = sb.ToString(); var dbt = new DbTask("Aw_ActivityLog", queryString); dbt.DbTaskSummaryOn = false; DbWorker.ExecuteQuery(ref dbt, delegate(int pCount, Exception pExc) { if (pExc != null) { Debug.WriteLine("Error Writing ActivityLog"); Debug.WriteLine("Sql="); Debug.WriteLine(queryString); Debug.WriteLine(DBMaria.PrettyException(pExc)); } }); }
private static void SqlParameterSetup(ref DbTask dbt) { SqlConnection conn = ConnectionOpen(dbt.ConnectionName, ref dbt); if (ConnectionNotOpenHappened(ref dbt, MethodBase.GetCurrentMethod())) { return; } dbt.SqlCmd.Connection = conn; if (dbt.Sql != null) { dbt.SqlCmd.CommandText = dbt.Sql; dbt.SqlCmd.CommandType = DbUtility.IsAdHocSql(dbt.Sql) == false ? CommandType.StoredProcedure : CommandType.Text; } bool parametersExist = true; var parmSP = dbt.QueryParameters; if (parmSP == null) { parametersExist = false; } if (parmSP.Length == 0) { parametersExist = false; } if (!parametersExist) { return; } if (parmSP.Length % 2 != 0) { throw new Exception("parameters are not paired"); } for (int countParams = 0; countParams < parmSP.Length; countParams += 2) { SqlParameter param = dbt.SqlCmd.CreateParameter(); param.ParameterName = parmSP[countParams].ToString(); param.Direction = ParameterDirection.Input; param.Value = parmSP[countParams + 1]; dbt.SqlCmd.Parameters.Add(param); } }
public static void DebugSql(DbTask pDbt) { //if (ConnectionNotOpenHappened(ref pDbt,MethodBase.GetCurrentMethod())) return; var DBDeets = new DbTaskSummary { QueryRaw = pDbt.Sql }; //if (pDbt.Sql.ToLower().IndexOf("insert into [activitylog]") > -1) return; if (pDbt.Sql != null & DbUtility.IsAdHocSql(pDbt.Sql)) { DBDeets.QueryType = TypeOfQuery.AdHocSQL; } if (DBDeets.QueryType == TypeOfQuery.AdHocSQL) { DBDeets.QueryRaw = pDbt.Sql; DBDeets.QueryPrettyFormatted = DBMaria.PrettySQL(pDbt.Sql); DBDeets.QueryAsExecutableText = DBDeets.QueryPrettyFormatted; } else { DBDeets.QueryType = TypeOfQuery.SProc; // TODO function in Utility StringBuilder sbSprocCall = new StringBuilder(); sbSprocCall.Append("EXEC " + pDbt.SqlCmd.CommandText + " "); foreach (SqlParameter parm in pDbt.SqlCmd.Parameters) { sbSprocCall.AppendFormat(" {0}={1}, ", parm.ParameterName, DbUtility.DelimitObject(parm.Value)); } string sprocCallExpanded = sbSprocCall.ToString(); // remove the trailing comma if (sprocCallExpanded.EndsWith(", ")) { sprocCallExpanded = sprocCallExpanded.Substring(0, sprocCallExpanded.Length - 2); } DBDeets.QueryPrettyFormatted = sprocCallExpanded; DBDeets.QueryAsExecutableText = DBDeets.QueryPrettyFormatted; } if (pDbt.IsSqlCommand) { DBDeets.QueryType = TypeOfQuery.CommandObject; DBDeets.QueryAsExecutableText = pDbt.SqlCmd.TSqlFromFromCommand(); } DBDeets.MthdBsPretty = DBMaria.PrettyMethodBase(pDbt.MethodBase); // timing transfer DBDeets.MilliSecondsElapsed = pDbt.SW.ElapsedMilliseconds; DBDeets.TimingPretty = DBMaria.PrettyDBtTiming(pDbt); if (pDbt.DbQueryType == DbQueryType.DataReader || pDbt.DbQueryType == DbQueryType.DataTable) { // TODO: may need tweaking with multiple result sets DBDeets.BlackBoxAsText = DBMaria.PrettyDataTable(pDbt.LogCaptureDataset.Tables[0], Schema: pDbt.Schema); DBDeets.BlackBox = pDbt.LogCaptureDataset; } DBDeets.RawExceptionQuery = pDbt.ExceptionQuery; DBDeets.RawExceptionDelegate = pDbt.ExceptionDelegate; DBDeets.ExceptionPretty = string.Empty; if (DBDeets.RawExceptionQuery != null) { DBDeets.ExceptionPretty = "QueryException=" + DBMaria.PrettyException(pDbt.ExceptionQuery); } if (DBDeets.RawExceptionDelegate != null) { DBDeets.ExceptionPretty += System.Environment.NewLine + "DelegateException=" + DBMaria.PrettyException(pDbt.ExceptionDelegate); } Debug.WriteLine(DBMaria.PrettyDBDeets(DBDeets)); }