仅在页面加载时与数据库建立一次连接

发布于 2024-09-27 07:24:03 字数 1144 浏览 1 评论 0原文

当我加载页面时,我使用以下代码填充中继器。

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独守阴晴ぅ圆缺 2024-10-04 07:24:03

只是不要关闭连接并触发另一个存储过程。之后关闭连接。因此,您可以调暗另一个 SQL 命令并执行它。

就像这样:

Dim Comm2 As Data.SqlClient.SqlCommand
Dim reader2 as Data.SqlClient.SqlDataReader

Comm2.CommandType = CommandType.StoredProcedure
Comm2.Paramaters.AddWithValue("@whateverValue", "Whatever")

然后在你打开连接之后

reader2 = Comm2.ExecuteReader()

然后你会发现 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:

Dim Comm2 As Data.SqlClient.SqlCommand
Dim reader2 as Data.SqlClient.SqlDataReader

Comm2.CommandType = CommandType.StoredProcedure
Comm2.Paramaters.AddWithValue("@whateverValue", "Whatever")

then just after you open the connection

reader2 = Comm2.ExecuteReader()

Then you'll find reader2 has what you want, but you used the same connection for both.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文