的DbConnection [英] DbConnection

查看:100
本文介绍了的DbConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,新闻组:


我正在为(I)DbConnection创建一个包装器。我可以连接到数据库

并放置查询;但是,我在包装课上遇到了一些问题。


总之,我有以下几点......


公共课CDB

{

protected System.Data.IDbConnection m_conn;


...


}


现在,我已经读过,当我完成时我应该总是关闭我的连接。

所以当我上课时包装连接已经准备好进行垃圾回收了,我在
中关闭了析构函数中的连接。它如下......


~CDB()

{

if(this.m_conn.State == System .Data.ConnectionState.Open)

{

试试

{

this.m_conn.Close();

}

catch(例外e)

{

Console.WriteLine(e.Message);

Console.ReadLine(); //我添加这个只是为了让我看到这个消息。

}

}

}


问题是我总是收到异常 - 这是一个

InvalidOperationException - " Handle未初始化。我有什么想法?b $ b我做错了,或者如何纠正这种情况?谢谢。如果您需要更多我的代码,请不要犹豫要求。我只是觉得另一个

代码在这种情况下无关紧要。再次感谢你。

Trecius

Hello, Newsgroupians:

I am creating a wrapper for (I)DbConnection. I can connect to a database
and place queries; however, I''m having some problems with my wrapped class.

In short, I have the following...

public class CDB
{
protected System.Data.IDbConnection m_conn;

...

}

Now, I''ve read that I should ALWAYS close my connection when I''m finished.
So when my class that wraps the connection is ready for garbage collection, I
close the connection in the destructor. It is as follows...

~CDB()
{
if (this.m_conn.State == System.Data.ConnectionState.Open)
{
try
{
this.m_conn.Close();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine(); // I add this just so I can see the message.
}
}
}

The problem is I always receive the exception -- which is an
InvalidOperationException -- "Handle is not initialized." Any ideas what I
am doing wrong, or how to rectify the situation? Thank you. Should you need
more of my code, please do not hesistate to ask. I just thought the other
code was irrelevant in this situation. Thank you again.
Trecius

推荐答案



代码的哪一部分抛出异常?当您检查连接

状态或尝试关闭连接时?


Adrian。

-

[如果对你有帮助,请标明我的答案]


" Trecius"写道:

Which part of the code throws the exception? When you check the connection
status or when you try to close the connection?

Adrian.
--
[Please mark my answer if it was helpful to you]


"Trecius" wrote:

你好,新闻组:


我正在为(I)DbConnection创建一个包装器。我可以连接到数据库

并放置查询;但是,我在包装课上遇到了一些问题。


总之,我有以下几点......


公共课CDB

{

protected System.Data.IDbConnection m_conn;


...


}


现在,我已经读过,当我完成时我应该总是关闭我的连接。

所以当我上课时包装连接已经准备好进行垃圾回收了,我在
中关闭了析构函数中的连接。它如下......


~CDB()

{

if(this.m_conn.State == System .Data.ConnectionState.Open)

{

试试

{

this.m_conn.Close();

}

catch(例外e)

{

Console.WriteLine(e.Message);

Console.ReadLine(); //我添加这个只是为了让我看到这个消息。

}

}

}


问题是我总是收到异常 - 这是一个

InvalidOperationException - " Handle未初始化。我有什么想法?b $ b我做错了,或者如何纠正这种情况?谢谢。如果您需要更多我的代码,请不要犹豫要求。我只是觉得另一个

代码在这种情况下无关紧要。再次感谢你。


Trecius
Hello, Newsgroupians:

I am creating a wrapper for (I)DbConnection. I can connect to a database
and place queries; however, I''m having some problems with my wrapped class.

In short, I have the following...

public class CDB
{
protected System.Data.IDbConnection m_conn;

...

}

Now, I''ve read that I should ALWAYS close my connection when I''m finished.
So when my class that wraps the connection is ready for garbage collection, I
close the connection in the destructor. It is as follows...

~CDB()
{
if (this.m_conn.State == System.Data.ConnectionState.Open)
{
try
{
this.m_conn.Close();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine(); // I add this just so I can see the message.
}
}
}

The problem is I always receive the exception -- which is an
InvalidOperationException -- "Handle is not initialized." Any ideas what I
am doing wrong, or how to rectify the situation? Thank you. Should you need
more of my code, please do not hesistate to ask. I just thought the other
code was irrelevant in this situation. Thank you again.
Trecius





Don'不要使用终结器。让你的类实现IDisposable和

Dispose()调用连接上的Close()。每当你的自定义类

包含一个IDisposable的实例变量时,你的类也需要

需要IDisposable。


自连接本身就是一个托管对象,对你来说不合适

在终结器中引用它(它可能已经完成了

)。


HTH,


Sam

------------------ ------------------------------------------

我们正在招聘! B-Line Medical正在寻求.NET

开发人员在医疗产品中的激动人心的职位

开发MD / DC。在轻松的团队环境中使用各种技术

。在Dice.com上查看广告。


2007年8月15日星期三10:52:01 -0700,Trecius

< Tr ***** @ discuss.microsoft.comwrote:


Don''t use a finalizer. Make your class implement IDisposable and in
Dispose() call Close() on the connection. Any time your custom class
contains an instance variable which is IDisposable, your class also
needs to be IDisposable.

Since the connection is itself a managed object it''s improper for you
to reference it at all in a finalzer (it may have been finalized
already).

HTH,

Sam
------------------------------------------------------------
We''re hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Wed, 15 Aug 2007 10:52:01 -0700, Trecius
<Tr*****@discussions.microsoft.comwrote:

> Hello,Newsgroupians:

~CDB()
{

if(this.m_conn.State == System.Data.ConnectionState.Open)

{

}
}
InvalidOperationException - " Handle未初始化。我有什么想法做错了,或者如何纠正这种情况?谢谢。如果您需要更多我的代码,请不要犹豫要求。我只是觉得其他的代码在这种情况下无关紧要。再次感谢。

Trecius
>Hello, Newsgroupians:
~CDB()
{
if (this.m_conn.State == System.Data.ConnectionState.Open)
{
}
}

The problem is I always receive the exception -- which is an
InvalidOperationException -- "Handle is not initialized." Any ideas what I
am doing wrong, or how to rectify the situation? Thank you. Should you need
more of my code, please do not hesistate to ask. I just thought the other
code was irrelevant in this situation. Thank you again.
Trecius


创建异常的方法是this.m_conn.Close()。 />
Trecius


" Adrian Voicu"写道:
The method that is creating the exception is this.m_conn.Close().
Trecius

"Adrian Voicu" wrote:

>

哪部分代码会引发异常?当您检查连接

状态或尝试关闭连接时?


Adrian。

-

[如果对你有帮助,请标明我的答案]


" Trecius"写道:
>
Which part of the code throws the exception? When you check the connection
status or when you try to close the connection?

Adrian.
--
[Please mark my answer if it was helpful to you]


"Trecius" wrote:

你好,新闻组:


我正在为(I)DbConnection创建一个包装器。我可以连接到数据库

并放置查询;但是,我在包装课上遇到了一些问题。


总之,我有以下几点......


公共课CDB

{

protected System.Data.IDbConnection m_conn;


...


}


现在,我已经读过,当我完成时我应该总是关闭我的连接。

所以当我上课时包装连接已经准备好进行垃圾回收了,我在
中关闭了析构函数中的连接。它如下......


~CDB()

{

if(this.m_conn.State == System .Data.ConnectionState.Open)

{

试试

{

this.m_conn.Close();

}

catch(例外e)

{

Console.WriteLine(e.Message);

Console.ReadLine(); //我添加这个只是为了让我看到这个消息。

}

}

}


问题是我总是收到异常 - 这是一个

InvalidOperationException - " Handle未初始化。我有什么想法?b $ b我做错了,或者如何纠正这种情况?谢谢。如果您需要更多我的代码,请不要犹豫要求。我只是觉得另一个

代码在这种情况下无关紧要。再次感谢你。

Trecius
Hello, Newsgroupians:

I am creating a wrapper for (I)DbConnection. I can connect to a database
and place queries; however, I''m having some problems with my wrapped class.

In short, I have the following...

public class CDB
{
protected System.Data.IDbConnection m_conn;

...

}

Now, I''ve read that I should ALWAYS close my connection when I''m finished.
So when my class that wraps the connection is ready for garbage collection, I
close the connection in the destructor. It is as follows...

~CDB()
{
if (this.m_conn.State == System.Data.ConnectionState.Open)
{
try
{
this.m_conn.Close();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine(); // I add this just so I can see the message.
}
}
}

The problem is I always receive the exception -- which is an
InvalidOperationException -- "Handle is not initialized." Any ideas what I
am doing wrong, or how to rectify the situation? Thank you. Should you need
more of my code, please do not hesistate to ask. I just thought the other
code was irrelevant in this situation. Thank you again.
Trecius


这篇关于的DbConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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