Is there a reason you need to use an ODBC connection for this? I would think it'd be easier to just open the text file directly and parse it yourself.
A:
Kevin Tighe
2009-01-22 22:16:32
How would it be easier? What's an easy way to do it?
jumbojs
2009-01-22 22:29:19
A:
I don't know if this matters but...
You might be missing the ending "\" in your dbq attribute...
EDIT: Actually...in the text you posted, you have 3 columns, not 2...(2 pipes instead of 1)
Jason Punyon
2009-01-22 22:16:53
A:
I always write the code myself for this kind of op. Here is an example of an abstract class I wrote for this purpose not so long ago. You could modify it or subclass it if you like
public abstract class ReadTextFile : ILoadable
{
public string Path { get; set; }
public UploadFileType FileType { get; set; }
protected internal List<List<string>> Table { get; set; }
public Guid BatchID { get; set; }
/// <summary>
/// Method that loads the raw text into a collection of strings
/// </summary>
/// <returns></returns>
public bool Load()
{
Table = new List<List<string>>();
var splitter = Convert.ToChar("\t");
try
{
using (TextReader tr = new StreamReader(Path))
{
// Discard the first line
String line = tr.ReadLine();
// Read and display lines from the file until the end of the file is reached.
while ((line = tr.ReadLine()) != null)
{
Table.Add(line.Split(splitter).ToList<string>());
}
tr.Close();
tr.Dispose();
}
return true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
}
public string Left(string param, int length)
{
//we start at 0 since we want to get the characters starting from the
//left and with the specified lenght and assign it to a variable
string result = param.Substring(0, length);
//return the result of the operation
return result;
}
public string Right(string param, int length)
{
//start at the index based on the lenght of the sting minus
//the specified lenght and assign it a variable
string result = param.Substring(param.Length - length, length);
//return the result of the operation
return result;
}
}
David Leon
2009-01-22 22:36:54
A:
Try using this connection string
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Owner\Desktop\IR\IR_Files\Absolute\MyTextFile.CSV;Extended Properties='text'
and:
- Beware of the number of columns
- Place the schema.ini in the same folder of the executable.
Eduardo Molteni
2009-01-22 22:47:24
+2
A:
I use the following connection string
string connectionString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"text;HDR=YES;Format=Delimited(|)\";", Path.GetDirectoryName(path));
and a Schema.ini file that typically begins
[myFile.txt]
Format=Delimited(|)
TextDelimiter="none"
and I'll execute a reader via
command.CommandText = String.Format("SELECT * FROM [{0}]", Path.GetFileName(path));
OleDbDataReader reader = command.ExecuteReader();
Also, the MSDN page on the text file driver was helpful when I first investigated this. Specifically, the page on the Schema.ini
file is quite useful.
Jason
2009-01-22 22:53:06