C# 正确使用和关闭
有人能告诉我,就“使用”、“关闭”和“尝试捕获”而言,下面的代码是否正确吗?根本不是一个C#/CLR的家伙,他继承了一段笨重的代码并试图对其进行排序:C# 正确使用和关闭,c#,sql-server,ado.net,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 integra
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帖子,但我似乎无法在脑海中对其进行排序。我是否需要在每个“使用”块中使用“关闭”?或者“使用”块是否自动调用“Dispose”,从而使“close”不相关?如中所述
using语句确保调用Dispose,即使
调用对象上的方法时发生异常。你可以
通过将对象放入try块并
然后在finally块中调用Dispose;事实上,这就是
using语句由编译器翻译
因此,在使用using语句时,不应该调用Close或Dispose。标准地
Dispose
调用Close
,一些代码分析器甚至说,不应该调用Dispose两次,或者不需要调用Close
。不确定目的,但通常避免重新抛出异常。即catch{…throw;}
气味。好的,那么“Close”是多余的,不需要调用@你是说“扔”不应该在那里?在这方面完全是新手,试着把一切都整理好!对于这里可能出现的重复,我感到抱歉(我确实读了这两篇文章,还有更多)代码>,我不是说永远不要使用它,而是要小心。这里有一个很好的阅读:考虑在这里发布你的问题: