Search This Blog

July 30, 2008

String operations using C#.Net

/*
Following are the methods in C#.Net on String for some of the interviews
1. Reverse a string
2. Remove duplicate words in a give string
3. Remove duplicate chars
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Interview
{
class StringOperations
{
/*Reverse the string -- Method 1*/
public string ReverseString(string inputString)
{
StringBuilder sbTemp = new StringBuilder("");
/*Check either the string is empty or null*/
if (String.IsNullOrEmpty(inputString))
{
return sbTemp.ToString();
}
/* If it is a valid string take a single charcter from last till start of the
string*/
for (int i = inputString.Length - 1; i >= 0; i--)
{
sbTemp.Append(inputString.Substring(i, 1));
}
return sbTemp.ToString();
}

/*Remove duplicate words in a give string.*/
public string RemoveDuplicateWords(string stringToRemoveDuplicates)
{
if (String.IsNullOrEmpty(stringToRemoveDuplicates))
{
return "";
}
ArrayList alist = new ArrayList();
foreach (string word in stringToRemoveDuplicates.Split(' '))
{
if (!alist.Contains(word))
{
alist.Add(word);
}
}
return string.Join(" ", (string[])alist.ToArray(typeof(string)));
}

/*Remove duplicate chars*/
public string RemoveDuplicateChars(string removeDups)
{
StringBuilder sb = new StringBuilder(string.Empty);
foreach (char c in removeDups.ToCharArray())
{
if (sb.ToString().IndexOf(c) == -1)
{
sb.Append(c);
}
}
return sb.ToString();
}
}
}

July 10, 2008

How to restore master (system) database


• How to restore master database backup.
o Stop the Sql Server
o Start the sql server in Single user mode using the command sqlservr.exe – m from command prompt.
o Restore the master database

• How to restore master database when the master database is corrupted.
o Execute the following command by placing setup disk with the following options by navigating the folder where setup.exe is present.
start /wait \setup.exe /qn
INSTANCENAME= REINSTALL=SQL_Engine
REBUILDDATABASE=1 SAPWD=
o After executing the above command, stop the sql server.
o Start the SQL Server in single user mode using the command sqlservr.exe –m
o Restore the master database from the backup.

July 08, 2008

SQL Server 2005: Features

SQL Server 2005 Features
• XML data Type
• Recursive Queries with CTE’s (Common Table Expressions)
• PIVOT and UNPIVOT
• APPLY
• TRY….CATCH error handling
• Database Mirroring
• Common Language Runtime (CLR) .Net Framework Integration
• Dynamic Management Views – Provides information on database performance.
• System Catalog Views
• SQL Server Management Objects (SMO)
• Dedicated Administrator Connection (DAC).
• Database Mail
• Online Index and Restore Options
• Table and Index Partitioning
• Multiple Active Result Sets (MAR)
• Indexes can be modified using Alter Index statement.
• Non-key columns can be added into Covered Index to improve performance.
• T-SQL Enhancements
o Common Table Expressions (CTE)
• Security Enhancements
o Execute As
• Tools
o SQLCMD – command tool to execute sql statements in batch mode.
o TableDiff – To compare data in two tables.
o Dta—Database engine Tuning Advisor – Tool to improve sql statements performance.
Tools introduced with SQL Server 2005
• SQL SERVER Configuration Manager
• SQL SERVER Surface Area Configuration
• SQL Server Management Studio

July 07, 2008

How to find columns with/without default values in a table

How to find the default values of all the columns in all the tables in a database.


SELECT SO.name AS [Table Name],SC.name AS [Column Name],SM.TEXT AS [Default Value],SC.colorder AS [Column Order]
FROM sysobjects SO
INNER JOIN sys.syscolumns SC
ON SO.id = SC.id
INNER JOIN sys.syscomments SM
ON SC.cdefault = SM.id
WHERE SO.xtype = 'U'
UNION
SELECT SO.name AS [Table Name],SC.name AS [Column Name],'' AS [Default Value],SC.colorder AS [Column Order]
FROM sysobjects SO
INNER JOIN sys.syscolumns SC
ON SO.id = SC.id
LEFT JOIN sys.syscomments SM
ON SC.cdefault = SM.id
WHERE SO.xtype = 'U'
AND SM.id IS NULL
ORDER BY SO.name,SC.colorder


How to find only the columns which contains default value in all the tables in a given database.

SELECT SO.name AS [Table Name],SC.name AS [Column Name],SM.TEXT AS [Default Value],SC.colorder AS [Column Order]
FROM sysobjects SO
INNER JOIN sys.syscolumns SC
ON SO.id = SC.id
INNER JOIN sys.syscomments SM
ON SC.cdefault = SM.id
WHERE SO.xtype = 'U'
ORDER BY SO.name,SC.colorder


How to find the columns and tables does not contain default values in a database.

SELECT SO.name AS [Table Name],SC.name AS [Column Name],SC.colorder AS [Column Order]
FROM sysobjects SO
INNER JOIN sys.syscolumns SC
ON SO.id = SC.id
LEFT JOIN sys.syscomments SM
ON SC.cdefault = SM.id
WHERE SO.xtype = 'U'
AND SM.id IS NULL
ORDER BY SO.name,SC.colorder