最后块是如何工作的。 [英] How finally block works.

查看:66
本文介绍了最后块是如何工作的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须知道finally块实际执行的时间。在异常发生后最终阻止执行还是会正常执行?

代码片段:

 尝试 
{
如果(txt_password.Text == txt_cpassword.Text)
{
con.Open();
cmd = new SqlCommand( DELETE FROM LoginTB WHERE Username =' + txt_username.Text + 'AND Password =' + txt_cpassword.Text + ',con);
cmd.ExecuteNonQuery();
MessageBox.Show( 用户帐户已删除);
}
else
{
MessageBox.Show( 抱歉...密码与确认密码)不匹配;
}
}
catch (例外)
{
MessageBox.Show( 无效的操作);
}
最后
{
con.Close();
}



如果尝试 - catch块没有给我任何异常意味着它通常执行到它想要的结果。

if我在finally块中写con.Close()是否有连接关闭或者我必须在try块中写con.Close()。



感谢你。

解决方案

简单来说,无论异常如何, finally 都可以保证执行。请参阅: try-catch-finally(C#参考) [ ^ ]



它声明:

Quote:

catch的常见用法,最后一起是获取和使用try块中的资源,处理catch块中的异常情况,并释放finally块中的资源。



旁注



使用参数化查询总是好的。也就是说,

 DELETE FROM LoginTB WHERE Username = ' + txt_username.Text +'  AND Password = ' + txt_cpassword.Text +' 

应该进行修改以防止SQL注入攻击。



请参阅:使用参数化查询来防止sql server中的sql注入攻击 [ ^ ]


finally 块总是执行 - 无论是否有错误。


规则拇指是尽可能晚地打开。尽快关闭。使用using语句将确保。



看看 http://msdn.microsoft.com/en-us/library/vstudio/yh598w02.aspx。



出于这个原因,我通常会这样做:



使用(MySqlConnection con = new MySqlConnetion(connectionString)

{

con.open();

//在这里执行数据库查询



} // con关闭并在退出时立即处理< br $> b $ b



这里有一个很好的例子:

http://www.dotnetperls.com/sqlconnection

I have to know when finally block actually executed.finally block executed after the exception occurred or will executed normally?
code snippet:

try
{
   if (txt_password.Text == txt_cpassword.Text)
   {
     con.Open();
     cmd = new SqlCommand("DELETE FROM LoginTB WHERE Username = '" + txt_username.Text + "' AND Password = '" + txt_cpassword.Text + "'", con);
     cmd.ExecuteNonQuery();
     MessageBox.Show("User account Deleted");
   }
    else
      {
       MessageBox.Show("Sorry...Password does not match with confirm password ");
      }
}
catch (Exception)
  {
   MessageBox.Show("Invalid operation");
  }
finally
  {
   con.Close();
  }


If try - catch block don't give me any exception means it normally executed to its desired result.
if I write con.Close() in finally block is there connection close or I have to write con.Close() in try block.

Thanking you.

解决方案

In very simple terms,finally is guaranteed to execute regardless of exception. See this : try-catch-finally (C# Reference)[^]

It states that:

Quote:

A common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block.


Side note:

It is always good to use parameterized query. That is,

DELETE FROM LoginTB WHERE Username = '" + txt_username.Text + "' AND Password = '" + txt_cpassword.Text + "'

should be modified to prevent SQL injection attacks.

See this : Using parameterized queries to prevent sql injection attacks in sql server[^]


The finally block always executes - irrespective of whether there is an error or not.


The rule of thumb is "open as late as possible. Close asap". Use "using" statement will ensure that.

Take a look at http://msdn.microsoft.com/en-us/library/vstudio/yh598w02.aspx.

For this reason, I usually do this:

using(MySqlConnection con= new MySqlConnetion(connectionString)
{
con.open();
// perform database query here

} //con gets closed and disposed immediately upon exits


There is a good example here:
http://www.dotnetperls.com/sqlconnection


这篇关于最后块是如何工作的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆