Monday, January 12, 2009
Paging in MS SQL server. (An alternative to Limit in MySQL)
Saturday, January 3, 2009
Updating Only Non NULL parameters in Stored Procedure
"Update only the columns with non null values in parameter in a row."
I will explain this.
Suppose you have a users table. You are using a single stored procedure to update all the columns in table.
suppose now you need to change only the field 'Name' using this stored procedure. The other parameters will be empty. Then u can use the ISNULL property to update the table.
Here is an example.
CREATE PROC [dbo].[usp_UserDetailsUpdate]
@ControlID int,
@EmailId varchar(255)=NULL,
@LoginId varchar(255)=NULL,
@Name varchar(255)=NULL
AS
UPDATE [dbo].[UserDetails]
SET
[EmailId] = ISNULL(@EmailId, EmailId),
[LoginId] = ISNULL(@LoginId,LoginId),
[Name] = ISNULL(@Name, [Name]
WHERE [ControlID] = @ControlID
StringBuilder Vs String Concatenations
StringBuilder or String Concatenation.. Which is better ?
How they work ?
String is immutable while StringBuilder is mutable.
After a string object is created its value cannot be changed. Then how
name ="jimmy"+"augustine" is possible ? When we assign this , we actually discard the old string object and create a new string.
But StringBuilder is mutable. StringBuilder class provides methods to change its contents at anytime.StringBuilder has a very useful method "append" to add new strings to the end of the existing string. StringBuilder internally reserves a certain amount of memory(Buffer). When a new string is added to the StringBuilder, the new string is just copied to the existing buffer. If the buffer is not enough to fit the new string , then a new buffer is created to fit the new string.
My Performance Test ( StringBuilder VS Concatenation):
Here is a simple performance test which demonstrate the power of stringBuilder over string concatenation.
DateTime start;
start = DateTime.Now;
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < strbuilderexecutiontime =" DateTime.Now" builder ="" start =" DateTime.Now;" str =" string.Empty;">for (int i = 0; i <>
{
str = str + i;
}
strBuilderExecutionTime =DateTime.Now - start ;
Console.WriteLine("Exection Time for Concatenation =" + strBuilderExecutionTime.ToString());
Output :
Execution Time for string builder =00:00:00.0312500
Execution Time for Concatenation =00:01:18.5937500
When looking at the output of the code , it is very clear that StringBuilder is much more efficient than the regular concatenation.
Is there any disadvantage for StringBuilder when comparing with String concatenation ?
Is StringBuilder always a better Option than concatenation ? No... Because creating a StringBuilder object using the constructor will take time. So if there is only few concatenations ( say less than 5 apprx) , then no need to go for StringBuilder.
Conclusion:
If the number of concatenations is more , then use StringBuilder. But if the number of concatenations is very few then go for ordinary "+" concatenations.
If the number of concatenations is more than 8, then in all the situations StringBuilder is faster than String concatenations.
Web Config file in ASP.net
How to add a web.config file?
1) Create a new website by selecting File->New->Website from the Menu
Visual Studio 2005 will create a basic website with a default page for you.
2) Click on the Website ->Add New Item from the menu or press Ctrl+Shift+A
A new pop up window will be displayed. Select Web Configuration File from the popUp.
3) Now you can see a Web.config file added to your project.
The default web.config file will look something like this.(I removed all the comments from web.config)
We can now use the web.config file. Now lets store a value in web.config file and read that from our application.
Reading a value from web.config.
1) Open web.config file
2) Add a new key in web.config file under AppSettings.
Now the web.config file will look like this.
3) Now We can read this value from our aspx page.
For reading a value from web.config file, we can use the ConfigurationManager class.
Write the following code in page load event .
String Name = ConfigurationManager.AppSettings.Get("Name");
Response.Write("Value of Name from web.config is : " + Name);
4) Now run your sample website. Done !
Storing a Connection String in web.config.
Storing your connection string in web.config file when you are using visual studion 2005 is very easy.
You can add a new connection string under the tag
Here is a sample web.config.
You can read the connection string using the following code.
String connectionString = ConfigurationManager.ConnectionStrings["TestConnectionString"].ToString();
Response.Write("Value of TestConnectionString from web.config is : " + connectionString);