Dim myCommand As New OleDbCommand(mySelectQuery, myConnection)
myConnection.Open()
Dim myReader As OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
While myReader.Read()
Console.WriteLine(myReader.GetString(0))
End While
myReader.Close()
'Implicitly closes the connection because CommandBehavior.CloseConnection was specified.
myConnection.Open()
Dim myReader As OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
While myReader.Read()
Console.WriteLine(myReader.GetString(0))
End While
myReader.Close()
'Implicitly closes the connection because CommandBehavior.CloseConnection was specified.
////////
另外,我还想问一下,返回reader的函数无法在函数里close(若关了就取不到数据了),那是不是只能在调用函数里close呢?例如直接MyMarket.CommentList.close掉吗?
///////
这个问题的解答:
如果函数返回了DataReader,那么该DataReader就被外部调用所引用,它所使用的连接也会继续存在,绑定操作后,应该关闭DataReader,释放所占空间和连接.
To:良子
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
这个只是说明在关闭Reader时,自动关闭连接,好象跟我的问题不是很相关。
To:oyl007(兰色)
我又试了试,只选择文本型的字段,可那个还是在DataGrid里显不出来。
To:cuilj(往上爬)
谢谢你回答的第二个问题,回头给分时少不了你的一份。
不过对你的建议(我建议你改用DataSet,这样可以不用考虑何时释放数据库 连接.
),表示遗憾,我用它就是想了解它。
麻烦大家再帮帮我,搞定一定给分的,要是不够,还可以加的,我的可用分都快要1万了。
回头也是散。