vs2005中的新语句using怎么用?

蜗牛水里爬 2005-07-15 03:07:56
Declares the beginning of a Using block and optionally acquires the system resources that the block controls.


Using { resourcelist | resourceexpression }
[ statements ]
End Using


Parts
resourcelist


Required if you do not supply resourceexpression. List of one or more system resources this Using block controls. Multiple resources are separated by commas.
resourceexpression


Required if you do not supply resourcelist. Variable or expression referring to a system resource this Using block controls. If you supply resourceexpression, you must acquire the resource before passing control to the Using statement.
statements


Optional. A block of statements the Using block runs.
End Using


Required. Terminates the Using block and disposes of all the resources it controls.
Each resource in the resourcelist part has the following syntax and parts:

哪位老大能翻译过来~
...全文
86 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2005-07-15
  • 打赏
  • 举报
回复
Using conn As New SqlConnection(dsn)
Using cmd As New SqlCommand("SELECT * FROM Employees", conn)
conn.Open()
Using rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
Console.WriteLine(rdr(0))
End While
End Using
End Using
End Using


Using语句

在C#里有一种using语句,在using语句中初始化的变量可以自动调用Dispose方法而被处置。using语句的实质是一个try...finally结构。现在Visual Basic 2005也具有这个语法:

Using r As New StreamReader(”c:\mytext.txt”)
s = r.ReadToEnd()
End Using

当语句跳出Using块时,r.Dispose()将自动被调用,确保非托管资源被顺利释放。使用Using语句要注意,Using中的变量必需实现IDisposable接口,而且要在进入Using前正确初始化。


http://blog.joycode.com/ninputer/archive/2004/05/18/21827.aspx

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧