2016-04-10 190 views
0

背景:查詢每次運行時都會返回相同的數據

通過這些代碼,我爲數據庫添加了不同的呼籲號碼。每個申訴號碼都有一個通過這些代碼分配給其的分配代碼。

問題:

問題是,當我同時增加超過2個上訴,該代碼只檢測添加分配給第一次上訴分配代碼,但沒有檢測到下一個加分配號。

我試着添加Thread.sleep方法,但結果是一樣的。

Try 
     ' Find out last assignment code 
     Dim fillCommand As String 
     Dim FillList As New OleDb.OleDbDataAdapter 
     Dim dt As New DataTable 
     Dim rowdata As Integer 

     Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ComplianceDB.accdb;Jet OLEDB:Database Password=xxxxxxx;" 'D:\Important Programing links\Programming Learning\Compliance Diary\ComlianceDiary\ComlianceDiary\database\ComplianceDB.accdb" 
     Dim Conn As New OleDb.OleDbConnection(ConnString) 
     Conn.Open() 

     If ListView1.Items.Count = 0 Then 
      MessageBox.Show("Please enter valid information to add appeal.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) 
     ElseIf BenchComboBox.Text = "" Or NoticeLocationTextBox.Text = "" Or BenchMembersTextBox.Text = "" Then 
      MessageBox.Show("Please enter all the required information to add appeal.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) 

     Else 

      For i = 0 To ListView1.Items.Count - 1 
       'Add Appeals to ATIR list 
       Me.ATIRTableAdapter.Insert(ClientComboBox.Text, ListView1.Items(i).SubItems.Item(1).Text, ListView1.Items(i).Text, ListView1.Items(i).SubItems.Item(2).Text, BenchComboBox.Text, BenchMembersTextBox.Text, HearingDate.Value.Date, ListView1.Items(i).SubItems.Item(4).Text, mgrComboBox.Text, txtdescription.Text, ListView1.Items(i).SubItems.Item(3).Text, NoticeLocationTextBox.Text, ChangePassword.GUIDTextBox.Text, DateAndTime.Today) 

       Dim AssignmentCommand = New OleDbCommand("select AssignmentCode, AssignmentName from Assignment where AssignmentName = 'ATIR Appeal - " & ListView1.Items(i).Text & "' AND ClientName = '" & ClientComboBox.Text & "'", Conn) ' (select max(AssignmentCode) from Assignment)" 'SELECT * FROM Assignment Where AssignmentName = '" & AssignmentNameTextBox.Text & "' AND ClientName = '" & ClientNameComboBox.Text & "'" ' WHERE DateofHearing BETWEEN " & Today.Date & " AND " & Today.Date.AddDays(15) 
       Dim Reader As OleDbDataReader = AssignmentCommand.ExecuteReader 
       'Check if the assignment name already exists, if yes, fetch old assignment code and add it to new compliance 
       If Reader.Read Then 
        Dim ATIRCodeCmd = New OleDbCommand("select ID from ATIR where ID = (select max(ID) from ATIR)", Conn) 'SELECT * FROM Assignment Where AssignmentName = '" & AssignmentNameTextBox.Text & "' AND ClientName = '" & ClientNameComboBox.Text & "'" ' WHERE DateofHearing BETWEEN " & Today.Date & " AND " & Today.Date.AddDays(15) 
        Dim ReaderATIRCode As OleDbDataReader = ATIRCodeCmd.ExecuteReader 
        While ReaderATIRCode.Read() 
         Me.NoticeComplianceTableAdapter1.Insert(ClientComboBox.Text, Reader.Item(1).ToString, Today.Date, Today.Date, Reader.Item(1).ToString, NoticeLocationTextBox.Text, txtdescription.Text, Reader.Item(0).ToString, "No", "", "", "Pending", ListView1.Items(i).SubItems.Item(1).Text, "", "", "", "Fresh Notice", "", "", ChangePassword.GUIDTextBox.Text, DateAndTime.Today, mgrComboBox.Text, PartnerNameTextBox.Text, SrManagerNameTextBox.Text, "AppellateCompliance", HearingDate.Value.Date, "", "", "", "", "", ReaderATIRCode.Item(0).ToString, "ATIR") 
        End While 
       Else 

        'if assignment does not exists, then create a new one and give new assignment number to new compliance. 
        Me.AssignmentTableAdapter1.Insert(ListView1.Items(i).SubItems.Item(1).Text, ClientComboBox.Text, "ATIR Appeal - " & ListView1.Items(i).Text, "N/A", "Pending", "No", Today.Date, HearingDate.Value.Date, HearingDate.Value.Date, mgrComboBox.Text, SrManagerNameTextBox.Text, PartnerNameTextBox.Text, "Income Tax Ordinance, 2001", "Pending", "", "Pending", "", "Pending", "", Today.Date, "", Today.Date, "Pending", Today.Date, "") 
        Dim AssignmentCodeCmd = New OleDbCommand("select AssignmentCode from Assignment where AssignmentCode = (select max(AssignmentCode) from Assignment)", Conn) 'SELECT * FROM Assignment Where AssignmentName = '" & AssignmentNameTextBox.Text & "' AND ClientName = '" & ClientNameComboBox.Text & "'" ' WHERE DateofHearing BETWEEN " & Today.Date & " AND " & Today.Date.AddDays(15) 
        Dim ReaderCode As OleDbDataReader = AssignmentCodeCmd.ExecuteReader 
        While ReaderCode.Read() 
         rowdata = ReaderCode.Item(0).ToString 
         ReaderCode.NextResult() 
        End While 

        Dim ATIRCodeCmd = New OleDbCommand("select ID from ATIR where ID = (select max(ID) from ATIR)", Conn) 'SELECT * FROM Assignment Where AssignmentName = '" & AssignmentNameTextBox.Text & "' AND ClientName = '" & ClientNameComboBox.Text & "'" ' WHERE DateofHearing BETWEEN " & Today.Date & " AND " & Today.Date.AddDays(15) 
        'Thread.Sleep(1000) 
        Dim ReaderATIRCode As OleDbDataReader = ATIRCodeCmd.ExecuteReader 
        While ReaderATIRCode.Read() 
         Me.NoticeComplianceTableAdapter1.Insert(ClientComboBox.Text, "ATIR Appeal - " & ListView1.Items(i).Text, Today.Date, Today.Date, "ATIR Appeal - " & ListView1.Items(i).Text, NoticeLocationTextBox.Text, txtdescription.Text, rowdata, "No", "", "", "Pending", ListView1.Items(i).SubItems.Item(1).Text, "", "", "", "Fresh Notice", "", "", ChangePassword.GUIDTextBox.Text, DateAndTime.Today, mgrComboBox.Text, PartnerNameTextBox.Text, SrManagerNameTextBox.Text, "AppellateCompliance", HearingDate.Value.Date, "", "", "", "", "", ReaderATIRCode.Item(0).ToString, "ATIR") 
        End While 

       End If 
       Reader.Close() 
      Next 

      MessageBox.Show("ATIR Appeal(s) have been noted.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information) 
      Conn.Close() 

      Me.Close() 
     End If 

    Catch ex As Exception 
     MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information) 
    End Try 
+1

***永遠不要***像'SELECT MAX(...)+ 1'這樣的「下一個最高」身份證號碼 - 這只是***不安全***在系統中多個用戶!讓數據庫處理這個問題 - 只需使用自動編號爲這些類型的計數器 - MS Access中的「Autonumber」或SQL Server中的「INT IDENTITY」列。不要試圖推出自己的 - 只是不要 - 使用數據庫給你的東西! –

+0

謝謝你的回覆。 –

+0

請建議我如何才能達到目的。選擇MAX(..)用於從數據庫中的另一個表中獲取ID的下一個最高值。哪個更適合從另一個表中獲取最高值的命令。 –

回答

0

沒關係。我通過使用@@IDENTITY sql命令解決了我的問題。謝謝大家的貢獻。

相關問題