none
Access to DbQuery parameters for logging

    Question

  • Is there any way to get the parameters collection used by DbQuery when passing the sql to sp_executesql?  DbQuery.ToString() shows them as @p__linq__0 (and up), but I can't figure out what the values are programatically.

    -Thanks

    Wednesday, March 30, 2011 4:04 PM

Answers

  • Hi,

    As far as I know there isn't any way to retrieve the parameters programatically, the ObjectQuery which DbQuery actually uses does have it, but it's not exposed either.

     


    --Rune
    Wednesday, March 30, 2011 6:13 PM

All replies

  • Hi,

    As far as I know there isn't any way to retrieve the parameters programatically, the ObjectQuery which DbQuery actually uses does have it, but it's not exposed either.

     


    --Rune
    Wednesday, March 30, 2011 6:13 PM
  • Hi danagutride,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 
     
    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 05, 2011 3:20 AM
    Moderator
  • Hi Alan,

    Thanks for following up.  I was assuming there was no other way to get the parameters per Rune's response.  If this correct, we can mark this as answered.

    -Dana

    Tuesday, April 05, 2011 12:25 PM
  • Hi Dana, 

    I don't know if this will still be of any utility for you, but I managed to get a trace string with parameters from a DbQuery using reflection to access its internal ObjectQuery:

     

     

      public static string ToTraceString<T>(this DbQuery<T> query)
      {
       var internalQueryField = query.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(f => f.Name.Equals("_internalQuery")).FirstOrDefault();
    
       var internalQuery = internalQueryField.GetValue(query);
    
       var objectQueryField = internalQuery.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(f => f.Name.Equals("_objectQuery")).FirstOrDefault();
    
       var objectQuery = objectQueryField.GetValue(internalQuery) as ObjectQuery<T>;
    
       return objectQuery.ToTraceStringWithParameters();
      }
    
      public static string ToTraceStringWithParameters<T>(this ObjectQuery<T> query)
      {
       string traceString = query.ToTraceString() + "\n";
    
       foreach (var parameter in query.Parameters)
       {
        traceString += parameter.Name + " [" + parameter.ParameterType.FullName + "] = " + parameter.Value + "\n";
       }
    
      
    
       return traceString;
      }
    

     

    Hope it helps


    Wednesday, June 08, 2011 7:14 PM
  • Dana,

    Thank you very much for your workaround. It is very helpful for us.

    Wednesday, October 24, 2012 10:09 AM