2015-10-20 49 views
0

我有點糊塗了這個代碼,我不知道我在哪裏做的不對返回詳細,後的IDataReader = DbObject.ExecuteReader(的DbCommand)第二次執行

Public Function CreateAddressDetailDb(ByVal AddressDetailList As IList(Of AddressDetail), ByVal EmployeeID As Integer) As String 

    Dim ReturnCode As Boolean = False 
    Dim Employeegeneratednumber As String = String.Empty 
    Dim ReturnValues As New Dictionary(Of String, Object) 
    Dim DbObject As Database = DatabaseFactory.CreateDatabase() 
    Dim DbCommand As DbCommand = DbObject.GetStoredProcCommand("usp_AddressDetial_Create") 

    Try 
     If Not AddressDetailList Is Nothing And AddressDetailList.Count > 0 Then 
      For Each Addressdetaulobject As AddressDetail In AddressDetailList 
       With Addressdetaulobject 
        Data.AddInParameter(DbCommand, "paEmployeeID", DbType.Int32, EmployeeID, DbObject) 
        Data.AddInParameter(DbCommand, "paUpdatedBy", DbType.Int16, .Updatedby, DbObject) 
       End With 

       Using DataReaderObject As IDataReader = DbObject.ExecuteReader(DbCommand) 

        DataReaderObject.Read() 
        ReturnValues("Result") = DataReaderObject.Item("Result") 
        ReturnValues("Employeenumber") = DataReaderObject.Item("Employeenumber") 
        DataReaderObject.Close() 
        If Employeegeneratednumber = String.Empty Or Employeegeneratednumber = "" Then 
         Employeegeneratednumber = CType(ReturnValues("Employeenumber"), String) 
        End If 
       End Using 
       DbCommand.Parameters.Clear() 
      Next 
     End If 
     If ReturnValues("Result").ToString().Equals("0") Then 
      Return Employeegeneratednumber 
     Else 
      Return String.Empty 
     End If 
    Catch ex As System.Exception 
     Throw 
    End Try 
    Return Employeegeneratednumber 
End Function 

當我插入一個Employee 1個地址我得到正確的Employeegeneratednumber,但如果插入Employee與2個地址,然後我得到舊的Employeegeneratednumber這是與當前員工無關。 此問題只發生在不在開發站點上的生產站點

回答

0

SqlDataReader提供了一種從SQL Server數據庫中讀取只向前行流的方法。

您的通話DataReaderObject.Read()將返回第一行而不是隻你必須遍歷可用行象下面這樣:

while (DataReaderObject.Read()) 
{ 
    ReturnValues("Result") = DataReaderObject.Item("Result") 
    ReturnValues("Employeenumber") = DataReaderObject.Item("Employeenumber") 
} 

您可能需要根據你的要求改變你的邏輯,因爲上面的代碼將始終返回的最後Employeenumber當您訪問ReturnValues("Employeenumber")時可用行的行。

+0

當我試圖插入address1然後獲得employeenumber = 10,當我嘗試插入地址2爲同一名僱員時,我得到錯誤的僱傭號碼。我在這裏通過同一名員工的地址列表 –

相關問題