OK I have a piece of code in VB.Net that takes a populated array and inserts each row into a table on my SQL2k DB using a stored procedure... This is the code...Dim _ValidateData(,) As String = arrayObject Dim param As New ParameterProvider Dim procResults As Integer = 0 Dim sqlCmd As SqlCommand Dim sn As New Validation.Snoozer Try 'field headers Dim _arFieldHeads() As String = Split(UCase(param.GetValue("Docs/Parameters.txt", "CSVFields", "List")), ",") 'field sqlDBTypes Dim _arFieldTypes() As String = Split(param.GetValue("Docs/Parameters.txt", "CSVFields", "DataTypes"), ",") 'field data lengths Dim _arFieldLengths() As String = Split(param.GetValue("Docs/Parameters.txt", "CSVFields", "DataLengths"), ",") Dim r As Integer = 0 For r = 1 To UBound(_ValidateData) 'for each line in the memory array except the headers which are first sqlCmd = New SqlCommand("procPopulateUploadBuffer", _SQLCONNECT) sqlCmd.CommandType = Data.CommandType.StoredProcedure Dim c As Integer = 0 For c = 0 To UBound(_arFieldHeads) 'for each column sqlCmd.Parameters.Add("@" & _arFieldHeads(c), _arFieldTypes(c), _arFieldLengths(c)) Next 'final parameter added manually sqlCmd.Parameters.Add("@UploadFileName", SqlDbType.NVarChar, 50) 'this result is always the same sqlCmd.Parameters("@UploadFileName").Value = _NEWFILENAME 'make sure connection is open If _SQLCONNECT.State = ConnectionState.Closed Then _SQLCONNECT.Open() End If 'go through each row of the array and excecute procedure with its values For c = 0 To UBound(_arFieldHeads) 'for each column 'add the value to the parameter in the right data type Select Case _arFieldTypes(c) Case SqlDbType.Int sqlCmd.Parameters("@" & _arFieldHeads(c)).Value = CInt(_ValidateData(r, c)) Case SqlDbType.NVarChar sqlCmd.Parameters("@" & _arFieldHeads(c)).Value = CStr(_ValidateData(r, c)) Case SqlDbType.SmallDateTime sqlCmd.Parameters("@" & _arFieldHeads(c)).Value = CDate(_ValidateData(r, c)) End Select Next sn.Snooze() 'exec proc and cumulate the results for testing the number 'of successful uploads against expected number. procResults = procResults + sqlCmd.ExecuteNonQuery() 'sqlCmd.Dispose() Next
OK, when I run this I randomly get duplicate records inserted into the table. Now I didn't know whether something I had done but when I stepped through it it was fine! Then I added the line sn.Snooze()(highlighted), which is from my own class, and the duplication decreased but was still present.The duplicated records vary according to the number of rows in the array (this is part of an uploading process on my website). It seems that even with the snoozer on (10 miliseconds), the more row in the original array the greater the proportion of duplicate arrays.Does anyone know of a bug in SQL Server that does this? It would seem like I need to give the SQLSvr some time to react before sending another sp , but if I am inserting a couple of 100/1000 rows it would take a long time.If anyone can give some guidance that would be great. Also, I am a bit of a beginner so apologies if this does not look very pretty!