过程仅更新第一个值

时间:2016-10-14 08:16:42

标签: vb.net stored-procedures foreach parameters

我有这个SQL程序,用于在给定 @StepId 值的情况下更新记录。 我从隐藏字段中获取值作为数组,然后循环它们。 问题是该过程仅更新第一个值。 循环发生但不会更新超过1的值。 我的数组包含 {1000,2000,3000,} 之类的内容 我的参数是 @StepId(int) @DateCalculationRule(char) @Result(int)

Private Sub buttonCalculateDatesClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonCalculateDates.Click

        Dim mySteps As String
        Dim myRule As String
        Dim ok As Integer = 1
        Dim connectionString As String = WebConfigurationManager.ConnectionStrings("cnnstring").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(connectionString)
        Dim cmd As New SqlCommand("SaveStepDeadlineRule", conn)
        conn.Open()
        conn.CreateCommand()
        cmd.CommandType = CommandType.StoredProcedure
        myRule = HiddRule.Value
        mySteps = HiddStepIDs.Value

        'Separate string by comas
        Dim parts As String() = mySteps.Split(New Char() {","c})
        Dim part As String

        For Each part In parts

            cmd.Parameters.Add(New SqlParameter("@StepId", part))
            cmd.Parameters.Add(New SqlParameter("@DateCalculationRule", myRule))
            cmd.Parameters.Add(New SqlParameter("@Result", 0))
            cmd.Parameters("@Result").Direction = ParameterDirection.Output
            Try

                cmd.ExecuteNonQuery()
                ok = IIf(IsDBNull(cmd.Parameters("@Result").Value), 1, cmd.Parameters("@Result").Value)
                RadGrid1.Rebind()


            Catch ex As Exception
                ok = 1
            End Try

        Next
        conn.Close()
    End Sub

1 个答案:

答案 0 :(得分:1)

每次循环时都需要清除参数:

cmd.Parameters.Add(New SqlParameter("@StepId", 0))
cmd.Parameters.Add(New SqlParameter("@DateCalculationRule", ""))
cmd.Parameters.Add(New SqlParameter("@Result", 0))
cmd.Parameters("@Result").Direction = ParameterDirection.Output

For Each part In parts
    cmd.Parameters("@StepId").Value = part
    cmd.Parameters("@DateCalculationRule").Value = myRule

    Try

        cmd.ExecuteNonQuery()
        ok = IIf(IsDBNull(cmd.Parameters("@Result").Value), 1, cmd.Parameters("@Result").Value)
        RadGrid1.Rebind()

    Catch ex As Exception
        ok = 1
    End Try

Next

这是如何使用的:

<RelativeLayout
    android:id="@+id/content_map"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:visibility="gone"  >
<fragment 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        tools:context=".MapsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

 </RelativeLayout>

你可以在循环外创建参数,因为它们不会改变,然后每次循环时只分配值:

foreach (WorkItemType workItemType in workItemTypes)
{
    WorkItem workitem = new WorkItem(workItemType);
    workitem.Title = "OTRS-TFS-Connector Test-Workitem";
    ArrayList requiredFields = workitem.Validate();
    if (requiredFields != null) {
        foreach(Field f in requiredFields) {
            if (f.Name.Equals("GemeldetVon")) {
                workitem.Fields["GemeldetVon"].Value = "some Value";
            }
            if...
        }
    }
}
ArrayList invalidFields = workitem.Validate();