Thursday, December 8, 2011

What is Cursor?Purpose of Cursor?Advantage and Disadvantage of Cursor.


Whenever we are processing sql statement SQL allots PRIVATE SQL AREA OR WORK AREA to store porcessed information. Cursor is a pointer to this private sql area.
Oracle automatically creates an implicit cursor for each sql statement.
If you want to handle and control processing of each row of data explicitly you may define explicit cursor.

A cursor is a set of rows together with a pointer that identifies a current row.

In other word, Cursor is a database object used by applications to manipulate data in a set on a row-by-row basis.

For Example:If you need to insert/update/delete bunch of data one by one, you have to use a cursor with a while loop. It’s not a good behavior to use loops in SQL but sometimes you have to use them. Anyway if you don’t know how to define and use a cursor, you may take a look at the code snippet below. Or maybe you may always forget how to define one like me ;o))

Steps for cursor

1. DECLARE CURSOR

2.
OPEN

3.
FETCH

4.
@@FETCH_STATUS

5.
CLOSE


6.DEALLOCATE CURSORS

DisAdvantage of Cursors

A cursor is a memory resident set of pointers -- meaning it occupies memory from your system that may be available for other processes. Poorly written cursors can completely deplete available memory. Means it consumes more Resources

Advantage of Cursors

Cursors can be faster than a while loop but they do have more overhead. Another advatage is that it is we can do RowWise validation or in other way you can perform operation on each Row.It is a Data Type which is used to define multivalue variable.

But one factor affecting cursor speed is the number of rows and columns brought into the cursor. Time how long it takes to open your cursor and fetch statements. If it's lengthy, look carefully at your cursor logic; see if you can remove columns from the declare statement, and change your where clause in the declare statement to only return rows the cursor needs. If the fetch statements themselves are lengthy or consuming too much IO or CPU, look at the cursor declare statement and ensure you have optimal indexes in place on your base tables or temporary tables.

Thursday, November 10, 2011

State Management:Cookie

In This Post I will show You what is Cookie,How to use it and what is advantage and Limitation of cookie..

What is Cookie?

A cookie is a small bit of text that accompanies requests and pages as they go between the Web
server and browser. The cookie contains information the Web application can read whenever the user visits the site.Imagine that when users request a page from your site, www.Example.com, your application sends not just a page, but a cookie containing the date and time. When the user's browser gets the page, the browser also gets the cookie, which it stores in a folder on the user's hard disk.Later, the user requests a page from your site again. When the user enters the URL
www.contoso.com, the browser looks on the local hard disk for a cookie associated with the URL. If the cookie exists, the browser sends the cookie to your site along with the page request. Your
application can then determine the date and time that the user last visited the site. You might use the information to display a message to the user, check an expiration date, or perform any other useful function.

Limitation of Cookies?
Before we start on the mechanics of working with cookies, I will mention a couple of limitations of
working with cookies. Most browsers support cookies of up to 4096 bytes. That is plenty of space for storing a few values on the user's computers, but you would not want to try to store a dataset or some other potentially large piece of data in a cookie. More practically, you probably do not want to store a big collection of user information in a cookie. Instead, you would want to store a user number or other identifier. Then, when the user visits your site again, you would use the user ID to look up user details in a database. (But see Cookies and Security for some caveats about storing user information.)
Browsers also impose limitations on how many cookies your site can store on the user's computer. Most browsers allow only 20 cookies per site; if you try to store more, the oldest cookies are discarded. Some browsers also put an absolute limit, usually 300, on the number of cookies they will accept from all sites combined.

How To Add(write) Cookie...

Example
HttpCookie cookie = new HttpCookie("BackgroundColor");
cookie.Value = "green";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);

Read Cookie

string color=Request.Cookies["BackgroundColor"].Value;

Delete Cookie

HttpCookie cookie = new HttpCookie("BackgroundColor");
cookie.Value = "green";
cookie.Expires =DateTime.Now.AddDays(-1);
Response.Cookies.Add(cookie);


Store Multiple values in cookie and Read it.

HttpCookie userCookie = new HttpCookie("UserInfo");
userCookie["Country"] = "Italy";
userCookie["City"] = "Rome";
userCookie["Name"] = "Jones";
userCookie.Expires = DateTime.Now.AddDays(3);
Response.Cookies.Add(userCookie);
Label1.Text = "Cookie create successful!";
HttpCookie cookie = Request.Cookies["UserInfo"];
if (cookie != null)
{
string country = cookie["Country"];
string city = cookie["City"];
string name = cookie["Name"];
Label2.Text = "Cookie Found and read";
Label2.Text += "Name: " + name;
Label2.Text += "Country: " + country;
Label2.Text += "City: " + city;
}