using的基本用法

1、using指令。using + 命名空间名字,这样可以在程序中直接用命令空间中的类型,而不必指定类型的详细命名空间,类似于Java的import、C++的<#Include>,这个功能也是最常用的,几乎每个cs的程序都会用到。 
例如:using System; 一般都会出现在*.cs中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Configuration;
View Code

2、using别名。using + 别名 = 包括详细命名空间信息的具体的类型。 
这种做法有个好处就是当同一个cs引用了两个不同的命名空间,但两个命名空间都包括了一个相同名字的类型的时候。当需要用到这个类型的时候,就每个地方都要用详细命名空间的办法来区分这些相同名字的类型。而用别名的方法会更简洁,用到哪个类就给哪个类做别名声明就可以了。注意:并不是说两个名字重复,给其中一个用了别名,另外一个就不需要用别名了,如果两个都要使用,则两个都需要用using来定义别名的。

using aClass = NameSpace1.MyClass;
using bClass = NameSpace2.MyClass;
View Code

3、using和SqlConnection、SqlCommand、SqlDataAdapter

用法:将功能代码单独包装起来,结束时自动释放托管的资源

版本1,未使用using:

/// <summary>
        /// 返回数据集dt
        /// </summary>
        /// <param name="database"></param>
        /// <param name="cmdType"></param>
        /// <param name="sql"></param>
        /// <param name="pars"></param>
        /// <returns></returns>
        private static DataTable GetDataTable(String database, CommandType cmdType, String sql, SqlParameter[] pars)
        {

            if (String.IsNullOrEmpty(database))
            { throw new Exception("未设置参数:database"); }
            else if (String.IsNullOrEmpty(sql))
            { throw new Exception("未找到sql语句"); }
            try
            {
                SqlConnection conn = GetConnection(database);
                conn.Open();
                SqlCommand cmd = GetCommand(conn, null, cmdType, sql, pars);
                SqlDataAdapter myda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                myda.Fill(dt);
                myda.Dispose();
                cmd.Dispose();
                conn.Close();
                return dt;
            }
            catch (SqlException sqlex)
            {
                System.Text.StringBuilder log = new StringBuilder();
                log.Append("查询数据出错:");
                log.Append(sqlex.Message);
                throw new Exception(log.ToString());
            }
        }
View Code

版本2,使用using:

/// <summary>
        /// 返回数据集dt
        /// </summary>
        /// <param name="database"></param>
        /// <param name="cmdType"></param>
        /// <param name="sql"></param>
        /// <param name="pars"></param>
        /// <returns></returns>
        private static DataTable GetDataTable(String database, CommandType cmdType, String sql, SqlParameter[] pars)
        {
            
            if (String.IsNullOrEmpty(database))
            { throw new Exception("未设置参数:database"); }
            else if (String.IsNullOrEmpty(sql))
            { throw new Exception("未找到sql语句");}
            try
            {
              using(SqlConnection conn = GetConnection(database))
              {
                  conn.Open();
                  using(SqlCommand cmd = GetCommand(conn,null,cmdType,sql,pars))
                  {
                     using(SqlDataAdapter da = new SqlDataAdapter(cmd))
                     {
                         DataTable dt = new DataTable();
                         da.Fill(dt);
                         return dt;
                     }
                  }
              }
            }
            catch (SqlException sqlex)
            {
                System.Text.StringBuilder log = new StringBuilder();
                log.Append("查询数据出错:");
                log.Append(sqlex.Message);
                throw new Exception(log.ToString());
            }
        }
View Code

using相当于语句块:

SqlConnection conn = null;
            try
            {
                conn = new SqlConnection();
            } 
            catch{} 
            finally
            {
                if (conn != null)
                {
                    conn.Dispose(); 
            };

SqlCommand cmd = null;
            try
            {
                cmd = new SqlCommand();
            } 
            catch{} 
            finally
            {
                if(cmd != null) { cmd.Dispose(); 
            };

SqlDataAdapter myda = null;
            try
            {
                myda = new SqlDataAdapter();
            } 
            catch{} 
            finally
            {
                if (myda != null)
                {
                    myda.Dispose(); 
            };
View Code
原文地址:https://www.cnblogs.com/su1643/p/6655904.html