次のように使用した場合、「使用」はどのような目的に役立ちますか。-
1つの例はこれです(回答-@ richj-このコードを使用して問題を解決しました)
private Method(SqlConnection connection)
{
using (SqlTransaction transaction = connection.BeginTransaction())
{
try
{
// Use the connection here
....
transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
}
}
マイクロソフトサポートサイトを読んでいるときに見つけた他の例
public static void ShowSqlException(string connectionString)
{
string queryString = "EXECUTE NonExistantStoredProcedure";
StringBuilder errorMessages = new StringBuilder();
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
try
{
command.Connection.Open();
command.ExecuteNonQuery();
}
catch (SqlException ex)
{
for (int i = 0; i < ex.Errors.Count; i++)
{
errorMessages.Append("Index #" + i + "\n" +
"Message: " + ex.Errors[i].Message + "\n" +
"LineNumber: " + ex.Errors[i].LineNumber + "\n" +
"Source: " + ex.Errors[i].Source + "\n" +
"Procedure: " + ex.Errors[i].Procedure + "\n");
}
Console.WriteLine(errorMessages.ToString());
}
}
}
私はsystem.data.sqlclientなどを使用してページの上部で実行しているので、なぜこれがコードの途中で物を使用しているのですか?
それを省略した場合(コードが機能することはわかっています)、どの機能が失われるのでしょうか?