 |
|
|
 |
|
|
 |
|
 |
Hello all,
I have tried the code.Its good and gave me a lot of help. Now i want to pass all the records to the database and populate the database anybody have any idea? Thanks in advance
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
If a field value is repeated you will wind up with the wrong number of fields.
I have a comma-delimited file. Let's say a given line looks like this: foo,bar,quack,foo
Note that 'foo' is repeated. The number of matches that come back from the Regex is 3 instead of 4.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hmm..that's odd, I've never run into that before and the regex is only matching for the delimiter, not the actual content of the field. Are you sure the problem is at that point?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
My apologies. I was adapting your code to my own purposes and forgot the little 'kludge' and the end of the foreach loop to pick up the last field.
Thanks for responding! That saved me some work.
Kevin
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
I kept having a problem using the matches if the first column were left blank - so i switched to an Array, and it seems to be working fine (with a lot less code, as well). Inside the while loop:
//i split on tabs - you can use whatever you want string[] values = val.Split(new Char[] { '\t' }); DataRow drRow; //create a new row drRow = dtTable.NewRow(); for (int i = 0; i < values.Length; i++) //columns and values should match drRow[i] = values[i].Trim(); dtTable.Rows.Add(drRow); //Get the next line from the input file val = NextLine(stream);
Ben
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Sure you could do that, in my case I had to break the lines on whitespace which is the w3c spec and so there are a variety of characters that could be considered whitespace which would not allow working with the method you've outlined.
A regular expression is quite powerful for more complex cases, but if you know you're always going to have tabs then you could go the route you mentioned.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Excellent code that works from the start. Great job and thank-you again.
Here are some other code changes I did to make use of the new DataGridView (vs older DataGrid) and my text file had /r at the end of each row, so I needed to remove the /r ( and for good measure I removed line feeds also)
1) in Text2DataSet.cs
sTemp=sTemp.Trim('"'); // added this line sTemp = sTemp.Replace("\n", "").Replace("\r", "");
2) in the calling code (as in demo the btnDataSet_Click event code) //replaced this line below with the next line //dataGrid1.SetDataBinding(dsTemp,"Data"); dataGridView1.DataSource = dsTemp.Tables["Data"];
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Newbie - any updates regarding use of ADO.NET 2.0? I will try using code now, but I am not too familiar with ADO yet. Would there be any improvements to code with the use of ADO.NET 2.0?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Possibly but it will run fine on 2.0 as is. Remember it's only a sample, something designed for understanding the methods in question. If you just want to import delimited text files there are several excellent libraries on CodeProject that are pre-built and very efficient for doing that such as FileHelpers.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
It has been great for me to use the sample you have provided. I am trying to learn C# and I am trying to think of projects I can do to learn it! So viewing your sample has been a great experience. A good task might be for me to see if there are any changes to upgrade ADO 2.0!
The comments are excellent and really have help also!
|
| Sign In·View Thread·PermaLink | 3.00/5 (1 vote) |
|
|
|
 |
|
 |
This is the first hit I got for parsing delimited files in C#, and will be the last I go...this is exactly what I needed. I've just gotta say nice work with your commenting. As a heavy commenter myself I really appreciate gaining a firm grasp of what is going on through commenting.
Sharpmike
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I am a newbie and was wondering how you would modify this program to parse out certain data in a text file and out putting it into another text file.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hello, the code illustrates two ways of parsing a text file, one is to an xml document the other to a dataset, either one could be saved to file.
If you mean extracting certain fields and then rewriting out another delimited text file with just those fields, I would probably use the dataset example as a starting point to write a program that allowed a user to scan the first row of the input data, use those fields or field names as a basis to build a user interface selection row for the user so they can select the fields they want to import, probably I would just import all the data without skipping any fields into a dataset as in the example, then when writing it back out to a text file would be the point I would skip the unrequired fields. That way you can check that the data source is "clean" i.e. self consistent, no missing fields etc without trying to pick and choose out the fields in the import part of the process. (unless of course we're talking about gigabyte sized files or something)
Although if you are in the habit of working with data in text files then you may want to consider the XML version since that's a natural fit for textual data storage.
If you are looking at writing an application that will repeatedly deal with the same format text file and repeatedly write out another format text file as a result then you could probably use much of the code in the example with very little changes other than redefining the fields as you require them from my old apache web log fields to whatever works for you see comments in source code about doing exactly that.
If you just need to do ad-hoc data transformation of text files I wouldn't bother writing a program for that, there are so many out there, Microsoft Access is excellent for general purpose data conversion.
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
 |
Hi,
Will this tool convert a CSV comma delimited file to a text file? Some of the fields in the cvs file do not contain data. Will the offsets be correct?
On a project attempting same.
Thanks.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi Boomer, well, for starters it's not really a tool, it's a technique you can adapt to your situation, if your expecting a tool that you can run to immediately do what you want it won't do that.
If a CSV file contains no data in some fields that's no problem depending upon how you code it, it's looking for the delimiter so as long as the empty fields are still delimited then you're ok. If they are not delimited then there is nothing in the world that is going to make that ok because there is no way of any software telling what goes in what field unless all the missing delimiters are at the right side of the line of data in which case you could code it so that when it comes to a carriage return or line feed character at the end of the line and hasn't counted enough fields it can substitute empty data for the missing fields.
If, however, there is a situation where a comma was missing early in the line of fields but then there was more data after it, that's a mess that is probably next to impossible to deal with in any automated way.
"A preoccupation with the next world pretty clearly signals an inability to cope credibly with this one."
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
as u said, u hv got good xperience in C/C++....that is reflected in this program even though u just started with C#,,when u wrote this program.
This code will b useful to me for sure!.. Thanks a lot.
Srinivas Varukala virginia, USA. nivas.org
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
 |
Curious as to why you went through the trouble of regular expressions to parse the file when an Ole connection object would have done the samething. For instance:
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\;Extended Properties='text;HDR=No;FMT=Delimited'"); OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM ["+ dataFile + "]", conn);
DataSet ds = new DataSet("File"); da.FillSchema(ds, SchemaType.Source, dataFile); da.Fill(ds, dataFile);
Would have done the samething...
Fear not my insanity, fear the mind it protects.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Probably because I didn't know that was a possibility.
I'll have to experiment with that the next time I need to do something similar, however, I would be concerned with doing it that way in a case of an improperly formatted source file, doing it with regular expressions and looping allows for handling malformed "rows" in the source file, what would happen with the above if a "row" had an extra field or there was an extra cr/lf at the end of a line?
There is much to be said in favor of modern journalism. By giving us the opinions of the uneducated, it keeps us in touch with the ignorance of the community. - Oscar Wilde
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
If the data file was malformed it would throw an error, of which you would need to catch and handle accordingly. I can see though a time when either method might be used. Yours would be perfect for hand written data files or when you need extra control of the data parsing for one reason or another. Using the ADO tends to lock you down a bit too much at times I'll admit.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |