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

发布于 2024-12-08 04:25:33 字数 4291 浏览 1 评论 0原文

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

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();
                }
            }
        }
    }
}

Are there any difference between these 2 codes ? Which one should be used ?

asp.net 4.0 , c#

code 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();
            }
        }
    }
}

code 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();
                }
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百思不得你姐 2024-12-15 04:25:33

如果您有 ReSharper 为您做这件事或者愿意手动下载它们,您可以查看 SqlCommandSqlDataAdapter 的源代码,看看它们的 Dispose() 方法是否有效实际上做任何事情。如果没有,那么省略它们周围的 using 语句可能是安全的。

If you have ReSharper to do it for you or are willing to download them manually, you could look at the source code for SqlCommand and SqlDataAdapter and see if their Dispose() method actually does anything. If not, it's probably safe to leave out the using statements around them.

櫻之舞 2024-12-15 04:25:33

您应该考虑的是,using 子句用于确保在 using 子句的括号内使用的对象正确地处理到内存中。

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

What you should take in consideration is the fact that the using clause is used to make sure that the objects you use inside the brackets of that using clause are disposed correctly into memory.

So, I think you should use the second code example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文