I'm working on a little side app for a client whereby they provide me with a list of cities and I have to insert them into the database and associate them with their parent records.
Example:
ID | PID | Region
1 0 California
2 1 Los Angeles
3 1 San Fransisco
Now my code looks like this
Dim input As StreamReader
Dim index As Integer
Dim filename As String
Dim RegionDC As New DAL.RegionsDataContext
For Each TextFile As String In Directory.GetFiles(Server.MapPath("~/app_data/business-trader cities/"))
input = File.OpenText(TextFile)
filename = New FileInfo(TextFile).Name
index = 0
'this is where we want to select the ID for the filename'
Dim _ID = (From R In RegionDC.bt_Regions _
Where R.Region = filename.Replace(".txt", "") _
Select R.ID).FirstOrDefault
While Not input.EndOfStream
Dim q = (From r In RegionDC.bt_Regions _
Where r.Region = input.ReadLine() _
Select r.ID).FirstOrDefault
'***********************************'
' HERE IS WHERE IM TRYING TO VERIFY'
' IF THE RECORD EXISTS OR NOT'
'***********************************'
'now we loop through the txt file'
'and insert the data into the database'
Dim oRegion As New DAL.bt_Region
oRegion.Region = input.ReadLine()
oRegion.FSSearchCount = 0
oRegion.WSearchCount = 0
oRegion.PID = _ID
RegionDC.bt_Regions.InsertOnSubmit(oRegion)
RegionDC.SubmitChanges()
End While
'clean up the locked files'
input.Close()
input.Dispose()
Next
So basically if Los Angeles is in the TXT file, I don't want it re-entered into the database since it already exists.
Can anyone help me figure out how to verify if a record already exists prior to inserting?