仅在页面加载时与数据库建立一次连接
当我加载页面时,我使用以下代码填充中继器。
Dim conn As Data.SqlClient.SqlConnection
Dim Comm As Data.SqlClient.SqlCommand
Dim reader As Data.SqlClient.SqlDataReader
'conn = New Data.SqlClient.SqlConnection("Server=localhost\sqlexpress; " & _
'"Database=MyDB; Integrated Security=true")
conn = New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString)
Comm = New Data.SqlClient.SqlCommand( _
("HomePage"), conn)
Comm.CommandType = CommandType.StoredProcedure
Comm.Parameters.AddWithValue("@currentState", "Florida")
' Open the connection
conn.Open()
' Execute the category command
reader = Comm.ExecuteReader()
' Bind the reader to the repeater.......
blogRepeater.DataSource = reader
blogRepeater.DataBind()
' Close the reader
reader.Close()
' Close the connection
conn.Close()
End Try
现在我想(同时)调用另一个存储过程,以便我可以填充一些文本字段(也在页面加载上)。但是我怎样才能做到这一点,以便只调用数据库一次以获得更好的性能?
如果您不了解 VB.NET,C# 示例也可以使用
When I load my page I populate my repeater with the following code.
Dim conn As Data.SqlClient.SqlConnection
Dim Comm As Data.SqlClient.SqlCommand
Dim reader As Data.SqlClient.SqlDataReader
'conn = New Data.SqlClient.SqlConnection("Server=localhost\sqlexpress; " & _
'"Database=MyDB; Integrated Security=true")
conn = New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString)
Comm = New Data.SqlClient.SqlCommand( _
("HomePage"), conn)
Comm.CommandType = CommandType.StoredProcedure
Comm.Parameters.AddWithValue("@currentState", "Florida")
' Open the connection
conn.Open()
' Execute the category command
reader = Comm.ExecuteReader()
' Bind the reader to the repeater.......
blogRepeater.DataSource = reader
blogRepeater.DataBind()
' Close the reader
reader.Close()
' Close the connection
conn.Close()
End Try
Now I want to call another Stored Procedure (at the same time) so that I can populate some text fields (also on Page Load). But how can I do this so that I only make a call to my database once for better performance?
C# examples will also work if you don't know VB.NET
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是不要关闭连接并触发另一个存储过程。之后关闭连接。因此,您可以调暗另一个 SQL 命令并执行它。
就像这样:
然后在你打开连接之后
然后你会发现 reader2 有你想要的东西,但你对两者使用了相同的连接。
Just don't close the connection and fire off another stored procedure. Close the connection afterwards. So you would Dim another SQL command and execute it.
Something like:
then just after you open the connection
Then you'll find reader2 has what you want, but you used the same connection for both.