Monday, June 30, 2008

Reset Database Table Identity Value

One of things that always motivate me to blog is future reference. Anything I place here makes a good reference later. And of course I can always tell people "Hey you can find it here"

So here is today's SQL tip:

you can use the DDBC command to reseed the identity column value. This is usually helpful after a series of DELETE operations. By default MS SQL Server doesn't reseed (give the default seed value + 1) the identity column, so say you had a Users table with 1000 records with an identity column UserID. Then you decided to kick all the users off ( DELETE FROM Users). Then you decided to add them back INSERT INTO Users(.. ) VALUES ( ... ), the first inserted row will have a UserID value of 1001 ( not 1).

DDBC command sytnax:

DBCC CHECKIDENT ( table_name, [reseed / noreseed], [new_seed_value])

So to use it on our Users table, we can do the following:


DBCC CHECKIDENT ( Users, reseed, 0)

I will be soon blogging on the use of FOR XML EXPLICIT. This is not a SQL blog, It is just SQL that I keep forgetting.

Sunday, June 1, 2008

Method Overloading by Return Type

Days ago, I had that interesting conversation with some of my geek friends of overloading methods by return type. I said that is not possible in Java or C# to overload a method by its return type. In other words I said you can not have:

class Demo
{
public static int f()
{
return 1;
}

public static float f()
{
return 1.0F;
}
}

I have backed up my statement with the fact that compiler would have a lot of problems executing the right method, since the methods will have identical signatures but different return types so there is virtually no way of picking up the right method. So one of my friends suggested the following case:

class Demo
{
public static int f(int a)
{
return a;
}

public static float f()
{
return 1.0F;
}
}


With my previous assumption I said that the code won't compile too, which was proven wrong. Actually the compiler will allow that because it can simply differentiate between the two methods, since each of them has a different signature (different parameter list).

So we all learned a valuable lesson in method overloading

Update

The .NET Framework CIL (Common Intermediate Language) actually supports method overloading by return type. Of course the current languages (C#, VB.NET, J# ... etc) running on top of the .NET Framework don't support that feature except when overloading class conversion operators.

Monday, May 5, 2008

Microsoft is taking over Yahoo!

Despite the fact the Microsoft has officially withdrawn its offer to buy Yahoo. I am predicting that this is still the case and that Microsoft is taking ovveeeeeeeeeerrrrrrrrrrr.

Let's see how that goes though !

Sunday, April 27, 2008

Handy SQL Script

Today, I used the following SQL Script to import data from an Excel file into a database table.


INSERT INTO
Users (FirstName, LastName)
SELECT
*
FROM
OPENROWSET( 'Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=C:\Users.xls', 'SELECT * FROM [Sheet1$]')

Sunday, March 30, 2008

When to automate?

Automation is always good when in context. This is what the software industry is all about anyway, automation of most of the repetitive tasks.

But as a software developer what have you done to make your job easier? We keep developing tools and systems for other parties. Though it is us who need those tools because it is us who are doing the repetitive tasks all the time.

So a better question than "When to automate?" 'd be "What to automate?"

Personally speaking, I tend to automate testing. A while ago I was working on a web application that required upload to a test FTP server. So whenever I changed an asp file or re-compiled a FLASH movie, I had to manually upload those files to the FTP server. Of course that didn't work, and that was when I started looking out for ways to automate the whole process. I came up with a very simple solution which is using the built-in windows FTP command which supports scripting. (ftp -s:filename)

That saved me a lot of time back then. And we are not even talking about Code Generation or batch updates.

Beside testing, there is a list of things that if automated could make your life easier and help you spend more time on stuff that matters instead of doing repetitive boring tasks.

For instance if you are doing some C# development for windows, you probably have been bored writing properties and private members for every new class you add to your solution. Though C# 3.0 supports automatic properties now. Still it is a boring task. Others include adding error providers to a windows form and handling the Validated event for every control.

These are just example of repetitive tasks that are actively killing our time and minds. If you feel you are wasting time on such tasks, consider automation. Spend some of your time on developing tools for YOU! I have done that and it really saves me a lot of time and helps me focus on more important stuff.


For example a while ago I wrote a very small tool that does the following:
1 - Generates class member variables and properties.
2 - Generates error providers and generate validation events.
3 - Creates temporary text files for me for my todos and so on.
4 - Takes screenshots helping me when I want to demonstrate something to someone or for use in emails and reporting bugs.

Below is a screenshot of that tool.


Developing it took me less than 4 hours, thats less than half my working day. BUT saved me infinite hours of hitting my head against the wall.
I am planning to develop a similar tool to help me with my SQL tasks soon.

Exception handling versus error codes

A while ago, I was really lost between throwing/catching exceptions and returning error codes. After doing some research I have reached the following:
Exception handling is usually expensive due to the stack unwinding process, specially when it is not caught near where it was thrown.

So you can't just go throwing an exception for every trivial error or an invalid input to a procedure. This leads me to believe that a good approach 'd be to check all the inputs given to a method using normal if/else blocks,returning failure status or error codes when something goes wrong or wrong input is provided and only throwing an exception when a fatal error occurs.