Represents an open connection to the folder that contains text files. This class cannot be inherited.
For a list of all members of this type, see TxtConnection Members.
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
xPortTools.TxtClient.TxtConnection
[Visual Basic] NotInheritable Public Class TxtConnection Implements IDbConnection, IDisposable [C#] public sealed class TxtConnection : IDbConnection, IDisposable
An TxtConnection object represents a unique session to the folder that contains text files.
When you create an instance of TxtConnection, all properties are set to their initial values. For a list of these values, see the TxtConnection constructor.
If the TxtConnection goes out of scope, it is not closed. Therefore, you must explicitly close the connection by calling Close or Dispose.
[C#, Visual Basic] The following example creates an TxtCommand and an TxtConnection. The TxtConnection is opened and set as the Connection for the TxtCommand. The example then calls ExecuteReader, and closes the connection.
[C#]
using xPortTools.TxtClient;
private void ReadFiles() {
TxtConnection connection = new TxtConnection(@"Data Source=C:\SourceFiles");
connection.Open();
//Create TxtCommand to specify from which text files to query data
TxtCommand command = new TxtCommand("Authors.csv;Titles.csv");
command.Connection = connection;
//Create and open TxtDataReader
TxtDataReader textReader = (TxtDataReader)command.ExecuteReader();
//Read data from the file
OutputReaderContent(textReader);
System.Diagnostics.Debug.WriteLine("------- End of first result --------");
//Advance to the next text file
if (textReader.NextResult()) {
OutputReaderContent(textReader);
}
//Release all opened resources
textReader.Close();
textReader.Dispose();
textReader = null;
command.Dispose();
command = null;
connection.Close()
connection.Dispose();
connection = null;
}
private void OutputReaderContent(TxtDataReader textReader) {
//Read through until there are any rows in a source file
while (textReader.Read()) {
//Iterate through all the fileds for the row
for (int i = 0; i < textReader.FieldCount; i++) {
if (!textReader.IsDBNull(i)) {
System.Diagnostics.Debug.WriteLine(textReader.GetValue(i).ToString());
} else {
System.Diagnostics.Debug.WriteLine("NULL");
}
}
}
}
[Visual Basic]
Imports xPortTools.TxtClient
Private Sub ReadFiles()
Dim connection As TxtConnection = New TxtConnection("Data Source=C:\SourceFiles")
connection.Open()
'Create TxtCommand to specify from which text files to query data
Dim command As TxtCommand = New TxtCommand("Authors.csvTitles.csv")
command.Connection = connection
Dim textReader As TxtDataReader
'Create and open TxtDataReader
textReader = command.ExecuteReader()
'Read data from the file
OutputReaderContent(textReader)
System.Diagnostics.Debug.WriteLine("------- End of first result --------")
'Advance to the next text file
If textReader.NextResult() Then
OutputReaderContent(textReader)
End If
'Release all opened resources
textReader.Close()
textReader.Dispose()
textReader = Nothing
command.Dispose()
command = Nothing
connection.Close()
connection.Dispose()
connection = Nothing
End Sub
Private Sub OutputReaderContent(ByVal textReader As TxtDataReader)
'Read through until there are any rows in a source file
Do While (textReader.Read())
'Iterate through all the fileds for the row
For i As Integer = 0 To textReader.FieldCount - 1
If Not textReader.IsDBNull(i) Then
System.Diagnostics.Debug.WriteLine(textReader.GetValue(i).ToString())
Else
System.Diagnostics.Debug.WriteLine("NULL")
End If
Next
Loop
End Sub
Platforms: Windows Server 2008 family, Windows Server 2003 family, Windows 2000 family, Windows 7 family, Windows Vista family, Windows XP family
TxtConnection Members | xPortTools.TxtClient Namespace | TxtDataAdapter | TxtCommand