English 中文(简体)
SqlDataReader - 多个链接
原标题:SqlDataReader - Multiple connections

I ve written a console app in VB. 该网络从事一些数据库工作,并出现奇怪的运行时间错误。

The main Code:

Sub Main(ByVal args() As String)
        Try
            user = args(0)
            batchID = args(1)

            GetBatchRevision()
             batchRev = 1

            Dim getTestScripts As SqlCommand = New SqlCommand("GetTestScriptsInTestBatch", cs)
            getTestScripts.CommandType = CommandType.StoredProcedure

            Dim batchIDParam As SqlParameter = getTestScripts.Parameters.Add("@batchID", SqlDbType.Int, 4)
            Dim batchRevParam As SqlParameter = getTestScripts.Parameters.Add("@batchRev", SqlDbType.Int, 4)

             batchIDParam.Value = 1
             batchRevParam.Value = 1
            batchIDParam.Value = batchID
            batchRevParam.Value = batchRev

            Console.WriteLine(batchID & " " & batchRev)
            Console.WriteLine(cs.State)
            Console.ReadLine()

            Using cs
                cs.Open()
                Dim reader As SqlDataReader = getTestScripts.ExecuteReader(CommandBehavior.CloseConnection)

                While reader.Read()
                    Console.WriteLine("Executing Test Script " & reader("ScriptID").ToString() & " Revision " & reader("ScriptRev").ToString)
                End While

                Console.ReadLine()
            End Using

        Catch ex As Exception
        End Try
    End Sub

GetBatchRevision:

Private Sub GetBatchRevision()
    Using cs
        Dim GetNewestRev As New SqlCommand("SELECT Max(BatchRev) FROM TestBatch WHERE BatchID=" & batchID, cs)
        cs.Open()
        Dim reader As SqlDataReader = GetNewestRev.ExecuteReader(CommandBehavior.CloseConnection)
        reader.Read()

        If Not IsDBNull(reader(0)) Then
            batchRev = reader(0).ToString()
        End If
    End Using

End Sub

<代码>batchRev和batchID均为该模块内的全球变量。

突然:

  • The app prints out "1" (user input), "1" (database result), "0" (enum of Closed connection)
  • When I press Enter to get past the first Console.ReadLine(), the app simply closes out.

如果我发表以下意见:GetBatchRevision,并直接设定batchRev = 1,则我得出上述结果,以及“Executing Test 1 Revision 52”,“Executing Test cast 2 Revision 66”,这些结果是所储存程序的结果。 《在试验中的试验》()。

全球变化宣言如下:

<条码> ID As String

<条码> Rev As String

任何想法,为什么)导致该机坠毁? 它本身(删除该法典中储存的部分)只执行罚款。 我最初的猜测是,存在着一种hang然的联系,但结束一个“使用”的区块应该是关闭King的连接以及与这种连接相关的任何公开读者(如前文所述,国家回归0)。

最佳回答

参看<代码>cs也是全球变量。 这是一个坏的想法。 当你每次重新使用新链接时,网络数据存取会更好。 也许,你会这样做,但你会重建一些坏习惯。 相反,把你的联系作为全球变量加以载荷,并在建立你的联系时加以利用。

其次,没有理由讨论全球变量的<代码>GetBatchRevision(。 它是否接受论点,而是结果。 当然,我可以忽略 s子注射问题,因为你把bat子混为一谈,直到你扼杀为止。 在确定这些错误后,将职能的新版本改为:

Private Function GetBatchRevision(ByVal BatchID As String) As String
    Using cn As New SqlConnection(cs), _
          GetNewestRev As New SqlCommand("SELECT Max(BatchRev) FROM TestBatch WHERE BatchID= @BatchID", cn)

        GetNewestRev.Parameters.Add("@Batch", SqlDbType.Int).Value = Convert.ToInt32(BatchId)

        cn.Open()
        Return GetNewestRev.ExecuteScalar().ToString() 
    End Using

End Function

如果你把BatchRev和BatchID当作 in,而不是在内部加以扼杀,这甚至会更好。

问题回答

你的问题是:

 reader.Read()

 If Not IsDBNull(reader(0)) Then

<代码>reader.Read(>> 或许会退回不实;然而,你试图查阅reader(0)。 !!

您应将其改为:

IF reader.Read() AndAlso Not IsDBNull(reader(0)) Then
       etc
End If




相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签