尝试/最后使用数据访问代码 [英] try/finally with data access code

查看:58
本文介绍了尝试/最后使用数据访问代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有代码这样的话

SqConnection cndb = new SqlConnection(connstr)

SqlDataReader dr = null

tr


cndb.Open()

SqlCommand cmd = new SqlCommand(" exec mysp",cndb)

dr = cmd .ExecuteReader()

catch(System.Exception e1){Debug.WriteLine(e1.Message);

finall


如果(dr!= null)dr.Close()

cndb.Close()

什么问题我是博士有可能是非null,但不是打开 - 当finally块开始时 - 从而导致lin

如果(dr!= null)dr.Close()

失败,会导致cndb.Close()永远不会发生,让连接保持打开状态

这是在ASP.NET页面中

当然我不需要放另一个尝试...终于在第一个终于

或者是否有一些属性我可以用来检查它的状态


谢谢

任何建议请...

解决方案

不,博士不能为空。如果失败,ExecuteReader将抛出异常,它将不会返回null。


您可以简化所有这些并使用using重写它。声明,因为

你操作的所有对象实现了IDisposable接口。

以下是我将如何重写这个:


使用(SqlConnection cndb = new SqlConnection(connstr))

{

cndb.Open();

using(SqlCommand cmd = new SqlCommand(") ; exec mysp",cndb))

{

使用(SqlDataReader dr = cmd.ExecuteReader())

{

//你的东西在这里

}

}

}


另外,除非你真的想在这个级别记录异常(你应该

返回一个状态,表明操作失败了),我不会在这里看到

异常?我会让它向上传播,直到达到b $ b战略在你的代码中(或在框架中)指向它将被记录的地方

以及执行将在何处恢复。


布鲁诺。


" RepStat" <一个******* @ discussions.microsoft.com> écritdansle message de

新闻:5D ********************************* * @ microsof t.com ...

如果我有代码如

SqConnection cndb = new SqlConnection(connstr);
SqlDataReader dr = null; <尝试
{/ cndb.Open();
SqlCommand cmd = new SqlCommand(" exec mysp",cndb);
dr = cmd.ExecuteReader();
}
catch(System.Exception e1){Debug.WriteLine(e1.Message);}
最后
{
if(dr!= null)dr。关闭();
cndb.Close();
}

什么错误我是有可能dr是非null,
但不是open - 当finally块开始时 - 从而导致行if(dr!= null)dr.Close();
失败,这将导致cndb.Close()永远不会发生,留下
连接开放?这是在一个ASP.NET页面。
我当然不必再试一次......终于在第一个终于?
或者是否有一些我可以用来检查状态的属性它?

谢谢!
请提出任何建议......





" ; RepStat" <一个******* @ discussions.microsoft.com>在消息中写道

新闻:5D ********************************** @ microsof t.com ...

如果我有代码如

SqConnection cndb = new SqlConnection(connstr);
SqlDataReader dr = null;
尝试
{cnb.Open();
SqlCommand cmd = new SqlCommand(" exec mysp",cndb);
dr = cmd.ExecuteReader();
}
catch(System.Exception e1){Debug.WriteLine(e1.Message);}
最后
{
if(dr!= null)dr.Close() ;
cndb.Close();
}








我有什么可能是博士非空,
但不开放 - 当finally块开始 - 因此导致行if(dr!= null)dr.Close();
失败,这将导致cndb.Close()永远不会发生,让
连接保持打开状态?这是在一个ASP.NET页面。
我当然不必再试一次......终于在第一个终于?
或者是否有一些我可以用来检查状态的属性它?

谢谢!
请提出任何建议......




你实际上想要在每个外面打开每个Disposable对象尝试

块,然后在最后关闭它。


在C#中它看起来像这样:


private static SqlConnection Connect()

{

SqConnection cndb = new SqlConnection(connstr);

cndb.Open();

返回cndb;

}

.. 。


使用(SqlConnection cndb = Connect())

{

SqlCommand cmd = new SqlCommand(" exec mysp" ,cndb);

使用(SqlDataReader dr = cmd.ExecuteReader())

{

while(dr.Read())

{

。 。 。

}

}

}


或者,更紧凑但更隐蔽


使用(SqlConnection cndb = Connect())

using(SqlDataReader dr = new SqlCommand(" exec mysp",cndb).ExecuteReader())

{

while(dr.Read())

{

。 。 。

}

}


David


非常好,谢谢!


" Bruno Jouhier [MVP]" < BJ ****** @ club-internet.fr>在留言中写道

新闻:O6 ************** @ TK2MSFTNGP10.phx.gbl ...

不,博士不能空值。如果失败,ExecuteReader将抛出异常,
它将不会返回null。

您可以简化所有这些并使用using重写它。声明
,因为你操作的所有对象都实现了IDisposable接口。
以下是我将如何重写它:

使用(SqlConnection cndb = new SqlConnection(connstr))
{
cndb.Open();
使用(SqlCommand cmd = new SqlCommand(" exec mysp",cndb))
{
使用(SqlDataReader dr = cmd。 ExecuteReader())
//
//你的东西
}
}
}

另外,除非你真的想登录在这个级别的异常(你
应该返回一个状态,表明操作失败),我不会在这里捕获
异常吗?我会让它向上传播,直到它达到战略状态。在你的代码(或框架)中指出它将记录
以及执行将在何处恢复。

Bruno。

RepStat <一个******* @ discussions.microsoft.com> écritdansle message de
新闻:5D ********************************** @ microsof t .com ...

如果我有代码如

SqConnection cndb = new SqlConnection(connstr);
SqlDataReader dr = null;
尝试
{cnb.Open();
SqlCommand cmd = new SqlCommand(" exec mysp",cndb);
dr = cmd.ExecuteReader();
}
catch(System.Exception e1){Debug.WriteLine(e1.Message);}
最后
{
if(dr!= null)dr.Close();
cndb.Close();
}










我有什么可能是博士非空,


但不是打开 - 当finally块开始时 - 从而导致行

if(dr!= null)dr.Close();
失败,这将导致cndb.Close()永远不会发生,打开


连接?

这是在一个ASP.NET页面。
我当然不必另外尝试...终于在最初的
内?或者是否有一些我可以用来检查它状态的属性?

谢谢!
请提出任何建议......



If I have code such a

SqConnection cndb = new SqlConnection(connstr)
SqlDataReader dr = null
tr

cndb.Open()
SqlCommand cmd = new SqlCommand("exec mysp", cndb)
dr = cmd.ExecuteReader()

catch (System.Exception e1) {Debug.WriteLine(e1.Message);
finall

if(dr != null) dr.Close()
cndb.Close()
what bugs me is that is there any possibility that dr would be non-null, but not open - when the finally block started - thus causing the lin
if(dr != null) dr.Close()
to fail, which would cause the cndb.Close() to never occur, leaving the connection open
This is in an ASP.NET page
Surely I don''t have to put another try...finally within the first finally
or is there some property I can use to check the state of it

Thanks
Any suggestions please...

解决方案

No, dr cannot be null. ExecuteReader will throw an exception if it fails, it
will not return null.

You can simplify all this and rewrite it with the "using" statement because
all the objects that you manipulate implement the IDisposable interface.
Here is how I would rewrite this:

using (SqlConnection cndb = new SqlConnection(connstr))
{
cndb.Open();
using (SqlCommand cmd = new SqlCommand("exec mysp", cndb))
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
// your stuff here
}
}
}

Also, unless you really want to log the exception at this level (you should
return a status indicating that the operation failed), I would not catch the
exception here? I would let it propagate upwards, until it reaches a
"strategic" point in your code (or in the framework) where it will be logged
and where the execution will resume.

Bruno.

"RepStat" <an*******@discussions.microsoft.com> a écrit dans le message de
news:5D**********************************@microsof t.com...

If I have code such as

SqConnection cndb = new SqlConnection(connstr);
SqlDataReader dr = null;
try
{
cndb.Open();
SqlCommand cmd = new SqlCommand("exec mysp", cndb);
dr = cmd.ExecuteReader();
}
catch (System.Exception e1) {Debug.WriteLine(e1.Message);}
finally
{
if(dr != null) dr.Close();
cndb.Close();
}

what bugs me is that is there any possibility that dr would be non-null, but not open - when the finally block started - thus causing the line if(dr != null) dr.Close();
to fail, which would cause the cndb.Close() to never occur, leaving the connection open? This is in an ASP.NET page.
Surely I don''t have to put another try...finally within the first finally?
or is there some property I can use to check the state of it?

Thanks!
Any suggestions please...




"RepStat" <an*******@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...

If I have code such as

SqConnection cndb = new SqlConnection(connstr);
SqlDataReader dr = null;
try
{
cndb.Open();
SqlCommand cmd = new SqlCommand("exec mysp", cndb);
dr = cmd.ExecuteReader();
}
catch (System.Exception e1) {Debug.WriteLine(e1.Message);}
finally
{
if(dr != null) dr.Close();
cndb.Close();
}

what bugs me is that is there any possibility that dr would be non-null, but not open - when the finally block started - thus causing the line if(dr != null) dr.Close();
to fail, which would cause the cndb.Close() to never occur, leaving the connection open? This is in an ASP.NET page.
Surely I don''t have to put another try...finally within the first finally?
or is there some property I can use to check the state of it?

Thanks!
Any suggestions please...



You actually want to open each Disposable object just outside of the try
block, and then close it in the finally.

In C# it would look like this:

private static SqlConnection Connect()
{
SqConnection cndb = new SqlConnection(connstr);
cndb.Open();
return cndb;
}
.. . .

using (SqlConnection cndb = Connect())
{
SqlCommand cmd = new SqlCommand("exec mysp", cndb);
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
. . .
}
}
}

or, the more compact, but slightly more cryptic

using (SqlConnection cndb = Connect())
using (SqlDataReader dr = new SqlCommand("exec mysp", cndb).ExecuteReader())
{
while (dr.Read())
{
. . .
}
}

David


excellent, thanks!

"Bruno Jouhier [MVP]" <bj******@club-internet.fr> wrote in message
news:O6**************@TK2MSFTNGP10.phx.gbl...

No, dr cannot be null. ExecuteReader will throw an exception if it fails, it will not return null.

You can simplify all this and rewrite it with the "using" statement because all the objects that you manipulate implement the IDisposable interface.
Here is how I would rewrite this:

using (SqlConnection cndb = new SqlConnection(connstr))
{
cndb.Open();
using (SqlCommand cmd = new SqlCommand("exec mysp", cndb))
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
// your stuff here
}
}
}

Also, unless you really want to log the exception at this level (you should return a status indicating that the operation failed), I would not catch the exception here? I would let it propagate upwards, until it reaches a
"strategic" point in your code (or in the framework) where it will be logged and where the execution will resume.

Bruno.

"RepStat" <an*******@discussions.microsoft.com> a écrit dans le message de
news:5D**********************************@microsof t.com...

If I have code such as

SqConnection cndb = new SqlConnection(connstr);
SqlDataReader dr = null;
try
{
cndb.Open();
SqlCommand cmd = new SqlCommand("exec mysp", cndb);
dr = cmd.ExecuteReader();
}
catch (System.Exception e1) {Debug.WriteLine(e1.Message);}
finally
{
if(dr != null) dr.Close();
cndb.Close();
}

what bugs me is that is there any possibility that dr would be non-null,


but not open - when the finally block started - thus causing the line

if(dr != null) dr.Close();
to fail, which would cause the cndb.Close() to never occur, leaving the


connection open?

This is in an ASP.NET page.
Surely I don''t have to put another try...finally within the first finally? or is there some property I can use to check the state of it?

Thanks!
Any suggestions please...




这篇关于尝试/最后使用数据访问代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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