今天偶然发现的一个问题,200分,大家一起看一下

smoothwood smoothwood 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
大家请看下面这段程序:
Dim conn As New SqlClient.SqlConnection("server=localhost;user id=sa;password=ycm119;database=northwind;")
Dim cmd As New SqlClient.SqlCommand
Dim dtr As SqlClient.SqlDataReader
cmd.Connection = conn
cmd.CommandText = "Select * from Products"
conn.Open()
dtr = cmd.ExecuteReader
DataGrid1.DataSource = dtr
DataGrid1.DataBind()
Response.Write("isclosed=" & dtr.IsClosed)
----------------------------------
最后一句Response.Write("isclosed=" & dtr.IsClosed)输出的结果是isclosed=False ,即DataReader现在是处于打开状态。现在我把
dtr = cmd.ExecuteReader改成
dtr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
这时候Response.Write("isclosed=" & dtr.IsClosed)的输出变成isclosed=True 了,但是按照微软在MSDN上的描述:CommandBehavior.CloseConnection参数的作用是:
When the command is executed, the associated Connection object is closed when the associated DataReader object is closed.
也就是说CommandBehavior.CloseConnection参数的作用是在关闭DataReader之后,自动关闭打开的数据库连接,但是为什么在这里加上这个参数之后DataReader是否关闭的状态也受到了影响呢?
...全文

相关推荐

评论 66
收藏 已收藏
分享
返回顶部
smoothwood
.NET社区 62,275 成员 668,968 内容