Loop through file

Elbob

Member
Joined
Apr 28, 2009
Messages
18
Programming Experience
1-3
Hey

How can i create a loop on the following piece of code

VB.NET:
      If GetDevice.ExecuteNonQuery() > 0 Then
                        'Already Exists
                    Else
                        get = New SqlCommand(InsertString, dataCon)
                        get = New SqlCommand("INSERT INTO User (No) VALUES ('" & _
                        No & "')", dataCon)
                        GetDevice.ExecuteNonQuery()
                        MsgBox("New Users Have Been Added")
                    End If

I want it to loop through the phone numbers and add them to a table if they dontalready exist. After this is moves on to something else.

Ive been looking into while loops but it only added the first missing value.

Is it possible to continually go through the .csv finding these missing values until no more are found?

Thanks
 
Last edited:
Please mention what database you're using in future; there are many and the syntax and code for them varies. Based on your thread http://www.vbdotnetforums.com/winforms-data-access/34106-checking-duplicates.html I've adjusted my previous advice for the knowledge that youre using SQLServer

Your solution should look something like:

VB.NET:
Dim scmd as New SqlCommand("INSERT INTO user(no) VALUES (@num)", New SqlConnection("your connection string"))
scmd.Parameters.AddWithValue("@num", "0123456789") 'dummy value

scmd.Connection.Open()

For Each line as String in File.ReadAllLines("path of your file")

  Try
    scmd.Parameters("num").Value = line
    scmd.ExecuteNonQuery()
  Catch ex as Exception
    'value was already present: you did make phone number the PK, or unique indexed right?
  End Try

Next line

If it didn't, this is an example of how it would best be done
 
Last edited:
Back
Top