Handling various types of SQLException in C#

I had a usual scenario of handling various SQLExceptions and I found that there is in fact a very nice table which holds a list of all such scenarios.You just need to run the following query in your master DB.

SELECT * FROM sysmessages

And you can handle the errors in the C# side by looking into the Number property for SQLException.

try
{
}
catch (SqlException exception)
{
if (exception.Number == 2601)
{
//Ignore.Its ok I can live with this!
}
else
throw;
}

Find the table given a column name

I am using the below query pretty often nowadays, as i was digging my way,analyzing something in a legacy application.
You can replace the COLUMN_NAME in between the %% with the value that you want to search for.

SELECT T.NAME AS TABLE_NAME,C.NAME AS COLUMN_NAME
FROM SYS.TABLES AS T INNER JOIN SYS.COLUMNS C
ON T.OBJECT_ID = C.OBJECT_ID WHERE C.NAME LIKE ‘%COLUMN_NAME%’

I wanted to extend it to various databases as well and I found a cool little sp called sp_MSForEachDB .But couldnt get it to work 😦 with my above query.