Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 SQL Server Inserting Duplicate Recs From VB.Net

Author  Topic 

adjones1980
Starting Member

36 Posts

Posted - 2007-08-01 : 13:41:19
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!

RashidAwan
Starting Member

1 Post

Posted - 2007-08-06 : 08:09:20
Hello,

I am having a similiar kind of problem. Did you manage to fix this in the end,?
Go to Top of Page

adjones1980
Starting Member

36 Posts

Posted - 2007-08-06 : 08:19:32
No I did not. I just had tune the snooze timer so that it was a short as possible without an error occurring.

If you find the answer from anyone then please can you post it on this thread.

Many thanks
Go to Top of Page
   

- Advertisement -