A Free SQL Server Management Studio Add-In To Help Make You More Productive

by Robert 29. September 2011 01:42

So lately, I've been tasked with working with several databases where the number of tables and views within the databases number into the low thousands.  As you can imagine when working with any kind of long list, the amount of time spent scrolling and searching for the objects you are interested in proves to be a pain point after a while.  In SQL Server Management Studio (SSMS), you do have the ability to set filters, but they aren't persistent and they are limited in value.  For my purposes, the filters helped, but not nearly as much as I would have liked.  So, my first thought was that I wished there was a way that I could create a persistent "folder" structure.  After a quick search of the web, I wasn't able to find anything out there that was what I was looking for, but I did find code samples on creating SSMS add-ins.  I was pretty much resigned to trying to write an add-in myself to do what I wanted to do in SSMS.

After discussing my thoughts about this with a co-worker, he made me aware of a free add-in (SQL Treeo) created by Jakob Dvorak that does a lot of what I was looking for. It allows me to create persistent "virtual" folders as a hierarchy to organize the database objects into a far, far more manageable interface.  As noted in his forum, the add-in is an alpha version and still a work in progress.  I've used it now for a month and give it positive feedback.  There are some bugs and there are a couple modifications that would be nice to have, but all up, it is a solid piece of development and well worth a try (Note: currently it is only available for SQL Server 2008 R2 client).  If you're working with hundreds of tables within a database, it may just make you more productive (as it has for me) and reduce your frustration with constantly having to scroll up and down to find a table or view.

LINQ Max Value When No Rows Are Returned

by Robert 8. July 2011 16:22

I ran into an issue today where I was trying to get the max value for a column using LINQ to SQL but was getting an exception when no rows were returned.  I have a table of "Questions" and one of its columns is "DisplayOrder", which is an int field that allows for sorting on the front end.  When a new Question is added, I want it to show up last in the sorted list, so I want to insert the new record with a DisplayOrder that is the current maximum value + 1 (or with a DisplayOrder of 1 if it will be the first record).  My first attempt at getting the maximum value was:

int? currentMaxDisplayOrder = dc.Questions.Where(q => !q.IsDeleted).Max(q => q.DisplayOrder);

This worked fine when there were existing records but threw an exception when there were no matching questions.  Searching the internet provided me with guidance for a solution.  With that, I changed my LINQ statement to:

int? currentMaxDisplayOrder = dc.Questions.Where(q => !q.IsDeleted).Select(q => (int?)q.DisplayOrder).Max();
....
// And the new record's DisplayOrder
question.DisplayOrder = currentMaxDisplayOrder.GetValueOrDefault(0) + 1;

And FYI...the LINQ statement above evaluates to the following SQL

SELECT MAX([t1].[value]) AS [value]
FROM (
    SELECT [t0].[DisplayOrder] AS [value], [t0].[IsDeleted]
    FROM [Question] AS [t0]
    ) AS [t1]
WHERE NOT ([t1].[IsDeleted] = 1)

Programatically Renaming IIS 7 Virtual Directories and Applications

by Robert 22. March 2011 16:48

Today, I came across a need to programmatically rename IIS 7 Virtual Directories and Applications using C#.   Searching the web, I found several examples to add them, but wasn't able to find any to rename them.   By exploring the class methods and modifying the examples accordingly, I was able to come up with what I was looking for.

Note: The ServerManager class is within the Microsoft.Web.Administration namespace - the dll to reference can be found in the system IIS directory (%WinDir%\System32\InetSrv).

Rename IIS 7 Virtual Directory

     ServerManager iisManager = new ServerManager();
     Application app = iisManager.Sites["SiteName"].Applications["/"];
     VirtualDirectory vdir = app.VirtualDirectories["/OrigVirtualDirectoryName"];
     if (vdir != null)
     {
          vdir.SetAttributeValue("path", "/NewVirtualDirectoryName");
          iisManager.CommitChanges();
     }

Rename IIS 7 Application

     ServerManager iisManager = new ServerManager();
     Application app = iisManager.Sites["SiteName"].Applications["/OrigApplicationName"];
     if (app != null)
     {
          app.SetAttributeValue("path", "/NewApplicationName");
          iisManager.CommitChanges();
     }

If you want to work with a remote server, just change the first line to be

     ServerManager iisManager = ServerManager.OpenRemote("RemoteServerName");

What's Taking Up All The Space On My Computer?

by Robert 3. March 2011 13:50

There are times when the hard drive on your computer is running low on space and you would like to know what is taking up the space.  A really good utility to help with this is WinDirStat

This program will list all of the folders and files on your computer and display their sizes.  You can then right-click on a folder or file and easily delete it.  It is simple to use, and best of all, the program is free.

 

Tags:

General