.Net ADO 连接类
我创建了一个连接类,它应该将数据表/数据读取器等返回到我的网页。我担心使用此类无法正确关闭连接。这是课程:
Imports Microsoft.VisualBasic
Namespace myConnection
Public Class DB
Public Shared Function GetConnStr()
Return "server=foobar"
End Function
Public Shared Function OpenConn()
Return New System.Data.SqlClient.SqlConnection( GetConnStr )
End Function
Public Shared Function OpenReader(SQL As String)
Dim conn
conn = OpenConn
conn.Open
Return New System.Data.SqlClient.SqlCommand(SQL, conn).ExecuteReader(System.Data.CommandBehavior.CloseConnection)
End Function
Public Shared Function OpenTable(SQL As String)
Dim conn
conn = OpenConn
conn.Open
Dim dr As System.Data.SqlClient.SqlDataReader = New System.Data.SqlClient.SqlCommand(SQL, conn).ExecuteReader(System.Data.CommandBehavior.CloseConnection)
Dim dt As System.Data.DataTable = New System.Data.DataTable()
dt.Load(dr)
Return dt
End Function
Public Shared Function ExecuteSQL(SQL As String)
Dim conn
conn = OpenConn
conn.Open
Return New System.Data.SqlClient.SqlCommand(SQL, conn).ExecuteNonQuery()
End Function
End Class
End Namespace
以下是我使用它的方式:
rst = conn.OpenReader(SQL)
While rst.Read
end while
rst.close
我担心一旦投入生产,连接将无法正确关闭,我的网站将会失败。我是.net新手,这个类背后的负责人有什么问题吗?
I created a connection class that should return a datatables/datareaders etc to my webpages. I am worried that the connections won't be closed properly by using this class. Here is the class:
Imports Microsoft.VisualBasic
Namespace myConnection
Public Class DB
Public Shared Function GetConnStr()
Return "server=foobar"
End Function
Public Shared Function OpenConn()
Return New System.Data.SqlClient.SqlConnection( GetConnStr )
End Function
Public Shared Function OpenReader(SQL As String)
Dim conn
conn = OpenConn
conn.Open
Return New System.Data.SqlClient.SqlCommand(SQL, conn).ExecuteReader(System.Data.CommandBehavior.CloseConnection)
End Function
Public Shared Function OpenTable(SQL As String)
Dim conn
conn = OpenConn
conn.Open
Dim dr As System.Data.SqlClient.SqlDataReader = New System.Data.SqlClient.SqlCommand(SQL, conn).ExecuteReader(System.Data.CommandBehavior.CloseConnection)
Dim dt As System.Data.DataTable = New System.Data.DataTable()
dt.Load(dr)
Return dt
End Function
Public Shared Function ExecuteSQL(SQL As String)
Dim conn
conn = OpenConn
conn.Open
Return New System.Data.SqlClient.SqlCommand(SQL, conn).ExecuteNonQuery()
End Function
End Class
End Namespace
And Here is how I am using it:
rst = conn.OpenReader(SQL)
While rst.Read
end while
rst.close
I am worried that once I go into production the connections won't be close properly and my site will fail. I am new to .net, is there anything wrong with the principal behind this class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你是对的:你的连接不会以这种方式关闭。更糟糕的是,通过只接受 sql 命令的字符串,您将面临 sql 注入安全漏洞。作为更好模式的示例,我用来填充数据表的代码看起来更像是这样:
然后我会这样调用代码:
我在 C# 中为 SqlDataReader 有类似的代码,但它需要使用迭代器块,并且功能
不适用于VB仅刚刚使用 Visual Studio 2010 的服务包添加到 VB.Net Async CTP 已于几周前推出。这里要注意的重要一点是,我使用 using 块正确封装了 sql 连接,并且代码鼓励正确使用查询参数。You are right: your connection won't be closed this way. Even worse, by only accepting strings for your sqlcommand you open yourself up to sql injection security vulnerabilities. As an example of a better pattern, the code I use to fill a data table looks more like this:
I would then call the code like this:
I have similar code in C# for an SqlDataReader, but it requires using an iterator block and that feature
is not available for VBwas only just added to VB.Net with the service pack for visual studio 2010 and Async CTP out a few weeks ago. The important thing to take away here is that I have the sql connection correctly encapsulated with a Using block and the code encourages the correct use of query parameters.不幸的是,我同意其他评论之一。你为什么要编写自己的连接类?
使用 ADO.NET EF 或 LINQ To SQL 来管理上下文中的连接。
如果您继续执行正在执行的操作,请将连接包装在“使用”块中。
Unfortunately, I agree with one of the other comments. Why are you writing your own connection classes?
Use ADO.NET EF or LINQ To SQL which will manage the connections in the Context.
If you do continue to do what you are doing, wrap your connection in a Using block.
我使用一个模块来调用数据库,如果您使用多种形式,可以节省很多行...
这是我的模块形式:
然后调用数据库使用这个:
我希望这对您有帮助...
I use a module to call the database, saves alot of lines if your usin multiple forms...
This is my module form:
then for callin the database use this:
I hope this has been helpfull for you...