【问题标题】:Close SQL Connection from VB.net MVC 3.5 - Timeout issue从 VB.net MVC 3.5 关闭 SQL 连接 - 超时问题
【发布时间】:2016-07-21 08:13:20
【问题描述】:

我已经阅读了这里的很多帖子以寻求有关此问题的帮助,但似乎没有任何建议有助于关闭我的 SQL 连接。

这是我在 VB.net 中使用的函数:

Private Function InsertOtherProjectMembers(ByVal inProjectRequestId, ByVal inEmployeeId, ByVal inEmployee) As Boolean
    Dim reader As SqlDataReader

    Using cmd As New SqlClient.SqlCommand
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = objDataClass.Create_Connection()
        cmd.CommandText = "usp_InsertOtherProjectMember"
        cmd.Parameters.AddWithValue("@ProjectRequestId", inProjectRequestId)
        cmd.Parameters.AddWithValue("@EmployeeId", inEmployeeId)
        cmd.Parameters.AddWithValue("@Employee", inEmployee)
        reader = cmd.ExecuteReader()

        reader.Close()
        cmd.Connection.Close()
        cmd.Dispose()
    End Using

End Function

我最近刚刚添加了 Using - End Using,因为我在这里找到了一些想法。我还添加了 reader.Close、cmd.Connection.Close、cmd.Dispose。 在代码处于 Try-Catch-Finally 之前。

每次运行此代码时,它都会在循环中将员工添加到数据库中,因此它会为每个员工执行插入操作,从而为每个员工打开与数据库的连接。添加的员工名单可能很长,有时有 30-40 名员工。

我试图在创建连接后关闭连接,因为一旦用户进行保存并尝试在应用程序中加载任何其他内容,我们就会收到一条 SQL 错误,指出与数据库的连接太多。当我在数据库上运行sp_who 时,它向我显示了与数据库的增量连接,最初它可以是 2 或 3,其中包括登录和加载初始数据,但在我运行此 Insert 后,它会上升到 15 和然后到 20 岁,然后进入 30 年代。它总是在创建新的连接,从不关闭旧的连接。

这是为 usp_InsertOtherProjectMember 调用的 SQL 代码

    ALTER PROCEDURE [dbo].[usp_InsertOtherProjectMember]
@ProjectRequestId Varchar(50),
@EmployeeId varchar(5),
@Employee varchar(50)
AS

-- Get the integer part of the transmitted request number
Declare @inReqId AS Int
SET @inReqId = Substring(@ProjectRequestId, 3, Len(@ProjectRequestId)-2) 


Insert Into tblProjectRequestOtherMembers(ProjectRequestId, EmployeeId, Employee) 
Values(@inReqId, @EmployeeId, @Employee)

我还应该在代码中添加/更改什么以关闭连接吗?目前,连接仅由垃圾收集器关闭,最终......

感谢您的帮助!

编辑: 为 Create_Connection() 添加代码

Public Function Create_Connection() As Data.SqlClient.SqlConnection
    Dim strConn As String
    strConn = ConnectionString
    Dim Conn As New Data.SqlClient.SqlConnection(strConn)
    Conn.Open()
    Create_Connection = Conn
End Function

ConnectionString 取自 Webconfig 并被定义为公共变量:

ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

在网络配置中:

<add name="ConnectionString" connectionString="Server=*SERVERNAME*;Database=*DATABASENAME*;Trusted_Connection=True"/>

我还想补充一点,这是我继承的代码,我正在尝试对其进行修补......

【问题讨论】:

  • SET @inReqId = Substring(@ProjectRequestId, 3, Len(@ProjectRequestId)-2) 这很糟糕。将@ProjectRequestId 设为Int 类型,那么您就不必做所有这些无意义的事情了。也请张贴objDataClass.Create_Connection() 的代码...objDataClass.Create_Connection() 那个对象实例是你的问题,它有一个指向那个连接(对象)的指针...
  • 我已经用缺少的功能对我的代码进行了编辑...感谢您的回复!
  • 我知道这不是答案,但您能不能不执行以下操作...创建连接....循环添加,然后在循环中的所有添加完成后关闭/处理连接?比每次添加后打开和关闭更有效的方法。

标签: sql-server vb.net timeout connection


【解决方案1】:

请试一试...

Public Shared Function Create_Connection() As String
    Return ConnectionString 'Just return the connection string...
End Function

Using cmd As New SqlClient.SqlCommand
  Using con As New SqlClient.SqlConnection(Create_Connection())
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = con 'use the con which is your connection now...
            cmd.CommandText = "usp_InsertOtherProjectMember"
            cmd.Parameters.AddWithValue("@ProjectRequestId", inProjectRequestId)
            cmd.Parameters.AddWithValue("@EmployeeId", inEmployeeId)
            cmd.Parameters.AddWithValue("@Employee", inEmployee)

            con.Open()
            reader = cmd.ExecuteReader()
            reader.Close()
            con.Close()
  End Using
End Using

【讨论】:

    猜你喜欢
    • 2018-10-12
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 2012-01-29
    • 2017-05-24
    • 2015-11-19
    相关资源
    最近更新 更多