C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Difference between using directive and Using Statement in .NET


Posted Date: 19 Jun 2008    Resource Type: Articles    Category: .NET Framework

Posted By: Gaurav Agrawal       Member Level: Silver
Rating:     Points: 10




Using Directive



Using directive is basically used to import namespaces into application.

Example


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;


When the namespace is too long then we can use alises of that namespace.

Example


Using abs = System.Web.UI.WebControls.WebParts;


Now in the application we can use abs in place of System.Web.UI.WebControls.WebParts


Using Statement



Using Statement is widely used in database applications.It is basically used to close the database connection and release the memory occupied by the database objects. Basically, it is the task of garbage Collector(GC) but GC does not clean up database objects.
This statement removes the developers work to clean up the database objects.

Example


using (SqlConnection cn = new SqlConnection())
{
Guid gid = new Guid();
cn.ConnectionString = GetConnectionString();
string command = "Insert Into UserTable(USER_ID, USER_NAME) values(@ID,@Name)";
SqlCommand cmd = new SqlCommand(command,cn);
cmd.Parameters.AddWithValue("@ID",System.Guid.NewGuid().ToString());
cmd.Parameters.Add("@Name","Saurabh Agrawal");
cn.Open();
cmd.ExecuteNonQuery();
gid.ToString();
}


In the above example we have placed the SQLConnection in Using Statement. So we do not required to close up the connection and free the memory. These tasks are taken care by our Using Statement.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Using statement  .  Using directive  .  Difference between using statement and directive  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to save configuration settings of the assembly into the registry?
Previous Resource: Differences Between Convert.ToInt and Int.Parse
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

fax server

Contact Us    Privacy Policy    Terms Of Use