2014-01-28 1 views
0

나는 액세스 2010 db에 데이터를 삽입하는 간단한 쿼리를 사용합니다. 그러나 쿼리를 실행하면 한 번이 아닌 두 번 데이터를 삽입합니다. 버튼 클릭에 대한 쿼리를 실행하고 있으며 한 번만 호출됩니다. 내가 중단 점을 설정하고 코드가 실행되는 것을 지켜 보았고 내가 볼 수있는 것에서 단 하나의 항목 만 있었지만 액세스는 2였습니다. 누군가가 왜 이런 일이 일어나고 있는지 이해할 수있게 도와 주었고 더 디버깅하는 데 도움이 될 수 있습니다. 당신이 당신의 코드에서 두 번 oledbCmd.ExecuteNonQuery()를 작성했기 때문에 많은 감사Sql이 액세스에 데이터를 두 번 삽입하는 경우

If CDbl(msg) > 0 And rdbBoxReturn.Checked = True Then 

      Dim custref As String 
      Dim box As String 
      Dim itmAs String 
      Dim itm2 As String 
      Dim Quantity As Integer = Convert.ToInt32(txtBoxQuantity.Text) 


      sql = "SELECT Max(Requests.[Request no]) AS [MaxOfRequest no] FROM Requests" 

      oledbCmd.CommandText = sql 
      oledbCmd.Connection = oledbCnn 

      dr = oledbCmd.ExecuteReader() 

      If dr.HasRows Then 

       While dr.Read 

        itm = CStr(dr.Item("MaxOfRequest no")) 
        itm = String.Format("{0:D6}", (Convert.ToInt32(itm) + 1)) 

       End While 

      End If 

      dr.Close() 

      Dim tran As OleDbTransaction = oledbCnn.BeginTransaction() 

      Try 
       ' Here the connection should be already open 

       oledbCmd.Transaction = tran 

       For i = 0 To lvSelectedItems.Items.Count - 1 
        box = lvSelectedItems.Items.Item(i).Text 
        custref = lvSelectedItems.Items.Item(i).SubItems.Item(1).Text 

        sql = "Insert into Requests ([Request no], Customer, Dept, [Type], [Service level], [Date-time received], [Received by], [Date-time due], Quantity, [Cust requestor], [Status]) Values (?, ?, ?, 'B', ?, ?, ?, ?, ?, ?, 'O')" 

        Debug.Print(sql) 

        oledbCmd.CommandText = sql 
        oledbCmd.Parameters.Clear() 
        oledbCmd.Connection = oledbCnn 
        oledbCmd.Parameters.AddWithValue("@p1", itm) 
        oledbCmd.Parameters.AddWithValue("@p2", cmbCustomer.Text) 
        oledbCmd.Parameters.AddWithValue("@p3", cmbDept.Text) 
        oledbCmd.Parameters.AddWithValue("@p4", rbServiceLevel.ToString) 
        oledbCmd.Parameters.AddWithValue("@p5", dtpDateReceived.Value.ToString) 
        oledbCmd.Parameters.AddWithValue("@p6", txtBy.Text) 
        oledbCmd.Parameters.AddWithValue("@p7", dtpDateDue.Value.ToString) 
        oledbCmd.Parameters.AddWithValue("@p8", Quantity) 
        oledbCmd.Parameters.AddWithValue("@p9", cmbRequestBy.Text) 
        oledbCmd.ExecuteNonQuery() 
        Dim rowsAffected = oledbCmd.ExecuteNonQuery() 
        If rowsAffected = 0 Then 
         ' Fail to insert. Display a message and rollback everything 
         tran.Rollback() 
         Return 
        End If 


        'End If 

       Next 
       ' if we reach this point, then all the commands have been 
       ' completed correctly we could commit everything 
       tran.Commit() 

      Catch ex As Exception 
       MessageBox.Show(ex.Message) 
       tran.Rollback() 

      End Try 

      MessageBox.Show("You have successfully completed the box return", "Box return successfull") 
      Close() 

     Else 

      MessageBox.Show("You must select a box") 

      'CType(sender, RadioButton).Checked = False 
      'Return 

     End If 

      dr.Close() 
      oledbCnn.Close() 
+0

다음 요청 번호를 얻기 위해 사용하는 코드는 다중 사용자 환경에서는 안전하지 않습니다. – Fionnuala

답변

1

이유입니다. 코드에서 다음 줄을보십시오.

oledbCmd.ExecuteNonQuery()//Remove this line from your code and try 
Dim rowsAffected = oledbCmd.ExecuteNonQuery() 
+0

그래서 'oledbCmd.ExecuteNonQuery()'줄을 삭제하고 dim 문을 그대로 둡니다. 감사합니다 – user1532468

+0

@ user1532468 그것이 작동하는지 여부를 알려주십시오. – Bhushan

+0

우수. 내 눈은 오래되었습니다 :-) 많은 감사 – user1532468

관련 문제