简体   繁体   English

在ADO.NET中处理“使用”语句

[英]Working on “using” statement in ADO.NET

I want to properly dispose the SqlConnection object whenever i come out of the method. 每当我走出方法时,我都想适当地处置SqlConnection对象。 So im using the "using" statement as shown below. 因此,即时通讯使用“ using”语句,如下所示。

public int Hello()
{
   using(SqlConnection con=new SqlConnection(constring))
   {
      using(SqlCommand cmd=new SqlCommand(Query,con))
      {
       try
       {
          con.Open();
          return cmd.ExecuteNonQuery();
       }
       catch(Exception ex)
       {
          throw ex;
       }
       finally
       {
          con.Close()
       }
      }
   }
}

Now, what i want to know is, Will the above code 现在,我想知道的是,上面的代码

  1. Dispose the Connection properly when an Exception is occured in ExecuteNonQuery. 当ExecuteNonQuery中发生异常时,请正确处理连接。
  2. Make sure we will not get any ConnectionPool issues 确保我们不会出现任何ConnectionPool问题
  3. Make sure the data is returned properly 确保正确返回数据
  4. If an exception occurs in SqlConnection will it dispose the object? 如果SqlConnection中发生异常,它将处置该对象吗?

Can anyone help me on this? 谁可以帮我这个事?

You don't need the try/catch if you're just going to throw it, just change your code to this: 如果只想扔掉它,则不需要try/catch ,只需将代码更改为此:

public int Hello()
{
    using(SqlConnection con=new SqlConnection(constring))
    {
        using(SqlCommand cmd=new SqlCommand(Query,con))
        {
            con.Open();
            return cmd.ExecuteNonQuery();
        }
    }
}

and regardless of what happens, exception or not, the connection will get closed if it's open and disposed. 并且无论发生什么情况(是否发生异常),如果打开并处理该连接,它将关闭。

Dispose the Connection properly when an Exception is occured in ExecuteNonQuery. 当ExecuteNonQuery中发生异常时,请正确处理连接。 Yes

Make sure we will not get any ConnectionPool issues 确保我们不会出现任何ConnectionPool问题

i guess you mean connections would be properly relieved after executing query. 我想你的意思是执行查询后,连接将被适当地释放。 if that is your question than You should not by using this approach. 如果这是您的问题,那么您不应该使用这种方法。

Make sure the data is returned properly 确保正确返回数据

using has nothing to do with returning data 使用与返回数据无关

If an exception occurs in SqlConnection will it dispose the object? 如果SqlConnection中发生异常,它将处置该对象吗?

Yes

though you can rewrite your code as 虽然您可以将代码重写为

using(SqlConnection con=new SqlConnection(constring))
   {
      using(SqlCommand cmd=new SqlCommand(Query,con))
      {
       try
       {
          con.Open();
          return cmd.ExecuteNonQuery();
       }
       catch(Exception ex)
       {
          throw;
       }       
      }
   }

You should not use the using statement for sqlconnection. 您不应将using语句用于sqlconnection。

using(SqlConnection con=new SqlConnection(constring)) 使用(SqlConnection con =新的SqlConnection(constring))

Better you use try,catch and finally block to close the connection. 最好使用try,catch和finally块关闭连接。 So even if exception occurs in try & catch the finally block will execute and close the connection if its open. 因此,即使在try&catch中发生异常,finally块也将执行并在连接打开的情况下将其关闭。

The reason behind this is, think of below situaion. 这背后的原因是,想想下面的情况。

  1. Create object of a class that handles all database operation eg DBUtility objDB = new DBUtility() 创建处理所有数据库操作的类的对象,例如DBUtility objDB = new DBUtility()

the above statement creates object of class and also initializes the sqlconnection variable from the constructor. 上面的语句创建类的对象,并从构造函数初始化sqlconnection变量。

  1. Now i am using the object objDB for executing multiple queries one by one. 现在,我正在使用对象objDB一对一地执行多个查询。 For this it should initialize the sqlconnection object only once and use it for its whole life (life obj objDB). 为此,它应该只初始化一次sqlconnection对象,并在整个生命周期中使用它(生命obj objDB)。

  2. In your case the sqlconnection will be initialized as and when the method is called. 在您的情况下,sqlconnection将在调用方法时初始化。

  3. So simply init the connection once and open/close it for each of your operations. 因此,只需将连接初始化一次,然后为每个操作打开/关闭它。 Your connection will automatically disposed by Garbage collector when objDB is disposed. 处置objDB时,垃圾收集器会自动处置您的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM