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.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|