理解asp.net 4.0中的“using”语句 - C#

时间:2011-10-01 13:44:04

标签: c# .net asp.net using

这两个代码之间有什么区别吗?应该使用哪一个?

asp.net 4.0,c#

代码1:

using System;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;

public static class DbConnection
{
    public static string srConnectionString = "server=localhost;database=mydb;uid=sa;pwd=mypw;";

    public static DataSet db_Select_Query(string strQuery)
    {
        DataSet dSet = new DataSet();

        try
        {
            using (SqlConnection connection = new SqlConnection(srConnectionString))
            {
                connection.Open();
                SqlDataAdapter DA = new SqlDataAdapter(strQuery, connection);
                DA.Fill(dSet);
            }
            return dSet;
        }
        catch
        {
            if (srConnectionString.IndexOf("select Id from tblAspErrors") != -1)
            {
                using (SqlConnection connection = new SqlConnection(srConnectionString))
                {
                    connection.Open();
                    strQuery = strQuery.Replace("'", "''");
                    SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection);
                    command.ExecuteNonQuery();
                }
            }
            return dSet;
        }
    }

    public static void db_Update_Delete_Query(string strQuery)
    {
        try
        {
            using (SqlConnection connection = new SqlConnection(srConnectionString))
            {
                connection.Open();
                SqlCommand command = new SqlCommand(strQuery, connection);
                command.ExecuteNonQuery();
            }
        }
        catch
        {
            strQuery = strQuery.Replace("'", "''");
            using (SqlConnection connection = new SqlConnection(srConnectionString))
            {
                connection.Open();
                SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection);
                command.ExecuteNonQuery();
            }
        }
    }
}

代码2:

using System;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;

public static class DbConnection
{
    public static string srConnectionString = "server=localhost;database=mydb;uid=sa;pwd=mypw;";

    public static DataSet db_Select_Query(string strQuery)
    {
        DataSet dSet = new DataSet();

        try
        {
            using (SqlConnection connection = new SqlConnection(srConnectionString))
            {
                connection.Open();
                using (SqlDataAdapter DA = new SqlDataAdapter(strQuery, connection))
                {
                    DA.Fill(dSet);
                }
            }
            return dSet;
        }

        catch
        {
            using (SqlConnection connection = new SqlConnection(srConnectionString))
            {
                if (srConnectionString.IndexOf("select Id from tblAspErrors") != -1)
                {
                    connection.Open();
                    strQuery = strQuery.Replace("'", "''");

                    using (SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection))
                    {
                        command.ExecuteNonQuery();
                    }
                }
            }
            return dSet;
        }
    }

    public static void db_Update_Delete_Query(string strQuery)
    {
        try
        {
            using (SqlConnection connection = new SqlConnection(srConnectionString))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand(strQuery, connection))
                {
                    command.ExecuteNonQuery();
                }
            }
        }
        catch
        {
            strQuery = strQuery.Replace("'", "''");
            using (SqlConnection connection = new SqlConnection(srConnectionString))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection))
                {
                    command.ExecuteNonQuery();
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

如果你有ReSharper为你做或者愿意手动下载它们,你可以查看SqlCommandSqlDataAdapter的源代码,看看他们的Dispose()方法是否真的做了什么。如果没有,可以安全地省略它们周围的使用陈述。

答案 1 :(得分:1)

您应该考虑的事项是using子句用于确保您在该using子句的括号内使用的对象正确地放置在内存中。

所以,我认为你应该使用第二个代码示例。