Wednesday, 4 August 2010

How to get database data file path

Recently I needed to create universal script which added new file group and file to database regardless of where the MSSQL server was installed. The reason for creating new file was to split data which were changing a lot (table with cached documents) and data which were more or less static.

There was necessary to find out where is located primary file for current database and then to get just directory where the file was located.
DECLARE @PATH nvarchar(max)

SELECT @PATH = LEFT(filename, LEN(filename) + 1 - CHARINDEX('\', REVERSE(filename)))
FROM  master.dbo.sysaltfiles WHERE db_name(dbid) = db_name() AND name = 'PrimaryFileName'

Monday, 10 May 2010

Database Project in Visual Studio 2008

There exists database project in Database Developer edition of Visual Studio 2008. It can be very useful but also painful. The original version needs to have installed local database server for this kind of project but a lot of things can go wrong way. There is a lot of troubleshooting articles on Internet so you think it helps you to resolve the problems quickly - unfortunately not in my case. I have spent almost a day without success until I have found Microsoft® Visual Studio Team System 2008 Database Edition GDR R2. It took me less then one hour to install the package, convert old database project and deploy project. Package installs new database project which doesn't need database server on your local. That was really great and I recommend it GDR to everyone struggling with the original database project.

Wednesday, 28 April 2010

How to install Microsoft SQL Server 2008 Management Studio Express

I had on my computer installed SQL Server 2008 Express edition and I was looking for something which will help me to work with it. I have found there is Microsoft SQL Server 2008 Management Studio Express. You can find it on Microsoft download pages.
As a prerequisite you will need to install PowerShell if you already don't have it on your computer. I had trouble to use link to PowerShell 1.0 on the download page so I recommend to use this download link to PowerShell 2.0 on Windows Management Framework page in Windows Management Framework Core section.
When you have installed PowerShell you can start to install Management Studio. I was a bit surprised to see that what I downloaded contains SQL Server installation package. So for the installation of Management Studio 2008 you will need to go Installation > New SQL Server stand-alone installation or add features to an existing installation then pass installation checks and in Feature Selection check Management Tools - Basic.

Friday, 16 April 2010

Book recommedation - Co programátory ve škole neučí

At first I would like to apologize to non Czech speaking people. The book I would like to recommend is written in Czech.

I had the book for quite a long time in my bookcase, found it recently and I am glad that I have finally read it. The book discusses difficulties of software development, why a lot of projects are not finished in time or in money. Book doesn't contain any heavy and hard to read theory. On the other hand there are lot of examples from author's work experience. I believe everybody will find out similar examples from his own experience which makes the book very readable.

In short the book contains:
  • specificities of software development
  • frequent reasons why software project fails
  • principals for gathering requirements, writing specification
  • the most frequent mistakes of developers and their reasons
  • how to deal with security holes
  • what can influence speed of software development
  • problems with user interfaces
  • application testing
  • economy side of projects
  • management and communication in development team
  • importance and weaknesses of methodologies, analysis, visual modeling
  • and more...

Sunday, 24 January 2010

GDI object leaking example

Finally I have a bit of time to post what caused us GDI objects leaking troubles. I hope it can same time to others who are dealing with the same problem. We ensured that all Graphics, Brushes etc. are destroyed correctly but the application was still leaking. I would like to show part of code which we found when investigating the GDI object leaking issue. We used component which should show first and last and in tooltip some more info. Tooltip was created every time the person was set but the old one was not cleaned up. Each Tooltip use couple of GDI objects which were not released until the application was closed.
public Person SelectedPerson
{
  get { return currentPerson; }
  set
  {
    currentPerson = value;
    Text = GetDescription(currentPerson);
    if (currentPerson != null)
    {
      ToolTip toolTip = new ToolTip();
      toolTip.SetToolTip(this, GetDetailDescription(currentPerson));
    }
  }
}

Saturday, 21 November 2009

Great article about memory leaks in .NET application

When digging more information about GDI resource leaking I have found nice article which describes what memory leaks can be found in .NET applications and how to avoid them. The article also shows what tools can be used to find different kinds of leaks. Here is the link How to detect and avoid memory and resources leaks in .NET applications.

Tuesday, 17 November 2009

GDI objects leaking and its prevention

Everyone who is developing WinForms applications can find out that his application starts in some point have problem with raising GDI objects count. Usually developers who use some high-level programming language (in my case c#) don't think they should care about releasing some low-level objects such as GDI objects but it's not true. It can happen that the application which is opening a lot of forms, creating its forms dynamically etc. will start to be slow and after some time even unusable.

What are GDI objects for?

Among GDI objects belong Bitmap, Brush, DC, Font, Palette and some other objects (see full list in the link section). These objects are used for visualization of UI of applications in Windows. Each version of Windows has maximum number of GDI objects which can be created in the moment (e.g. WinXP has default of 10000 objects set). The max number can be set in Windows registry. The actual amount of created objects per application can be displayed in task manager (show GDI objects column settings).

What is the problem?

Each time the application needs to show new form, GDI objects are created and if they are not properly disposed then their amount is raising, consuming memory and windows kernel starts to have problem with redrawing forms, creating new forms and whole system slows down.

What can developer do to prevent GDI objects count raising?

Developer should make sure that Dispose() method is called on all disposable objects. The biggest problem is when the form content is composed dynamically then Dispose() method needs to be called manually on all dynamically created controls. When application shows non-modal forms (use form.Show()) then developer doesn't need care about calling Dispose() method. But if the application shows modal dialogs (use dlg.ShowDialog()) then Dispose() needs to be called manually to free up created GDI resources.

Links:

GDI Objects on msdn
GDI on wikipedia
Detecting GDI user handler leaks in WinForms
Detect and Plug GDI Leaks in Your Code with Two Powerful Tools for Windows XP
GDI memory leak in Windows Forms