Three methods for using asp.net using

Source: Internet
Author: User

1. using command. Using + namespace name, so that you can directly use the type in the command space in the program, without specifying the type of detailed namespace, similar to the Java import, this function is also the most commonly used, almost every cs program is used.

Example: using System;

Using System. Data;

2. The using statement defines a range to process objects at the end of the range.

Scenario:

When a class instance is used in a code segment, you want to automatically call the Dispose of the class instance as long as you leave the code segment for whatever reason.

To achieve this goal, it is also possible to catch exceptions using try... catch, but it is also convenient to use using.


For example,

 

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using aClass = leleapplication1.myclass;
Using bClass = leleapplication2.myclass;
Namespace ConsoleApplication1
{
Public
Class MyClass
    {
Public
Override
String ToString ()
        {
Return & quot; You are in ConsoleApplication1.MyClass & quot ;;
        }
    }
Class TestUsing
    {
    }
}
Namespace ConsoleApplication2
{
Class MyClass
    {
Public
Override
String ToString ()
        {
Return & quot; You are in ConsoleApplication2.MyClass & quot ;;
        }
    }
}
Namespace TestUsing
{
Using leleapplication1;
Using leleapplication2;
 
Class ClassTestUsing
    {     
Static
Void Main ()
        {           
           
AClass my1 = new aClass ();
Console. WriteLine (my1 );
BClass my2 = new bClass ();
Console. WriteLine (my2 );
Console. WriteLine (& quot; ress any key & quot ;);
Console. Read ();
        }
    }
}


Using (sdr = cmd. ExecuteReader (CommandBehavior. CloseConnection ))

{

Dt. Load (sdr); // The sdr object is used here.

} // Release the sdr object and close the connection object connection.

For example


Public
Static DataTable GetTable (string SQL, int executeTimeOut, string connStringName)
        {
DataTable dtRet = new DataTable ();
Using (SqlConnection SC = new SqlConnection (connStringName ))
            {
Using (SqlDataAdapter sqa = new SqlDataAdapter (SQL, SC ))
                {
Sqa. SelectCommand. CommandTimeout = executeTimeOut;
Sqa. Fill (dtRet );
Return dtRet;
                }
            }
        }


In this code, the sdr object is included in parentheses, indicating that when the code in using is executed successfully or fails, the Dispose method of the sdr object is automatically called as long as the code is left. CommandBehavior. CloseConnection is used to close the connection when the command object is released. It is consistent with the following code:

 

Try
Conn. Open ()
Return cmd. ExecuteNonQuery ()
Cmd. Parameters. Clear ()
Catch ex As Exception
Return False
Finally
'Close the database tutorial connection and destroy the cmd object.
If Not IsNothing (conn) Then
Conn. Close ()
Conn = Nothing
End If
If Not IsNothing (cmd) Then
Cmd. Dispose ()
Cmd = Nothing
End If
End Try

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.