(c) 2003 Visual Studio Magazine 
Fawcette Technical Publications

VB.NET	Create an Import Class
Listing 1	This code creates a FileSystemWatcher object to monitor the directory you specify in its constructor. The object's properties let you specify which kind of file to watch for and what action to perform against it. This code checks for an XML file's creation and raises the OnCreated event, which starts data import.

Imports System.IO

Module TestApp

	Sub Main()
	Dim fsw As New _
				FileSystemWatcher("G:\")
		AddHandler fsw.Created, _
							AddressOf OnCreated

		fsw.EnableRaisingEvents = True
		fsw.Filter = "*.xml"
		fsw.IncludeSubdirectories = True
		fsw.WaitForChanged(_
						WatcherChangeTypes.Created)

	End Sub

	Sub OnCreated(ByVal o As Object, _
					ByVal e As FileSystemEventArgs)
		Dim db As New DBCore.Import
		db.Start(e.FullPath)
	End Sub
End Module

VB.NET	Add Tracing Code
Listing 2	The Import class's Start method imports data into a database. This method creates a new thread that opens the specified file and starts to import the data by using ADO.NET classes' ReadXml and Update methods. The tracing code in the Start method logs the thread sleeping time and other messages.

Imports System.Diagnostics
Imports System.Data.SqlClient
Imports System.Threading

Public Class Import
	Private m_dbConn As SqlConnection
	Private m_ds As DataSet
	Private m_da As SqlDataAdapter
	Private m_ts As TraceSwitch

	Sub New()
		m_ts = New TraceSwitch("ImportTS", _
			"DBCore.Import class switch")

		Try
			Dim tw As New _
				TextWriterTraceListener( _
				"C:\Import.log")
			Trace.Listeners.RemoveAt(0)
			Trace.Listeners.Add(tw)

			m_dbConn = New _
				SqlConnection("server=.;" & _
				"database=Northwind;user=sa;pwd=")
			m_da = New SqlDataAdapter(_
				"SELECT LastName, FirstName " & _ 
				"FROM Employees", m_dbConn)
			Dim sb As New SqlCommandBuilder(m_da)
			m_ds = New DataSet

			Trace.WriteLineIf(m_ts.TraceInfo, _
				DateTime.Now & _
				" - Import: Import object " & _ 
				"created successfully.")
		Catch ex As Exception
			Trace.WriteLineIf(m_ts.TraceError, _
				DateTime.Now & " - Import: " & _
				ex.Message)
		Finally
				Trace.Flush()
		End Try
	End Sub

	Sub Start(ByVal strFullPath As String)
		Dim t As New Thread(AddressOf T_Import)
		t.Name = strFullPath
		t.Start()

		Trace.WriteLineIf(m_ts.TraceInfo, _
			DateTime.Now & _
			" - Start: Thread Import is started.")

		While t.IsAlive
			t.Sleep(1000)
			Trace.WriteLineIf(m_ts.TraceInfo, _
				DateTime.Now & _
				" - Start: Thread Import has" & _
				" slept 1 second.")
		End While

		Trace.WriteLineIf(m_ts.TraceInfo, _
			DateTime.Now & _
			" - Start: Thread Import has " & _
			"finished its job.")

		Trace.Close()
	End Sub

	Private Sub T_Import()
		Try
			Trace.WriteLineIf(m_ts.TraceInfo, _
				DateTime.Now & _
				" - T_Import: Begin to manage " & _
				Thread.CurrentThread.Name & _
				" file.")
			m_ds.ReadXml(Thread.CurrentThread.Name)
			m_da.Update(m_ds)
			Trace.WriteLineIf(m_ts.TraceInfo, _
					DateTime.Now & _
					" - T_Import: Import thread " & _
					"has added records " & _
					"successfully.")

		Catch ex As Exception
			Trace.WriteLineIf(m_ts.TraceError, _
				DateTime.Now & " - T_Import: " & _
				ex.Message)

		Finally
			Trace.Flush()

		End Try
	End Sub

End Class

