I am working on a small third-party client application in which they provide me with a list of cities, and I need to insert them into the database and link them to the parent records.
Example:
ID | PID | Region
1 0 California
2 1 Los Angeles
3 1 San Fransisco
Now my code is as follows:
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
''
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
''
''
''
''
''
''
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
''
input.Close()
input.Dispose()
Next
So if Los Angeles is in a TXT file, I do not want it to be re-entered into the database, as it already exists.
Can someone help me figure out how to check if a record exists before insertion?
source
share