正确使用和关闭

时间:2018-05-12 19:43:24

标签: c# sql-server ado.net

有人可以告诉我,如果以下代码是正确的,使用","关闭"和"尝试捕获"被关注到?根本不是C#/ CLR的人,继承了一段笨重的代码并试图将其排序:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Collections;
using System.Globalization;

// For the SQL Server integration
using Microsoft.SqlServer.Server;

// Other things we need for WebRequest
using System.Net;
using System.Text;
using System.IO;


public partial class UserDefinedFunctions
{

    // Function to return a web URL as a string value.
    [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
    public static SqlString GET(SqlString uri, SqlString username, SqlString passwd)
    {
        // The SqlPipe is how we send data back to the caller
        SqlPipe pipe = SqlContext.Pipe;
        SqlString document;
        try
        {
            // Set up the request, including authentication
            WebRequest req = WebRequest.Create(Convert.ToString(uri));
            if (Convert.ToString(username) != null & Convert.ToString(username) != "")
            {
                req.Credentials = new NetworkCredential(
                    Convert.ToString(username),
                    Convert.ToString(passwd));
            }
            ((HttpWebRequest)req).UserAgent = "CLR web client on SQL Server";

            // Fire off the request and retrieve the response.
            using (WebResponse resp = req.GetResponse())
            {

                using (Stream dataStream = resp.GetResponseStream())
                {
                    //SqlContext.Pipe.Send("...get the data");
                    using (StreamReader rdr = new StreamReader(dataStream))
                    {
                        document = (SqlString)rdr.ReadToEnd();
                        rdr.Close();
                    }

                    // Close up everything...
                    dataStream.Close();
                }
                resp.Close();
                // .. and return the output to the caller.
                return (document);
            }//end using
        }
        catch (WebException e)
        {
            document = e.ToString();
            return (document);
            throw;
        }
    }

    // Function to submit a HTTP POST and return the resulting output.
    [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
    public static SqlString POST(SqlString uri, SqlString postData, SqlString username, SqlString passwd)
    {
        SqlPipe pipe = SqlContext.Pipe;
        SqlString document;
        byte[] postByteArray = Encoding.UTF8.GetBytes(Convert.ToString(postData));

        // Set up the request, including authentication, 
        // method=POST and encoding:
        try
        {
            WebRequest req = WebRequest.Create(Convert.ToString(uri));
            ((HttpWebRequest)req).UserAgent = "CLR web client on SQL Server";
            if (Convert.ToString(username) != null & Convert.ToString(username) != "")
            {
                req.Credentials = new NetworkCredential(
                    Convert.ToString(username),
                    Convert.ToString(passwd));
            }
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";

            // Submit the POST data
            using (Stream dataStream = req.GetRequestStream())
            {
                dataStream.Write(postByteArray, 0, postByteArray.Length);
                dataStream.Close();
            }
            // Collect the response, put it in the string variable "document"
            using (WebResponse resp = req.GetResponse())
            {
                Stream dataStream = resp.GetResponseStream();
                using (StreamReader rdr = new StreamReader(dataStream))
                {
                    document = (SqlString)rdr.ReadToEnd();
                    rdr.Close();
                }
                dataStream.Close();
                resp.Close();
            }

            return (document);
        }//end try
        catch (WebException e)
        {
            document = e.ToString();
            return (document);
            throw;
        }//end catch
    }
}

我一直在搜索和检查每个StackOverflow帖子,但我似乎无法在脑海中对此进行排序。我需要"关闭"在每个"使用"块?或者使用""阻止呼叫"处理"自动并因此关闭"关闭"不相关?

1 个答案:

答案 0 :(得分:1)

Microsoft using Statement documentation中所述......

  

using语句确保即使调用Dispose也会调用   在对象上调用方法时发生异常。您可以   通过将对象放在try块中来实现相同的结果   然后在finally块中调用Dispose;其实这是怎么回事   using语句由编译器翻译。

因此,在使用using语句时,您不应该调用Close或Dispose。