jinfeng_wang

G-G-S,D-D-U!

BlogJava 首页 新随笔 联系 聚合 管理
  400 Posts :: 0 Stories :: 296 Comments :: 0 Trackbacks

http://groups.google.com/group/microsoft.public.dotnet.framework.adonet/browse_frm/thread/eca73047adca7edb/12f1ae6ae176afe1?lnk=raot#12f1ae6ae176afe1



1. jinfeng_W...@msn.com
 1月20日 上午9时56分   显示选项

新闻论坛:microsoft.public.dotnet.framework.adonet
发件人: jinfeng_W...@msn.com - 查找此作者的帖子 
日期:19 Jan 2006 17:56:57 -0800
当地时间:2006年1月20日(星期五) 上午9时56分 
主题:the difference between SqlConnection.IDisposable.Disp­ose() and SqlConnection.Dispose()
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 删除 | 报告滥用行为 

hi, I have a question about the difference between
SqlConnection.IDisposable.Dispose()  and  SqlConnection.Dispose(). Both
 of them realize the function of releasing the connection to the
ConnectionPool?  Do they have the same effection source code?  If they
are different, who can tell me the differences? If they are same, why
MS gives the SqlConnection.IDisposable.Dispose, but  only
SqlConnection.Dispose() method?


In the MSDN, there are following description about the
SqlConnection.IDisposable.Dispose Method:
  "This member supports the .NET Framework infrastructure and is not
intended to be used directly from your code."  what's the meaning of
it?


If the user has called the SqlConnection.IDisposable.Dispose() in the
client application, what probem results in?  and if there are some
problem becomes, then why did MS give us such a method?


in the same, who can tell me the using of
"SqlConnection.ICloneable.Clone ",
"SqlConnection.IDbConnection.BeginTransaction" and
"SqlConnection.IDbConnection.CreateCommand"?


Anybody can help me to solve my question? thanks a lot.


回复
             
     


   2. Cor Ligthert [MVP]
 1月20日 下午3时10分   显示选项

新闻论坛:microsoft.public.dotnet.framework.adonet
发件人: "Cor Ligthert [MVP]" <notmyfirstn...@planet.nl> - 查找此作者的帖子 
日期:Fri, 20 Jan 2006 08:10:00 +0100
主题:Re: the difference between SqlConnection.IDisposable.Disp­ose() and SqlConnection.Dispose()
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 

JinFeng,


Both of them are removing the connectionstring from the connectionobject.
They both have nothing to do direct with the ConnectionPool, although you
should close a connection either by close or dispose to let the
ConnectionPool do its job.


Every Interface can be used to get its members (in the implementing
contract) from the implementing class. Therefore it is an interface. A good
programmer start the name of his interfaces all with a capital I.


I hope that this gives an idea


Cor


回复
             
     


   3. jinfeng_Wang@msn.com
 1月20日 下午5时52分   显示选项

新闻论坛:microsoft.public.dotnet.framework.adonet
发件人: "jinfeng_W...@msn.com" <jinfeng_W...@msn.com> - 查找此作者的帖子 
日期:20 Jan 2006 01:52:12 -0800
当地时间:2006年1月20日(星期五) 下午5时52分 
主题:Re: the difference between SqlConnection.IDisposable.Disp­ose() and SqlConnection.Dispose()
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 删除 | 报告滥用行为 

hello, Cor . thanks for you answer. but it's not what i want.


please read the following URL and look at the left frame:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpre...


there are two methods in the SQLConnection:
 "Dispose"  and  "SqlConnection.IDisposable.Dispose" Method.


and in MSDN , the description about the
"SqlConnection.IDisposable.Dispose" is:  "This member supports the .NET
Framework infrastructure and is not intended to be used directly from
your code".
can you help to tell me, what's the meaning of the above setence? MS
advice me that i should not call the
"SqlConnection.IDisposable.Dispose", yeah?


if there is a client code, like this  (copied from  MSDN, and i have
modified it):
public void ReadMyData()
{
    String      myConnString = "Persist Security Info=False;User
ID=sa;Initial Catalog=Northwind;Data Source=DTK-S-SVR;User
ID=sa;Pwd=sa";
   string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
   SqlConnection myConnection = new SqlConnection(myConnString);
   SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
   myConnection.Open();
   SqlDataReader myReader;
   myReader = myCommand.ExecuteReader();
   while (myReader.Read())
      {
        Console.WriteLine(myReader.GetInt32(0) + ", " +
myReader.GetString(1));
     }
    myReader.Close();


     //myConnection.Close();  // old source code.
     IDisposable disposable =myConnection as IDisposable;   // new
source code.
     disposable.Dispose();   // new source code.


}


will it cause some problem when cast the myConnection  to IDisposable
and call  disposable.Dispose() ?
Does the "new souce code" have the same effect as the "old source code"
??
if they are, why MS implements the method of "IDisposable.Disp­ose()"
explicity. I means: there is only the SqlConnection.Dispose() method.

if there is no problem here, then can you why MS said that "This member
supports the .NET Framework infrastructure and is not intended to be
used directly from your code."??
if there is some problem, then why MS does not make the methods of
SqlConnection.IDisposable.Disp­­ose() and SqlConnection.Dispose()
shared the same source code? and why MS tell us that there exists a
"SqlConnection.IDisposable.Disp­­ose()" method, but warn us not to
call it??


in the same, how about the "SqlConnection.ICloneable.Clone ",
"SqlConnection.IDbConnection.BeginTransaction" and
"SqlConnection.IDbConnection.CreateCommand"?


i do not whether you have understand my question for my poor english
and expression.
can you help me? anyway, thanks to all of you.


回复
             
     


   4. Cor Ligthert [MVP]
 1月20日 下午6时29分   显示选项

新闻论坛:microsoft.public.dotnet.framework.adonet
发件人: "Cor Ligthert [MVP]" <notmyfirstn...@planet.nl> - 查找此作者的帖子 
日期:Fri, 20 Jan 2006 11:29:36 +0100
当地时间:2006年1月20日(星期五) 下午6时29分 
主题:Re: the difference between SqlConnection.IDisposable.Disp­ose() and SqlConnection.Dispose()
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 

JinFeng,


Both Frans and me are not first language English speakers (AFAIK is Frans
born where the native language is Fries and I where it is Dutch). In my
opinion should you never excuse you for your English in these newsgroups.
Almost everybody, no matter how good he can speak a language, will make
errors in email messages (even in his own). I assume that you are using the
English version of Visual Studio so that tells enough.


I thought that the protected dispose is used by component (don't mix this up
with the rest from what I write about component). If you open a form or a
component (by the designer), than you see the implementation of IDisposable
direct. That part is used to do the most of the disposing.


Nice pages about Idisposable are these new ones


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpre...


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpre...


As I said, be aware that if you use a form or a component the code is
already in the designer created part.


However as long discussions has be done in this newsgroup. Disposing and
Closing have the same effect on the connection pool.


I hope this gives some information.


Cor


回复
             
     


   5. Frans Bouma [C# MVP]
 1月20日 下午4时22分   显示选项

新闻论坛:microsoft.public.dotnet.framework.adonet
发件人: "Frans Bouma [C# MVP]" <perseus.usenetNOS...@xs4all.nl> - 查找此作者的帖子 
日期:Fri, 20 Jan 2006 00:22:50 -0800
当地时间:2006年1月20日(星期五) 下午4时22分 
主题:Re: the difference between SqlConnection.IDisposable.Disp­ose() and SqlConnection.Dispose()
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 

 


- 隐藏被引用文字 -
- 显示引用的文字 -

jinfeng_W...@msn.com wrote:
> hi, I have a question about the difference between
> SqlConnection.IDisposable.Dispose()  and  SqlConnection.Dispose().
> Both  of them realize the function of releasing the connection to the
> ConnectionPool?  Do they have the same effection source code?  If they
> are different, who can tell me the differences? If they are same, why
> MS gives the SqlConnection.IDisposable.Dispose, but  only
> SqlConnection.Dispose() method?

> In the MSDN, there are following description about the
> SqlConnection.IDisposable.Dispose Method:
>   "This member supports the .NET Framework infrastructure and is not
> intended to be used directly from your code."  what's the meaning of
> it?


> If the user has called the SqlConnection.IDisposable.Dispose() in the
> client application, what probem results in?  and if there are some
> problem becomes, then why did MS give us such a method?


> in the same, who can tell me the using of
> "SqlConnection.ICloneable.Clone ",
> "SqlConnection.IDbConnection.BeginTransaction" and
> "SqlConnection.IDbConnection.CreateCommand"?


> Anybody can help me to solve my question? thanks a lot.

 

        what does 'SqlConnection.IDisposable.Dispose' mean? 'IDisposable'
isn't a property or something of SqlConnection. 'Dispose()' is a method
in Component, the base class of SqlConnection. SqlConnection overrides
Dispose(true), which is called from Dispose(), and therefore whatever
Dispose() you call, it doesnt matter.

                FB


--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------


回复
             
     


   6. jinfeng_Wang@msn.com
 1月20日 下午5时55分   显示选项

新闻论坛:microsoft.public.dotnet.framework.adonet
发件人: "jinfeng_W...@msn.com" <jinfeng_W...@msn.com> - 查找此作者的帖子 
日期:20 Jan 2006 01:55:46 -0800
当地时间:2006年1月20日(星期五) 下午5时55分 
主题:Re: the difference between SqlConnection.IDisposable.Disp­ose() and SqlConnection.Dispose()
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 删除 | 报告滥用行为 

FB, i know that 'IDisposable'  is not one property of the
SQLConnection. please read my answer to Cor.
'SqlConnection.IDisposable.Dispose'  is copied from MSDN.    :-)
I think it means that SQLConnection has implemnt the Dispose() method
of the IDisposable explicity.


I want to know the difference between the two method of
SqlConnection.IDisposable.Disp­­ose() and SqlConnection.Dispose().


Thanks to you!!! thanks!


回复
             
     


   7. Frans Bouma [C# MVP]
 1月21日 下午6时54分   显示选项

新闻论坛:microsoft.public.dotnet.framework.adonet
发件人: "Frans Bouma [C# MVP]" <perseus.usenetNOS...@xs4all.nl> - 查找此作者的帖子 
日期:Sat, 21 Jan 2006 02:54:24 -0800
当地时间:2006年1月21日(星期六) 下午6时54分 
主题:Re: the difference between SqlConnection.IDisposable.Disp­ose() and SqlConnection.Dispose()
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 

 

jinfeng_W...@msn.com wrote:
> FB, i know that 'IDisposable'  is not one property of the
> SQLConnection. please read my answer to Cor.
> 'SqlConnection.IDisposable.Dispose'  is copied from MSDN.    :-)
> I think it means that SQLConnection has implemnt the Dispose() method
> of the IDisposable explicity.


        I thought that it meant that, but checking SqlConnection in reflector
I couldn't find IDisposable explicit implementations :D Hence my
question :)


> I want to know the difference between the two method of
> SqlConnection.IDisposable.Disp­­ose() and SqlConnection.Dispose().


        I have no idea.

                FB


--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------


回复
             
     


   8. jinfeng_Wang@msn.com
 1月23日 上午9时17分   显示选项

新闻论坛:microsoft.public.dotnet.framework.adonet
发件人: "jinfeng_W...@msn.com" <jinfeng_W...@msn.com> - 查找此作者的帖子 
日期:22 Jan 2006 17:17:00 -0800
主题:Re: the difference between SqlConnection.IDisposable.Disp­ose() and SqlConnection.Dispose()
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 删除 | 报告滥用行为 

:-)


copied from Reflector:


System.Data.SqlClient.SqlConnection.System.Data.IDbConnection.BeginTransact­ion()
: IDbTransaction
IDbTransaction IDbConnection.BeginTransaction()
{
      return this.BeginTransaction();

 

}


I think that the disposable is same as here .
but, but  why MS does such a foolish action :-(

this folliwing is copied from MSDN  in the
SqlConnection.IDbConnection.BeginTransaction Method ():
"This member supports the .NET Framework infrastructure and is not
intended to be used directly from your code."
faint to death. ~


回复
             
     


   9. William (Bill) Vaughn
 1月22日 上午2时55分   显示选项

新闻论坛:microsoft.public.dotnet.framework.adonet
发件人: "William \(Bill\) Vaughn" <billvaRemoveT...@nwlink.com> - 查找此作者的帖子 
日期:Sat, 21 Jan 2006 10:55:46 -0800
当地时间:2006年1月22日(星期日) 上午2时55分 
主题:Re: the difference between SqlConnection.IDisposable.Disp­ose() and SqlConnection.Dispose()
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 

Ok... if you're that curious, use Reflector to walk through the .Net
Framework code to see what it does. You must have a lot more time on your
hands that we do.


Frankly, it does not matter what they do. You don't need to call
them--either of them. As long as you use Close on the Connection you're
fine. Sure, you can call Dispose if you want to, but it won't help the
problem you're trying to solve.


--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________


"Frans Bouma [C# MVP]" <perseus.usenetNOS...@xs4all.nl> wrote in message
news:xn0ehgc512v14b001@news.microsoft.com...

 

- 隐藏被引用文字 -
- 显示引用的文字 -

> jinfeng_W...@msn.com wrote:

>> hi, I have a question about the difference between
>> SqlConnection.IDisposable.Dispose()  and  SqlConnection.Dispose().
>> Both  of them realize the function of releasing the connection to the
>> ConnectionPool?  Do they have the same effection source code?  If they
>> are different, who can tell me the differences? If they are same, why
>> MS gives the SqlConnection.IDisposable.Dispose, but  only
>> SqlConnection.Dispose() method?


>> In the MSDN, there are following description about the
>> SqlConnection.IDisposable.Dispose Method:
>>   "This member supports the .NET Framework infrastructure and is not
>> intended to be used directly from your code."  what's the meaning of
>> it?


>> If the user has called the SqlConnection.IDisposable.Dispose() in the
>> client application, what probem results in?  and if there are some
>> problem becomes, then why did MS give us such a method?


>> in the same, who can tell me the using of
>> "SqlConnection.ICloneable.Clone ",
>> "SqlConnection.IDbConnection.BeginTransaction" and
>> "SqlConnection.IDbConnection.CreateCommand"?


>> Anybody can help me to solve my question? thanks a lot.


> what does 'SqlConnection.IDisposable.Dispose' mean? 'IDisposable'
> isn't a property or something of SqlConnection. 'Dispose()' is a method
> in Component, the base class of SqlConnection. SqlConnection overrides
> Dispose(true), which is called from Dispose(), and therefore whatever
> Dispose() you call, it doesnt matter.


> FB


> --
> ------------------------------------------------------------------------
> Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
> My .NET blog: http://weblogs.asp.net/fbouma
> Microsoft MVP (C#)
> ------------------------------------------------------------------------

 

回复
             
     


   10. Frans Bouma [C# MVP]
 1月22日 下午6时52分   显示选项

新闻论坛:microsoft.public.dotnet.framework.adonet
发件人: "Frans Bouma [C# MVP]" <perseus.usenetNOS...@xs4all.nl> - 查找此作者的帖子 
日期:Sun, 22 Jan 2006 02:52:11 -0800
当地时间:2006年1月22日(星期日) 下午6时52分 
主题:Re: the difference between SqlConnection.IDisposable.Disp­ose() and SqlConnection.Dispose()
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 

 

William (Bill) Vaughn wrote:
> Ok... if you're that curious, use Reflector to walk through the .Net
> Framework code to see what it does. You must have a lot more time on
> your hands that we do.

> Frankly, it does not matter what they do. You don't need to call
> them--either of them. As long as you use Close on the Connection
> you're fine. Sure, you can call Dispose if you want to, but it won't
> help the problem you're trying to solve.

 

        On a side note: not all ADO.NET providers' connection objects can
live without a Dispose call. For example the Firebird .NET provider and
the ODP.NET providers do need a call to Dispose to properly clean up.
(especially firebird, for cleaning up on the server side!).

        FB, who still couldn't find an explicit IDisposable implementation on
SqlConnection...


--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------


回复
 
11. jinfeng_Wang@msn.com
 1月23日 上午8时55分   显示选项

新闻论坛:microsoft.public.dotnet.framework.adonet
发件人: "jinfeng_W...@msn.com" <jinfeng_W...@msn.com> - 查找此作者的帖子 
日期:22 Jan 2006 16:55:26 -0800
当地时间:2006年1月23日(星期一) 上午8时55分 
主题:Re: the difference between SqlConnection.IDisposable.Disp­ose() and SqlConnection.Dispose()
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 删除 | 报告滥用行为 

thanks all of you.


If i was just a client programmer, i think all the information from you
and MSDN is enough.
but,...  now i am trying  to develop one new .NET Data Provider for own
database.
so i want  to know what has happened in SQL .NET Data Provider.
it give me a dozens of puzzling interface.
here is "SqlConnection.IDisposable.Disp­­ose() " and
"SqlConnection.Dispose()".


In fact, the SQLConnection is inherited from Compnent, which has
implemented the IDisposable.
This is very like the question of "deadly diamond", that one Class
inherits one Interface from TWO path.
             IDisposable
              /                \
              |                 |
Component       IDBConnection
              |                 |
              \                /
            SQLConnection


if there is no difference between the
SqlConnection.IDisposable.Disp­­ose() and SqlConnection.Dispose(),
then MS has no need to implement the
SqlConnection.IDisposable.Disp­­ose() explicitly. But, it has done
it, that means there are some differences between them. What's that?
MSDN has not told us, it just warn us not to call  the
SqlConnection.IDisposable.Disp­­ose() in the client program.    :(


In the  Oracle® Data Provider for .NET,
     public sealed class OracleConnection : Component, IDbConnection,
ICloneable
but, OracleConnection has not implement the
IDbConnection.IDisposable.Disposable() explicity.


MS, :-(


 14. Frans Bouma [C# MVP]
 1月23日 下午5时54分   显示选项

新闻论坛:microsoft.public.dotnet.framework.adonet
发件人: "Frans Bouma [C# MVP]" <perseus.usenetNOS...@xs4all.nl> - 查找此作者的帖子 
日期:Mon, 23 Jan 2006 01:54:56 -0800
当地时间:2006年1月23日(星期一) 下午5时54分 
主题:Re: the difference between SqlConnection.IDisposable.Disp­ose() and SqlConnection.Dispose()
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 

 

jinfeng_W...@msn.com wrote:
> If i was just a client programmer, i think all the information from
> you and MSDN is enough.
> but,...  now i am trying  to develop one new .NET Data Provider for
> own database.


        there are guidelines for that if I'm not mistaken. And I think you
should relax a little. I work with a lot of ADO.NET providers and none
of them works the same as the others. So 'what should be done' is what
you think is the easiest for your users :).

        So, 'Close()' should clean up, and for example connection.Dispose()
should also dispose commands, parameters etc.


        In general derive a class from DbConnection, and override the specific
methods to add your own code.

 

> so i want  to know what has happened in SQL .NET Data Provider.
> it give me a dozens of puzzling interface.
> here is "SqlConnection.IDisposable.Disp­­ose() " and
> "SqlConnection.Dispose()".


        Have you looked into the code with reflector ? I think you should do
that. :)

 

- 隐藏被引用文字 -
- 显示引用的文字 -

> In fact, the SQLConnection is inherited from Compnent, which has
> implemented the IDisposable.
> This is very like the question of "deadly diamond", that one Class
> inherits one Interface from TWO path.
>              IDisposable
>               /                \
>               |                 |
> Component       IDBConnection
>               |                 |
>               \                /
>             SQLConnection

> if there is no difference between the
> SqlConnection.IDisposable.Disp­­ose() and SqlConnection.Dispose(),
> then MS has no need to implement the
> SqlConnection.IDisposable.Disp­­ose() explicitly. But, it has done
> it, that means there are some differences between them. What's that?
> MSDN has not told us, it just warn us not to call  the
> SqlConnection.IDisposable.Disp­­ose() in the client program.    :(

 

        I looked up the page, and I can only get to that page through the
index. So I think it's a mistake in the MSDN. As said by others in this
thread, look at the code through reflector first, then come back here
with questions.

        Also, inherited interfaces are simply type definitions, not
implementations. So 1 routine can serve the Dispose() method of
multiple interfaces.

 

> In the  Oracle® Data Provider for .NET,
>      public sealed class OracleConnection : Component, IDbConnection,
> ICloneable
> but, OracleConnection has not implement the
> IDbConnection.IDisposable.Disposable() explicity.

> MS, :-(

 

        Neither has sqlconnection!!! Look into the code! Just because it's an
error in the MSDN doesn't mean it's true. Why do you ignore what we
said and keep believing an errorous page in the msdn?

                FB


--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------


回复
             
     

 
   15. jinfeng_Wang@msn.com
 1月23日 下午10时15分   显示选项

新闻论坛:microsoft.public.dotnet.framework.adonet
发件人: "jinfeng_W...@msn.com" <jinfeng_W...@msn.com> - 查找此作者的帖子 
日期:23 Jan 2006 06:15:59 -0800
当地时间:2006年1月23日(星期一) 下午10时15分 
主题:Re: the difference between SqlConnection.IDisposable.Disp­ose() and SqlConnection.Dispose()
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 删除 | 报告滥用行为 

:-)


MSDN Error?
why the MS make such an Error .
I have said that  now i am developing one .NET Data Provider for my own
database, so i should know the everything what have happened in the SQL
.NET  Data Provider, and learning something from the MS's code.


now i have realized why MS  give us such a SQL .NET  Data Provider.
wait a minute, and i  will write it clearly.


回复
             
     

 
   16. jinfeng_Wang@msn.com
 1月23日 下午11时10分   显示选项

新闻论坛:microsoft.public.dotnet.framework.adonet
发件人: "jinfeng_W...@msn.com" <jinfeng_W...@msn.com> - 查找此作者的帖子 
日期:23 Jan 2006 07:10:37 -0800
当地时间:2006年1月23日(星期一) 下午11时10分 
主题:Re: the difference between SqlConnection.IDisposable.Disp­ose() and SqlConnection.Dispose()
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 删除 | 报告滥用行为 

firstly, let's take a look at the following code.


///--------------
        public interface myInterface
        {
                Object getobject();
        }


        public class MyImplement : myInterface
        {
                public String getobject()    //override.
                {
                        return "str";
                }
        }///---------------
the code above can not be compiled, the compiler will have give us an
error.  Because the method of MyImplement.getobject() has a wrong
return type, which is "String", but the method of
MyInterface.getobject() declared that the return type is "Object:. Here
the compiler take the "return type" into the "override" compile
progregess. If MyImplement.getobject() returns "String",  this is not
the implementation of MyInterface.getobject().


now let's take a look at another code.
///---------------
        public class AnotherImplement
        {
                public object getobject()
                {
                        return new object();
                }


                public string getobject()  //overload
                {
                        return new string("");
                }


        }
///---------------
Here I want to overload the method of "getobject()", but the compiler
give us an error. Here the compiler DOES NOT take the "return type"
into the "overload" compile progregess (DIFFERENCE WITH THE OVERRIDE).


Now let's go back to the IDBConnection and SQLConnection.
In the interface of IDbConnection, it declares one method of
"createcomand".
///----------
IDbConnection {
    ....
    IDbCommand CreateCommand();
   ....


}


///----------
When design the interface of IDBConnection, the designer does not
what's kind of "Command" will be returned, for example
"SQLCommand","OracleCommand". so in the IDbConnection.CreateCommand(),
it only return an interface of "IDbCommand".

Now we are design the SQLConnection which implements the interface of
"IDbConnection". if in the SQLConnection, it only write :
///---------
  SQLConnection:IDbConnection {
   ....
      SQLCommand  CreateCommand() {
              ........
      }
   ....
 }
///---------
the compiler will give us an error, because  the "return type" is
SQLCommand, but not IDbCommand(defined in the IDbConnection). THE
COMPILER TAKE THE RETURN TYPE INTO THE COMPILE PROGRESS. so we must
give another definition of "IDbConnection.CreateCommand()".


if we write the code as follows:
///---------
  SQLConnection:IDbConnection {
   ....
      SQLCommand  CreateCommand() {
              ........
      }


      IDBCommand CreateCommand() {
              .......
     }
   ....
 }
///---------
The compiler will give us an error. THE COMPILER DOES NOT TAKE THE
RETURN TYPE INTO THE COMPILE PROGRESS.


so the SQLConnection have to implement  the
IDbConncection.CreateCommand explicyly.
///--------
   SQLConnection:IDbConnection {
   ....
      SQLCommand  CreateCommand() {
              ........
      }


      IDBCommand  IDbConnection.CreateCommand() {
              return this.CreateCommand();
     }
   ....
 }
///---------
The above is the  SQLConnection.


============BUT============
The MSDN told us the "we should call the
SQLConnection.IDbConnection.CreateCommand() in the client program".
that is, "we should use the SQLConnection.CreateCommand() in the client
program."
THIS will result a foolish result. In the client pogram, we will write
such a code:
///------------
   void doSomething() {
         SqlConnection  conn = new SqlConnection(connectionString);
         SqlCommand command = conn.CreateCommand();
                           //  We MUST have call
SqlConnection.CreateCommand(),
                           //  not
SqlConnection.IDbConnection.CreateCommand(),
                           //  which is suggested by MSDN.
   }
///------------
The above client code has violated the programming rule: "PROGRAMMING
TO INTERFACE".
According to the MSDN, we can not "PROGRAM TO IDbCommand".
The action of  "SqlConnection.CreateCommand()"  is difference with  the
action of"IDbConnection.CreeateCommand()".


Now if i want to shift to the Oracle database.   All of the client code
must be modified, because the above is "PROGRAM TO SqlCommand".  :-(
What a foolish ADO.NET FrameWork.


I am a java programmer. If i design the SQLConnection, i will give a
such a implemention:
///--------
   SQLConnection:IDbConnection {
   ....
      IDBCommand  CreateCommand() {
              ........
      }


      SqlCommand  CreateSqlCommand() {
              ........
     }
   ....
 }
///---------
If the client programmer want to "PROGRAM TO SqlCommand",  he will call
the method of SqlConnection.CreateSqlCommand().  If the client
programmer want to "PROGRAMM TO IDbConnection", he will call the
SqlConnection.CreateCommand(). Now The action of
"SqlConnection.CreateCommand()"  is same as   the action
of"IDbConnection.CreeateCommand()".
Because the client code is "PROGRAM TO IDbCommand", so if the client
program shifts to Oracle database, the client code will not have to
modify all the code.


回复
 

posted on 2006-01-23 09:35 jinfeng_wang 阅读(949) 评论(0)  编辑  收藏 所属分类: .Net

只有注册用户登录后才能发表评论。


网站导航: