通过ref传入SqlConnection [英] Passing in SqlConnection by ref

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

问题描述

你好,


这里是场景:


对象A打开一个Sql Db连接来执行多个SqlCommands。然后它需要将这个连接传递给对象B的构造函数,而对象B的构造函数又会在同一个连接上执行更多命令。

我有要理解如果SqlConnection作为value传递给
(unboxed),对象B将创建自己的SqlConnection副本,所以当

对象A关闭其连接时,对于对象B'的副本,它仍然是开放的。这是

是否正确?是不是通过ref传递它的正确方法?什么是最好的

做法?


谢谢。

Hello,

Here''s the scenario:

Object A opens a Sql Db connection to execute number of SqlCommands. Then it
needs to pass this connection to a constructor of object B which in turn
executes more commands on the same connection.
I have an understanding that if SqlConnection is passed as "value"
(unboxed), object B will create its own copy of SqlConnection, so when
object A closes its connection, it remains open for object B''s copy. Is this
correct? Is not the right way to pass it by ref? What are "the best
practices"?

Thank you.

推荐答案

SqlConnection是一个对象,默认情况下将通过引用传递。


中创建SqlConnections使用块并使用try / catch / finally块访问它,确保

关闭finally块中的连接。


DalePres

MCAD,MCDBA,MCSE

" LP" < lp@a.com>在消息中写道

news:uO ************** @ TK2MSFTNGP10.phx.gbl ...
The SqlConnection is an object and will be passed by reference by default.

Just as a suggestion, you should typically create your SqlConnections in a
using block and access it using a try/catch/finally block, making sure to
close the connection in the finally block.

DalePres
MCAD, MCDBA, MCSE

"LP" <lp@a.com> wrote in message
news:uO**************@TK2MSFTNGP10.phx.gbl...
你好,
这是场景:

对象A打开Sql Db连接以执行多个SqlCommands。然后
它需要将此连接传递给对象B的构造函数,而构造函数又在同一连接上执行更多命令。
我理解如果SqlConnection传递为value
(未装箱),对象B将创建自己的SqlConnection副本,因此当对象A关闭其连接时,它对于对象B'的副本保持打开状态。这个
对吗?是不是通过ref传递它的正确方法?什么是最好的
实践?

谢谢。
Hello,

Here''s the scenario:

Object A opens a Sql Db connection to execute number of SqlCommands. Then
it
needs to pass this connection to a constructor of object B which in turn
executes more commands on the same connection.
I have an understanding that if SqlConnection is passed as "value"
(unboxed), object B will create its own copy of SqlConnection, so when
object A closes its connection, it remains open for object B''s copy. Is
this
correct? Is not the right way to pass it by ref? What are "the best
practices"?

Thank you.



在C#中,当你通过ref传递它时:


public void MyMethod(ref SqlConnection oConn)


它是一个指针of of sort回到原始对象。你将

以确保在后续方法调用中捕获所有错误

使用此引用,因此主方法可以确保

对象被处理掉,连接关闭。


-

2005 Microsoft MVP C#

Robbe Morris
< a rel =nofollowhref =http://www.robbemorris.comtarget =_ blank> http://www.robbemorris.com
http://www.mastervb.net/home/ng/foru...t10017013.aspx


" LP" < lp@a.com>在消息中写道

news:uO ************** @ TK2MSFTNGP10.phx.gbl ...
In C#, when you pass this by ref:

public void MyMethod(ref SqlConnection oConn)

it is a pointer "of sorts" back to the original object. You''ll
to be sure that you trap all errors in subsequent method calls
with this reference so the master method can ensure the
object gets disposed of and the connection closed.

--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.mastervb.net/home/ng/foru...t10017013.aspx


"LP" <lp@a.com> wrote in message
news:uO**************@TK2MSFTNGP10.phx.gbl...
你好,
这是场景:

对象A打开Sql Db连接以执行多个SqlCommands。然后
它需要将此连接传递给对象B的构造函数,而构造函数又在同一连接上执行更多命令。
我理解如果SqlConnection传递为value
(未装箱),对象B将创建自己的SqlConnection副本,因此当对象A关闭其连接时,它对于对象B'的副本保持打开状态。这个
对吗?是不是通过ref传递它的正确方法?什么是最好的
实践?

谢谢。
Hello,

Here''s the scenario:

Object A opens a Sql Db connection to execute number of SqlCommands. Then
it
needs to pass this connection to a constructor of object B which in turn
executes more commands on the same connection.
I have an understanding that if SqlConnection is passed as "value"
(unboxed), object B will create its own copy of SqlConnection, so when
object A closes its connection, it remains open for object B''s copy. Is
this
correct? Is not the right way to pass it by ref? What are "the best
practices"?

Thank you.



我很好奇要知道为什么ref必不可少或需要ref

SqlConnection oConn?由于SqlConnection是一个引用类型,MyMethod将指向从名为Mymethod的函数创建的同一个对象。我错过了一些基本的东西吗?


-

Ajay Kalra [MVP - VC ++]
aj ******* @ yahoo.com

" Robbe Morris" <在** @ turnkeytools.com>在消息中写道

新闻:#X ************** @ TK2MSFTNGP10.phx.gbl ...
I am curious to know why is the ref essential or needed in "ref
SqlConnection oConn"? Since SqlConnection is a reference type, MyMethod will
be referring to the same object that was created from the function that
called Mymethod. Am I missing something basic?

--
Ajay Kalra [MVP - VC++]
aj*******@yahoo.com
"Robbe Morris" <in**@turnkeytools.com> wrote in message
news:#X**************@TK2MSFTNGP10.phx.gbl...
在C#中,当你通过ref传递这个:

public void MyMethod(ref SqlConnection oConn)

它是一个指针of of sort回到原始对象。您将确保使用此引用捕获后续方法调用中的所有错误,因此主方法可以确保处理对象并关闭连接。

-
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.mastervb.net/home/ng/foru...t10017013.aspx


LP < lp@a.com>在消息中写道
新闻:uO ************** @ TK2MSFTNGP10.phx.gbl ...
In C#, when you pass this by ref:

public void MyMethod(ref SqlConnection oConn)

it is a pointer "of sorts" back to the original object. You''ll
to be sure that you trap all errors in subsequent method calls
with this reference so the master method can ensure the
object gets disposed of and the connection closed.

--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.mastervb.net/home/ng/foru...t10017013.aspx


"LP" <lp@a.com> wrote in message
news:uO**************@TK2MSFTNGP10.phx.gbl...
你好,
这是场景:

对象A打开Sql Db连接以执行多个SqlCommands。
然后它需要将此连接传递给对象B的构造函数,而构造函数又在同一连接上执行更多命令。
我理解如果SqlConnection传递为value
(未装箱),对象B将创建自己的SqlConnection副本,因此当对象A关闭其连接时,它对于对象B'的副本保持打开状态。这个
对吗?是不是通过ref传递它的正确方法?什么是最好的
实践?

谢谢。
Hello,

Here''s the scenario:

Object A opens a Sql Db connection to execute number of SqlCommands. Then it
needs to pass this connection to a constructor of object B which in turn
executes more commands on the same connection.
I have an understanding that if SqlConnection is passed as "value"
(unboxed), object B will create its own copy of SqlConnection, so when
object A closes its connection, it remains open for object B''s copy. Is
this
correct? Is not the right way to pass it by ref? What are "the best
practices"?

Thank you.




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

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