循环SQL如果有行然后继续

时间:2014-12-17 16:26:36

标签: sql vb.net

如果结果继续有行,我正在尝试运行此循环。所以基本上如果我的sql语句继续在ssql = "SELECT TOP 1 * from [OrderHeader] Where ([IsisDownloadDate] is null or [IsisDownloadDate] = '')"

上返回一行

然后运行提交给webrequest,然后返回一个值,然后将该值提交到同一行并更新该列

所以基本上我只是希望它继续更新下一行,只要ssql保持返回行,如果它不返回任何行然后停止。

除了连续循环问题外,我还能完成所有工作

以下是代码:

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click


    Dim objDR As SqlClient.SqlDataReader
    Dim objCommand As SqlClient.SqlCommand
    Dim ConnectionString As String = "Data Source=localhost;Initial Catalog=datarep;user id=sa;password=test123;"
    Dim objConnection As SqlClient.SqlConnection
    Dim ssql As String

    objConnection = New SqlClient.SqlConnection(ConnectionString)
    ssql = "SELECT TOP 1  * from [OrderHeader] Where ([IsisDownloadDate] is null or [IsisDownloadDate] = '')"

    If objConnection.State <> ConnectionState.Open Then
        objConnection.Open()
    End If
    objCommand = New SqlClient.SqlCommand(ssql, objConnection)
    objDR = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
    objCommand = Nothing

    If objDR.HasRows Then
        While objDR.Read()

            Dim objSO As New WebReference.SalesOrder
            Dim objBTAddr As New WebReference.BillToAddress
            Dim objSTaddr As New WebReference.ShipToAddress
            Dim objAddr As New WebReference.Address
            Dim objPart() As WebReference.SalesOrderPart
            Dim objMisc As New WebReference.SalesOrderMiscCharges
            Dim objPayment As New WebReference.Payment
            Dim objCreditCard As New WebReference.SalesOrderCreditCard
            Dim objApproval As New WebReference.SalesOrderCreditCardApproval

            objSO.OrderNumber = "69355522"
            objSO.CompanyId = "301"
            objSO.CustomerNumber = "5838303"
            objSO.Code = "I"

            objSO.PONumber = objDR("OrderNumber").ToString()
            objSO.Source = "TAW.COM"

            objSO.OrderDate = Format(Date.Now, "MM/dd/yy")
            objSO.RequiredDate = Format(Date.Now, "MM/dd/yy")

            objSO.ShipCode = "UPG"
            objSO.EmployeeId = "1"

            objAddr.Name = "José Peña,EPS H-1607"
            objAddr.Address1 = "LÄRKGATAN 9"
            objAddr.City = "Québec"
            objAddr.Country = "US"
            objAddr.State = "CA"
            objAddr.Zip = "90220"
            objSTaddr.Address = objAddr

            'objSTaddr.Phone = "310-900-5509"

            objBTAddr.AccountNumber = "595522"
            objBTAddr.Address = objAddr
            objSO.BillToAddress = objBTAddr

            'turn on for .88
            'objSO.ShipTo = objSTaddr
            'objSO.ShipTo.Phone = objSTaddr.Phone

            ReDim objPart(1)

            objPart(0) = New WebReference.SalesOrderPart
            objPart(0).PartNumber = "EVE510-621"
            objPart(0).PartId = "EVE"
            objPart(0).Quantity = 1
            objPart(0).Price = 39.99
            objPart(0).Description = "PWRAID SPCR"

            objSO.Parts = objPart
            Dim ws As New WebReference.WebServiceTyped
            Dim result As WebReference.SubmitOrder = ws.SubmitSalesOrder(objSO)

            Dim ordernum As String = result.OrderId

            Dim s As String = "Data Source=localhost;Initial Catalog=datarep;user id=sa;password=test123;"
            Dim sql As String
            Dim con As New SqlConnection

            con = New SqlConnection(s)
            con.Open()

            sql = "WITH UpdateList_view AS ( SELECT TOP 1  * from [OrderHeader] Where ([IsisDownloadDate] is null or [IsisDownloadDate] = '') ) update UpdateList_view set [IsisDownloadDate] = '" & result.OrderId & "'"
                Dim cmd As New SqlClient.SqlCommand(sql, con)
                cmd.ExecuteNonQuery()

                con.Close()

        End While
    End If
    objDR.Close()
    objDR = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

除了整体之外,你为什么要这样做呢,问题是你只选择一行。你的while循环通过那个单行然后退出。这段代码存在很多问题,我建议你不要这样做。

让我们仔细阅读一下代码。假设有两行符合此条件,即行J和行6.您选择顶部1,然后返回行J。您的If objDR.HasRows将评估为true,您将进入while状态。阅读和更新后,您将回到while状态。您已经阅读了行J,并且您的vb.net代码不知道数据库中还有其他内容,因此我们退出while循环并退出子。

我建议您立即选择符合您条件的所有行。选择所有数据而不是前1名将比从数据库中一次选择一行中的数据更好,因为外出并连接到数据库是很昂贵的。按照您的方式,您将为符合条件的每一行连接两次数据库。我的方式,你将为每一行连接一次,再开始一次。如果要更新很多行,这将是一个巨大的差异。你的sql看起来应该更像

SELECT UniqueId from [OrderHeader] Where ([IsisDownloadDate] is null or [IsisDownloadDate] = '')

现在循环播放时,您将浏览所有数据。我还建议您在更新数据时确保使用将更新您正在查看的特定行的参数,通常最好使用某种唯一ID。实际上,你也不需要一个cte。类似的东西:

sql = New SqlCommand("UPDATE UpdateList_view SET [IsisDownloadDate] = @OrderId WHERE UniqueId = @ID", dbConn)
sql.Parameters.AddWithValue("@OrderId", result.OrderId)
sql.Parameters.AddWithValue("@Id", objDR.GetInt32(0))

注意,objDR.GetInt32(0)会将@Id参数设置为在第一个sql查询中选择的唯一ID。另外,请看一下我如何在sqlCommand中添加参数。您应该养成以这种方式编码的习惯,因为update UpdateList_view set [IsisDownloadDate] = '" & result.OrderId & "'"会让您打开sql注入。

最后,您可能需要考虑执行sql批量更新,而不是一次更新一行。 This可能是一个很好的起点。