Is there any tool to see the queries run against the database? - asp.net

Is there any tool that will inspect either asp.net or sql server and report all the queries that are run against the database? The reason I ask is I am using Linq for a project and want to double check what its actually doing for each page.
Ideally I'd like to view a page in a browser and have a report of all the queries that were run to create that page.
I know I can view the SQL it runs for individual queries using debugging/breakpoints, and I know about LinqPad, but I'm afraid Linq is making several more queries on its own to get related data that I may not be directly aware of. Is there anything (tool/program/report/etc) like what I described? Thanks!
EDIT: Is there any FREE tool that can do this? I am using Sql Server 2008 Express and don't have SQL Profiler unfortunately.

Absolutely, There is a SQL tool called SQL Profiler. It does require elevated database permissions in order to run profiler.
There is a decent tutorial on how to run Profiler on TechRepublic.
Another option out there is the NHibernate Profiler. I know that it is not as "free" as SQL Profiler, have never used it, but the screen shots for it look pretty cool.

Profiler is the best tool of them all for this but it can be expensive in inexperienced hands.
You can also try to do "exec sp_who" and then a "dbcc inputbuffer (111)" - just put the process id in the place of 111.

SQL Profiler does that.

Since you are using SQL Server Express, how about this tool?
Profiler for Microsoft SQL Server 2005/2008 Express Edition
Microsoft SQL Server family includes
free Express edition, that is fully
functional, however has some
disappointing limitations which
prevent from using it in development
process. One of them is absense of
profiling tools, standard SQL profiler
is not included. However, now you have
an ability to use express edition for
tuning your system. SQL Server Express
Edition Profiler provides the most of
functionality standard profiler does,
such as choosing events to profile,
setting filters, etc. By now there are
no analogue free tools.
Download Here

A quick and dirty way to log LINQ to SQL queries in ASP.NET is this (assuming a Northwind.Dbml):
NorthwindDataContext context = new NorthwindDataContext();
context.Log = Response.Output;
This will write all queries to the response stream. Nasty, but handy for instant gratification without need for debuggers or profilers etc.

For the LINQ to SQL queries specifically, you can also use the DataContext.Log property to output the queries to a TextWriter, so you can do things like write to debugger output window or (as in my use) to log4net.
These links might help:
Log to debugger output window, file, or memory
Log to log4net
Won't cover the stuff not generated by L2S, so this may not be the end all solution for you... but I've found it helpful.

This one is only free for the first 45 days, but it gives you runtime profiling/logging with a bunch of filter options, SQL Server query execution plan logging etc. Built specifically for profiling L2S apps:
http://www.huagati.com/L2SProfiler

Related

Should I consider migrating from SQL Server to Oracle for my ASP.NET applications?

We're upgrading our systems to support clustering and auto failover features. Our business runs .NET 4 applications, web apps and services on SQL Server Express. We can upgrade to SQL Server Standard, but the cost has motivated us to consider other options. Is it a legitimate option to integrate our .NET data layer with ODP.NET? After searching, I have seen a tendentious statement or two in the negative (viz) and yet it would seem that people are doing it anyway. What development features in the Visual Studio IDE will we lose? Thanks for your help!
Well, I'm now working since 20+ years with Oracle and MS SQL Server, having done a lot of projects. Some projects are running now more than 10 years, with all the updates, maintenance and so on.
My quick answer is: Stay with MS SQL Server. Go to Oracle only, if you have really GOOD TECHNICAL reason, or if you are planning really an ENORMOUS database, and if you have enough staff to handle all thge administration.
The main reason is that SQL Server is much easier to maintain; and it also integrates greatly into the Microsoft environment.
Oracle, in contrast, has a steep learning curve. The handling of Oracle is much more "manual" then MS SQL Server. Well, that's also a good thing, because you are in control of every small detail, but it also means that you need to learn a lot; or you need to pay experts. And it is not so easy to find people who really know what to do.
I really like both Systems, but for a rule of thumb, I normally suggest to use MS SQL Server.
I've been using .net with Oracle for years, and migrate away from it whenever the option is available.
If all your database code is in stored procs and you call it though the codebehind or a library and you use ansi sql your migration from ms sql to oracle will be fairly painless.
If you use TableAdapters, they re-write any sql you put in to the oldschool oracle 8 syntax like table1,table2,table3 then have a big where clause to do the join conditions. There's also some weird bugs where sometimes sql that runs fine over in SQL Developer won't work in the TableAdapters.
If you use Entity Framework migration should be pretty easy, but the MS SQL driver is much better then the Oracle one. There have been several queries I couldn't do though EF in oracle because of some of the various errors with the current driver.
If you need more info let me know.
Also if Cost is the main reason to consider migration, why not go with mysql?
Since you are already working in MS SQL, you must be habitual of the way it work, be it entity framework or any other data execution. Yes offcource, microsoft has very high license rates for it. But if you want to move to any other database, it is perfectly alright. I have personally used MS SQL and MySQL both. Initially you might face some syntax related issues, but do remember that logic remains the same for fetching and saving the data. Further it gives a benefit that you got to learn a new language and that too at the expense of far less money.

Sql Server Profiler Free Tool

I need A free Sql 2008 R2 Profiler As I Can not Use Sql Server Included Sql Profiler for some reasons. If there are any free profiling tools or something like this - it will be good
To run traces like SQL Server Profiler, even with AnjLab's replacement, you still need server-side permissions to run it.
In SQL Server 2000, you had to be a member of the sysadmin
In SQL Server 2005, you can GRANT ALTER TRACE to allow non-sysadmins to run either profiling tool
Learn about profiling background, Events, traces and Extended events and in your case you will not need a profiler, since you'll be able to do all there tricks by proper querying the sql server itself.
Take a look also on Dynamic Management Views and Functions
And finally there is one free tool available sqlexpressprofiler and also here is one more from Google code named AnjLab
Some solutions provided by Idera
Why can't you use the included one, does it error? I never found anything which came close to the included one apart from AnjLab Profiler. The only annoyance with it is you can't export traces for tuning.
Anjlab Sql Profiler was great, and still is if you can get your hands on the old free version.
Now there is Datawizard SQL Performance Profiler which has a free trial, but is commercial.
There is an open source SQL Profiler by AnjLab which I'm using regularly:
[AnjLab SQLProfiler][1]

How to analyse LINQ queries

I swear I've seen how to do this before, but now that I actually need to do it I can't remember where I saw it at. I need two different things --
1) to see the actual SQL query that is generated by a LINQ query and
2) when the SQL query actually hits the database to do whatever (CRUD operations)
Is there a tool that will allow me to do this?
EDIT:
Sorry, should have given more detail.
-- LINQ to Entities is what I'm using.
-- Also, I don't have admin rights on our instance of SQL Server, so I can't use SQL Profiler. I could always call the DBA and have them do it for me, but it's a hassle. I should have mentioned that and I apologize. What I really want is a tool that I can use on my own box that will allow me to see when a LINQ query hits the database while I am in debug mode (debugging and stepping through the code).
Try using SQL Profiler. It's great for seeing what LINQ to SQL is generating.
SQL Profiler is a graphical tool that allows system administrators to monitor events in an instance of Microsoft® SQL Server™. You can capture and save data about each event to a file or SQL Server table to analyze later. For example, you can monitor a production environment to see which stored procedures are hampering performance by executing too slowly.
LINQPad is also a great tool for writing linq and sql statements for testing.
LINQPad compiles your queries using .NET's CSharpCodeProvider (or VBCodeProvider). Because C# and VB are statically typed, any database objects that you reference need the backing of a typed DataContext. For performance, LINQPad builds typed DataContexts on the fly using Reflection.Emit rather than generating and compiling source code. It uses LINQ to SQL rather than Entity Framework because LINQ to SQL is an order of magnitude faster in building the metamodel when instantiated.
The .ToString() on the IQueryable for LINQ to SQL will show you the query.
var myquery = from x in dbcontext.MyTable
where x.Prop1 == someValue
select new {
value1 = x.prop1,
value2 = 5,
};
var sqlstring = myquery.ToString(); //<= look in this string
There are a few tools that can help.
L2SProf (Linq2Sql Profiler) is a for pay tool that can do a lot to help you see and optomize your queries. http://l2sprof.com/
LinqPad is a great tool for write linq to sql queries and seeing the sql that comes out. It also lets you write c# code just to experiment with things. It's a great coding scratch pad. There is a free version, but you get extra features if you pay. http://www.linqpad.net/
Otherwise, there are instructions here for viewing the generated sql for free with builtin stuff: http://msdn.microsoft.com/en-us/library/bb386961.aspx
Using Visual Studio 2010, turn on IntelliTrace and you'll see every request for LINQ To SQL, LINQ To Entities and generally everything that ultimately uses ADO.NET.
Are you looking for the DataContext.Log property? (I'm assuming you're using LINQ to SQL.)
You could create a TextWriter which dumped a stack trace and timestamp every time it was written to, which would give you the timing information.
depends entirely on the ORM. Most ORMs offer some sort of Log property you can plug a TextWriter or so into. Consult the docs for ORM to learn more.
In general: when you use some sort of SaveChanges method. This ties in with (1), the logger will provide you with the information.
Here is a solution, it is very complex: http://blogs.msdn.com/b/jkowalski/archive/2009/06/11/tracing-and-caching-in-entity-framework-available-on-msdn-code-gallery.aspx

The best place to put SQL in a ASP.NET app is...?

I am creating a ASP.NET application that is pretty much self contained, it will be pushed to different hosts and as a result will include an embedded database engine - I did look at SQLite but that can not run under medium trust and that could be an issue, so it looks like it is going to have to be Microsoft SQL Server Compact Edition 4.
Microsoft SQL Server Compact Edition does not support stored procedures and because of such I'm wondering where the best place to include the SQL code would be, taking into account future edits, etc.
So far I think that my options are either directly embedded in the code as a String or Stringbuilder type (which can get messy for advanced queries), or maybe in SQL files stored in the App_GlobalResources folder - the second option is something something that I have only just considered, but think that that would be a pretty good option for containment and future editing - does anyone have any other suggestions?
Regards, Nathan.
You can store parameterized queries in your resource file.
You can use MS SQL Express. It is free, it has stored procedures, but it has 4GB database size limit (increased to 10 GB for SQL Server Express 2008 R2).
For more information read this.
I think it's better to use some ORM tool or LINQ to SQL, than store native SQL strings.
Like other have said an ORM, Entity Framework or Linq to SQL is probably better than inline SQL, however as your question wasn't asking about any of these I would suggest storing you SQL files as embedded resources. Others have mentioned resource resource files but I feel that resource files are difficult to use and you do not have an nice IDE to develop within. Having embedded .SQL files should be easier to version control, use within Visual Studio, allow you to keep your queries as closely as possible to your data access components and make it difficult to change once in a production environment.

SQL Server console like control for ASP.NET

I'm deploying a web site and I need to run large TSQL scripts contained in a single file in a production server where I don't have full access to SQL Server console and I can't connect remotely. The scripts is a mixed of table, stored procedures and views creations. All I can do is to run 1 group of TSQL sentences, like the ones for a stored procedure.
I have two options: to parse the file manually looking for GO's sentences and run each block of sentences before that GO, or to do the same task but with a tool. Using a tool I will be very fast doing the task, but I don't know any tool such that.
Do you know any tool that I can use?
I think it must be something like a control, with an editor where I will paste or load the scripts to run, and it will be able to parse and run them in sequence, like the Microsoft SQL Server Management usually does.
You could check out some articles on CodeProject on the topic and maybe use one of those tools / component for your needs?
Universal Database Admin for ASP.NET and SQL Server (Reloaded)
ASP.NET Database Admin Control
Web SQL Utility
Most of those come with full source and could also serve you as a starting point for a custom version of your own.
Marc
you got many options
usually i create stored procedure in sql management studio then save it as string, then use linq2sql to execute the stored procedure, works very well.
use sql server smo object where you can really do mostly many things like creating DBs, tables, it is really cool, i create 1 page on my site and use it to update DB with it.
here is a good link for that
http://www.sqldbatips.com/showarticle.asp?ID=34
you can use Redgate SQL data compare, and Compare. they rock really to synchronize DBs, it have saved me a lot of time and it is super easy, highly recommended really.
hope this helps.
If those don't work, it's pretty easy to write such a tool yourself. Just use Regex.Split to split the text on the GO lines, then loop, calling ExecuteNonQuery for each section.

Resources