Sunday, May 31, 2009

How to get commonly used directories using .Net

Many times we require users home directory and other commonly used directories in .NET, this can be achieved using a combination of the Environment.SpecialFolders enumeration, and the Environment.GetFolderPath() function.

Special Folders

Environment.SpecialFolder enumeration contains:

Environment.SpecialFolder.ApplicationData
Environment.SpecialFolder.System
Environment.SpecialFolder.CommonApplicationData
Environment.SpecialFolder.CommonProgramFiles
Environment.SpecialFolder.Cookies
Environment.SpecialFolder.Desktop
Environment.SpecialFolder.DesktopDirectory
Environment.SpecialFolder.Favorites
Environment.SpecialFolder.History
Environment.SpecialFolder.InternetCache
Environment.SpecialFolder.LocalApplicationData
Environment.SpecialFolder.MyComputer
Environment.SpecialFolder.MyMusic
Environment.SpecialFolder.MyPictures
Environment.SpecialFolder.Personal
Environment.SpecialFolder.ProgramFiles
Environment.SpecialFolder.Programs
Environment.SpecialFolder.Recent
Environment.SpecialFolder.SendTo
Environment.SpecialFolder.StartMenu

This enumeration can be used to get specific folder path, using
Environment.GetFolderPath as:

public static String GetMyDocumentsDir()
{
return Environment.GetFolderPath(Environment.SpecialFolder.Personal);
}

It's a nice simple trick for getting system dependent information while using .NET

Happy programming !

Please leave your comments if it helped you!!

Wednesday, April 15, 2009

Configure MSDE: Installation/Server Setup, Database Creation/Backup/Restore/Deletion using OSQL

SERVER SETUP

Step 1: Download Microsoft SQL Server 2000 Desktop Engine Service Pack 3 file (MSDE2000A.exe) from
http://go.microsoft.com/fwlink/?linkID=14502

Step 2: Execute MSDE2000A.exe to extract the MSDE, it extracts to C:\MSDERelA by default

Step 3: Now open an MS-DOS command prompt, and change to the directory where MSDERelA file is extracted, now execute following command to set up the server.

Windows authentication with network access.
Setup.exe DISABLENETWORKPROTOCOLS=0 SAPWD=<password> <Press Enter>
Windows authentication without network access.
Setup.exe SAPWD= <password> <Press Enter>
Mixed Windows and SQL mode authentication with network access.
Setup.exe DISABLENETWORKPROTOCOLS=0 SAPWD=<password> SECURITYMODE=SQL <Press Enter>

Mixed Windows and SQL mode authentication without network access.
Setup.exe SAPWD=<password> SECURITYMODE=SQL <Press Enter>

Step 4: Now in installation window in the left pane double click “Named Pipes” and then “TCP/IP” and then click ok.

Step 5: Now go to Start>Control Panel>Administrative Tools>Services and right click MSSQLSERVER option in the right pane and press Start to start the service. Also make sure startup type should be “Automatic”.

CREATE DATABASE
Execute following command to create database

C:\>OSQL –U sa –P <password> –S <Press Enter>

1>use master
2>go
1>CREATE DATABASE <database name>
2>go

BACKUP DATABASE
Execute following command to backup database

C:\>OSQL –U sa –P <password> –S <Press Enter>

1>BACKUP DATABASE <database name> TO DISK=<backup file path>
2>go

RESTORE DATABASE
Execute following command to restore database

C:\>OSQL –U sa –P <password> –S <Press Enter>

1>RESTORE DATABASE <database name> FROM DISK=<backup file path>
2>go

DELETE DATABASE
Execute following command to delete database

C:\>OSQL –U sa –P <password> –S <Press Enter>

1>use master
2>go
1>DROP DATABASE <database name>
2>go

Sql Server 2005 - Twelve Tips For Optimizing Query


I recently came across very nice article about optimization tips for SQL Server 2005.


1. Turn on the execution plan, and statistics

The first thing you need to do is to use the tools that help you determine whether a query done one way is better than another. That’s what we’re trying to do. By comparing the original query to a new query that we come up with is the best way to evaluate the benefits of any changes.

To do this, go into Sql Server Management Studio and select the Query menu. Select the “Include Actual Query Plan.” This turns on the graphical Execution Plan when you execute a query, and that can be found in the bottom pane after the execution.

In the Execution Plan, you can mouse over the components of the plan and it provides tooltip information boxes. The box contains Estimated Subtree Cost, which can be used to help determine whether one query is better than another. Of course, it’s not always right, as some query parts are not included in the execution plan, but it helps. It is also helpful to know the estimated number of rows, which is also found in this tooltip box.

Next, turn on statistics. Type the following statement:

SET STATISTICS IO ON;

This causes statistics to be output to the Messages tab in the bottom pane. The information you want here is mainly logical reads and physical reads. Logical reads are page reads from memory. Physical reads are page reads from disk. This stat can be a little deceptive as it doesn’t include CPU in the metric, but in general, the less page reads, the less work done, and so the more performant the query will be.

To counteract the above two you should also compare the actual execution times. To do this, eecute the following statement:

SET STATISTICS TIME ON;

This also has issues, as blocking and contention issues affect the output time. You should execute the query a few times to determine how accurate the time shown is.

2. Use Clustered Indexes

Having the clustered index on the primary key is sometimes not the most efficient place for the clustered index to be. A clustered index is the most performant type of index. The whole table is sorted according to the clustered index. If the table is involved in lots of joins based on the primary key, it is probably the right place for it to be, but if you are continually filtering or grouping on other columns in a table, then you should possibly consider changing the primary key index to Non-Clustered, and putting the clustered index on those filtered or grouped columns.

The following statement removes and existing clustered index on the primary key and replaces it with a non-clustered index:

ALTER TABLE MySchema.SalesOrderHeader
DROP CONSTRAINT PK_SalesOrderHeader
GO
ALTER TABLE MySchema.SalesOrderHeader
ADD CONSTRAINT PK_SalesOrderHeader
PRIMARY KEY NONCLUSTERED(SalesOrderID);
GO

Then the following statement adds a new clustered index to a table.

CREATE CLUSTERED INDEX MyClusteredIndex
ON MySchema.SalesOrderHeader (OrderID)
GO

3. Use Indexed Views

Indexed Views have been around for a while. A view is like a named query, and these days you can add indexes to them. If used correctly, they can cause a massive improvement in execution times, often better than a clustered index with covering columns on the original table. Also, in SQL Server Developer Edition and Enterprise Edition, a view index will also be automatically used if it is the best index even if you don’t actually specify the view in your query!

CREATE VIEW MySchema.SalesByCustomer
WITH SCHEMABINDING
AS
SELECT soh.SalesTerritoryID, soh.CustomerID,
SUM(sod.Quantity * sod.UnitPrice)
FROM MySchema.SalesOrderHeader soh
INNER JOIN MySchema.SalesOrderDetail sod
ON (soh.SalesOrderID = sod.SalesOrderID)
GROUP BY soh.SalesOrderTerritory, soh.CustomerID
GO

Note the use of the schema binding attribute. This prevents you from changing underlying tables while this view exists, and is necessary if you want to add an index. Some people avoid indexed views for this reason, as the maintenance becomes more complicated as further dependencies to the view are created. The following statement adds an index:

CREATE UNIQUE CLUSTERED INDEX IdxSalesOrderView
ON MySchema.SalesByCustomer(
SalesTerritoryID, CustomerID
)
GO

4. Use Covering Indexes

Covering indexes are a feature that was newly added to SQL 2005. Basically, you can create an index optimised for the query itself based on joins, filters and grouping, and then add additional columns that can be retrieved directly from the index for use in select statements, as follows:

CREATE NONCLUSTERED INDEX TestIndex
ON MySchema.SalesOrderDetail(OrderId)
INCLUDE (Quantity, UnitPrice)

The above statement causes a non-clustered index to be created on the SalesOrderDetail table. If queries are executed on the OrderId column, the index will be used, and if the only other columns being retrieved are Quantity and UnitPrice, then the query optimiser doesn’t need to retrieve any extra columns from the underlying table. It can just use the index. Because the query optimiser doesn’t need to query the original table, performance is improved.

5. Keep your clustered index small.

One thing you need to consider when determining where to put your clustered index is how big the key for that index will be. The problem here is that the key to the clustered index is also used as the key for every non-clustered index in the table. So if you have a large clustered index on a table with a decent number of rows, the size could blow out significantly. In the case where there is no clustered index on a table, this could be just as bad, because it will use the row pointer, which is 8 bytes per row.

6. Avoid cursors

A bit of a no-brainer. Cursors are less performant because every FETCH statement executed is equivalent to another SELECT statement execution that returns a single row. The optimiser can’t optimise a CURSOR statement, instead optimising the queries within each execution of the cursor loop, which is undesireable. Given that most CURSOR statements can be re-written using set logic, they should generally be avoided.

7. Archive old data

Another no-brainer, so I won’t say much. If you want to improve query performance, give the optimiser less work to do. If you can cut down the number of rows the query has deal with, then performace will improve. I have no problem with people creating audit triggers to move historical data into other tables for this reason. Alternatively, if you don’t need your data after a certain period of time, back up your database and remove the data.

8. Partition your data correctly

These days, you don’t actually have to move old data out of a table to improve query performance. You can partition your table into a number of data segments based on a partition function. The query optimiser can use the partition function to look at rows only on the most appropriate filegroup. To create partitions, you need a partition function and a partition scheme.

CREATE PARTITION FUNCTION myRangePartitionFunction(int)
AS RANGE RIGHT FOR VALUES (1,100,1000)

Once the partition function is created, you can then apply the function to a partition scheme for a table.

CREATE PARTITION SCHEME myRangePartitionScheme
AS PARTITION myRangePartitionFunction
TO (filegrp1, filegrp2, filegrp3, filegrp4)

Then it’s just a matter of creating the table to use the partition scheme on the column you decided to partition on:

CREATE TABLE mySchema.myPartitionTable(
col1 int,
col2 nvarchar(100)
)
ON myRangePartitionScheme(col1)

9. Remove user-defined inline scalar functions

Inline scalar functions are convenient if you want to return a single value, but at the expense of performance. They look somewhat like stored procedures, and they can be used in SQL statements. The problem is that they are not expanded and therefore not optimised into the query plan by the query optimiser. Bad news, because it turns a Seek into a Scan. Queries like this may appear to be performant in the Execution plans and also in the IO statistics, but when you run the query, it can perform really really badly. No seriously, really bad.

Here’s an example of what I’m talking about:

CREATE FUNCTION dbo.fnGetPostalCode(
@Suburb nvarchar(100),
@State nvarchar(10))
RETURNS int
AS
BEGIN
RETURN ISNULL(
(
SELECT PostalCode
FROM dbo.PostalCode
WHERE Suburb = @Suburb
AND State = @State
), -1 );
END
GO

The following statement will only perform a clustered index scan, not a seek, and on a big table this could seriously affect performance.

SELECT s.SalesPersonID,
s.SuburbName,
s.State,
dbo.fnGetPostalCode(s.SuburbName,s.State)
AS PostalCode
FROM dbo.SalesPerson

You can have a look at the details by clicking on SQL Server Management Studio’s Query menu, and selecting “Include Actual Execution Plan”

One way to get around this is to simply inline the underlying query from the function, as follows:

SELECT s.SalesPersonID, s.SuburbName, s.State,
ISNULL( (SELECT PostalCode
FROM dbo.PostalCode
WHERE Suburb = s.SuburbName
AND State = s.State), -1)
AS PostalCode
FROM dbo.SalesPerson

Inline the SQL statement will perform significantly better.

10. Use APPLY

The apply statement was created for the situation where you put multiple inline nested queries in the one statement. For example, take the following statement:

SELECT soh.SalesOrderID,
Quantity=(SELECT TOP 1 (Quantity)
FROM Sales.SalesOrderDetails
WHERE SalesOrderID = soh.SalesOrderID),
UnitPrice=(SELECT TOP 1 (UnitPrice)
FROM Sales.SalesOrderDetails
WHERE SalesOrderID = soh.SalesOrderID)
FROM Sales.SalesOrderHeader soh

This performs an extra query, retrieving data from another table using the same criterion. This can now be replaced with the following:

SELECT soh.SalesOrderID, soh.OrderDate, a.*
FROM Sales.SalesOrderHeader soh
CROSS APPLY (
SELECT TOP (1) sod.UnitPrice, sod.Quantity
FROM Sales.SalesOrderDetail sod
WHERE sod.SalesOrderId = soh.SalesOrderId
ORDER BY sod.Quantity DESC
) as a

11. Use computed columns

Computed columns are derived from other columns in a table. By creating and indexing a computed column, you can turn what would otherwise be a scan into a seek. For example, if you needed to calculate SalesPrice and you had a Quantity and UnitPrice column, multiplying them in the SQL inline would cause a table scan as it multiplied the two columns together for every single row. Create a computed column called SalesPrice, then index it, and the query optimiser will no longer need to retrieve the UnitPrice and Quantity data and do a calculation - it’s already done.

12. Use the correct transaction isolation level

If there are a lot of rows in your table, multiple concurrent requests to that table could cause contention if the correct transaction isolation level is not set. If requests are repeatedly blocked, it could be time to consider whether to change.

For example, READ UNCOMMITED is equivalent to dirty reads, or NOLOCK. That is, if a transaction is in the middle of processing and you read a row, the data may not be valid, especially if multiple inserts/updates are occuring that require atomicity. This is the most performant and it ignores locking altogether, but is generally not allowed by good design and is a special case.

With READ_COMMITTED_SNAPSHOT, it specifies that any data read by the transaction will be the transactionally consistent version of the data that existed at the start of the transaction. Internally, it makes a versioned copy of the data and this is placed in tempdb until the transaction has competed. Except when the database is being recovered, snapshot transactions do not request locks when reading data, and therefore do not block other transactions from writing data. Transactions writing data also do not block other transactions reading data.

There are various other types of transaction options, including REPEATABLE_READ and SERIALIZABLE amongst others that you can look at to determine whether they are appropriate for your needs.


Reference : Original Article


Wednesday, April 1, 2009

Calculate Age using Sql server

This simple SQL will help you to calcuate you age using SQL server


DECLARE @BirthDate DATETIME
DECLARE @CurrentDate DATETIME

SELECT @CurrentDate = getdate(), @BirthDate = '11/24/1984' --Add your date of birth here

SELECT DATEDIFF(YY, @BirthDate, @CurrentDate) -
CASE
WHEN( (MONTH(@BirthDate)*100 + DAY(@BirthDate)) > (MONTH(@CurrentDate)*100 + DAY(@CurrentDate)) )
THEN 1 ELSE 0 END


Please leave your comments if it helped you!!

Saturday, January 17, 2009

Resolving the warning 'Saving changes is not permitted' in SQL Server 2008

For those new to SQL Server 2008, when you perform operations like Adding/Dropping a column or changing the data type of the column in the Management Studio, a warning pops up:

"Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can't be re-created or enabled the option Prevent saving changes that require the table to be re-created."

Solution:

In order to prevent this warning from occuring, Go to Tools > Options > Designers > Table and Database Designers > Uncheck the checkbox 'Prevent saving changes that require the table to be re-created check box'


Now you will be able to make changes to the tables.


If it helped you...please drop ur comments

Generate a unique number in Sql Server 2005

We often find ourselves in the need of generating unique numbers in our database applications.

Let us quickly take a tour of how to do that using Sql server 2005.

SQL Server contains the NEWID() function. This function creates a unique value of type uniqueidentifier. We can use this function in several ways to generate unique numbers to suit our requirements :

Generate GUID's :

UniqueIndentifiers are also knows as GUID's. To generate a GUID use :

SELECT NEWID() as GuidNo

generates FBF2D8E9-F8BE-4F0B-9D49-7CA7C2E3F22C on my machine

Generate only digits :

SELECT ABS(CAST(CAST(NEWID() AS VARBINARY(5)) AS Bigint)) as UniqueNo

generates 427357674589 on my machine

Generate fixed digit unique numbers :

At times, we may also need to generate fixed digit numbers. You can do that in the following manner :

SELECT REPLACE(STR(CAST(CAST(NEWID() AS binary(5)) AS bigint),12),0,0)

generates 470370453006 on my machine


If it helped you ....please drop ur comments !!

Monday, November 10, 2008

String type data inside gridview : Preserve extra spaces

Here’s my problem…
Very often we face a gridview "bug" that when we are saving the string data in the database just like this

"Sanjeev    sanjeev1    sanjeev2     sanjeev123" 
but when it is shown in the gridview it looks like this

" Sanjeev sanjeev1 sanjeev2 sanjeev123".

GridView eats up the multiple spaces. This is only because of the browser that parses HTML. For a browser “space” is nothing, it only considers the " " as space.

And we have a solution too…
If you want to keep your string type data inside gridview, with all the extra spaces that you have entered then there are some workarounds exist for it.

1. BoundField
In case if you are using the BoundField then define it in this way.

<asp:BoundField DataField="description" DataFormatString="<pre>{0}</pre>" HtmlEncode="False" />

There are two important properties of this field.
1. Keep the HtmlEncode=False
2. Inclose the DataFormatString inside the tag.


2. TemplateField
If you are using the TemplateField then define your template field in this way.

<asp:TemplateField ConvertEmptyStringToNull="False">
<ItemTemplate>
<pre ><asp:Label ID="Label1" runat="server" Text='<%# Bind("description") %>'></asp:Label></pre>
</ItemTemplate>
</asp:TemplateField>


There is one important property that needs to set in this case.
1. Enclose your template control inside <pre></pre> tag (in above example I have enclosed the Label Control inside <pre></pre>

Save the HTML friendly spaces
If you don't want to enclose BoundField or the TemplateColumn inside the <pre></pre> tags then you have to manually change the space with HTML compatible space i.e. at the time while saving it in database.
A very simple way is to use the Replace function of string. Just like this.

String str =
"Testing    Testing1       Testing2     Testing123";

str.Replace(" ", " ");


If you liked this .....please drop ur valuable comments.. :)