views:

94

answers:

4

I am working on a content management system which is being sort of retrofitted onto an existing database, and the database has many many tables. There will be a staging database, where we will make changes and allow users to 'preview in place'. Then any changes have to be approved, and to publish them we will connect to a live version of the same database (same schema) and play-forward the captured changes.

I have found some code (called Doddle Audit) which, with some customization, is giving me great information about what is changing. I am able to get a list of all columns, before and after, for updates, inserts, and deletes. But what I would really like to have is the underlying SQL being run by SubmitChanges(). LinqToSql has to generate this, so why can't I have it?

I have googled around and looked at code involving SubmitChanges, mousing over stuff, and I can't seem to find it. Does anyone know of a way to obtain this?

+2  A: 

Linq To Sql Profiler. It does that and a whole lot more.

mxmissile
A: 

You could use SQL Server Profiler, not as good as Linq To SQL Profiler, but free.

To get the sql-statement in a string, you can do something like this:

using (var context = new MyDataContext()) 
{
   var query = from p in context.Persons
             select p;
   string sql = context.GetCommand(query).CommandText;


}
gautema
Well, I know that I could Profile, to see from a debugging point of view, what's going on. But I need something where I can, in code, trap what's being sent and store it. So it's not a debugging function.
Josh Warner-Burke
I have edited my answer with a bit more info
gautema
Right it is possible to get the SQL for a select. The issue is with updates/inserts/deletes. I have a set of changes and I'm auditing all of them and recording them. There is no 'query' per se.
Josh Warner-Burke
A: 

You can try this:

Console.WriteLine(context.GetCommand(query).CommandText);

Hope this helps.

Thanks,

Raja

Raja
In case anyone is wondering what I ended up doing...Linq to Sql is just not quite good enough for this project. I went with SubSonic 2.2, which I am very familiar with. And I developed a ChangeSet class which allows you to pass in records and querycommands and record the sql that is run on the database.The other upside to using subsonic here is the changeset class (which is defined in 2 partial classes, one custom code, one generated) can be serialized and stored in session. So in any given 'content area' M changes across N postbacks can be queued up into one 'changeset'.
Josh Warner-Burke
+2  A: 

Use the DataContext.Log property like this:

using(DataContext dc = new DataContext()){
  StringBuilder sb = new StringBuilder();
  dc.Log = new StringWriter(sb);
}

You will see the generated query in debug mode.

Devart
For debug scenarios it can be quite usefull to write a little Writer that uses Debug.WriteLine etc :)
cyberzed
That is pretty cool, definitely good to know... but I just debugged my test scenario where I'm doing an update and a few deletes (child records) and inserts, and although that stringbuilder showed me the select commands, it didn't show any of the insert/update/delete commands :(
Josh Warner-Burke