|
We try to read data from the file and insert into database and generate reports the data.Which number is dialled from which extension.
But I am failing to read itself.
BR,
Arjun
|
|
|
|
|
So start by looking at the data - how is it organised?
Hopefully (since it has a .TXT extension) it is line based - if so, then it should be pretty easy to handle.
Have you tried
string[] lines = File.ReadAllLines(datafile);
If that works, (and it should, even on a 64 bit system!) it gives you a chance to process each line and transfer that to a separate row in SQL - which would be a lot easier to work with!
|
|
|
|
|
I have a similar problem to be done.
My requirement is to read one large file and split it into 2 files depending on the content.
File format is flat file containing records in each line.
Depending on the record, it will either go into 1st or second file.
What I am currently doing is to read the file one line at a time, check it and write it to either of the two new files created.
With this approach, it is taking around 3 hrs for a file size of 900 MB.
I would like to improve the logic for faster processing.
Can anyone suggest on a better approach ?
|
|
|
|
|
Well, this did it in 59.413 seconds with a 900MB text file, but it also removed all the duplicate lines (of which there were a lot, I don't keep huge text files lying around!)
string origPath = @"D:\Temp\MyHugeText.txt";
string inPath = @"D:\Temp\MyHugeTextIn.txt";
string notInPath = @"D:\Temp\MyHugeTextOut.txt";
var lines = File.ReadLines(origPath);
var isIn = lines.Where(l => l.Contains("raise"));
var notIn = lines.Except(isIn);
File.WriteAllLines(inPath, isIn);
File.WriteAllLines(notInPath, notIn);
Even with the select test reversed so the the duplicates are still written to disk, we are talking about 96.693 seconds showing it's a bit disk limited!
Never underestimate the power of stupid things in large numbers
--- Serious Sam
|
|
|
|
|
Hi this is executing fast.But I will check how long it will take if i try processing each and every line.
BR,
Arjun
|
|
|
|
|
Thanks OriginalGriff. I will try this out.
BTW, is'nt this method using LINQ?
Because, in my deployment scenario there is no 4.0 framework installed. So I may have to target for 3.0 or earlier frameworks.
I can modify this logic to not use LINQ right?
|
|
|
|
|
It uses a Linq method yes - but it was introduced at version 3.5 over 6 years ago!
Yes, you can do it yourself, but it probably won't be as quick (or easy)
Never underestimate the power of stupid things in large numbers
--- Serious Sam
|
|
|
|
|
Yes. But still the client is using VB6 apps and trying to interface them with new .Net services
I'll try both ways and see how the performance is.
|
|
|
|
|
Hi,
Thanks for the post. Its reading real fast but I am getting an error "Cannot read from a closed TextReader".
string origPath = @"D:\Temp\MyHugeText.txt";
string inPath = @"D:\Temp\MyHugeTextIn.txt";
string notInPath = @"D:\Temp\MyHugeTextOut.txt";
var lines = File.ReadLines(origPath);
var isIn = lines.Where(l => l.Contains("raise"));
var notIn = lines.Except(isIn);
File.WriteAllLines(inPath, isIn);
File.WriteAllLines(notInPath, notIn);
BR,
Arjun
|
|
|
|
|
Strange - I just ran it again, and I don't. Have you got another code in there?
Never underestimate the power of stupid things in large numbers
--- Serious Sam
|
|
|
|
|
I have changed only the "Contains" part and the Origpath,InPath and Outpath values.Please see below
string origPath = @"F:\Bharath CS\tickets.txt";
string inPath = @"F:\Bharath CS\ticketsIn.txt";
string notInPath = @"F:\Bharath CS\ticketsOut.txt";
var lines = File.ReadLines(origPath);
var isIn = lines.Where(l => l.Contains("ED5"));
var notIn = lines.Except(isIn);
File.WriteAllLines(inPath, isIn);
File.WriteAllLines(notInPath, notIn);
if I try the above code it did not work.So tried something else and it started to work.This I found on google.
public static IEnumerable<string> MyReadLines(string path)
{
using (var stream = new StreamReader(path))
{
string line;
while ((line = stream.ReadLine()) != null)
{
yield return line;
}
}
}
I used the above method instead of File.ReadLines and it worked.
|
|
|
|
|
 I tried with File.ReadLines(path) and I was able to read it and insert into database.
In order to show progress of reading and inserting into database, I used backgroundworker.Below is my code:
const string dataFile = @"F:\Bharath CS\Document1.txt";
public Form1()
{
InitializeComponent();
InitializeBackgroundWorker();
}
private void InitializeBackgroundWorker()
{
backgroundWorker1.DoWork +=
new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(
backgroundWorker1_RunWorkerCompleted);
backgroundWorker1.ProgressChanged +=
new ProgressChangedEventHandler(
backgroundWorker1_ProgressChanged_1);
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int count = 0;
string prev = "";
foreach (string line in File.ReadLines(dataFile))
{
if (backgroundWorker1.CancellationPending)
{
break;
}
backgroundWorker1.ReportProgress(count);
try
{
MySqlConnection conn1 = new MySqlConnection("server=demo;port=3306;database=demodb;userid=xyz;pwd=xyz");
conn1.Open();
MySqlCommand cmd1 = new MySqlCommand();
cmd1.Connection = conn1;
string s = line.Replace("\"", "");
if (s.Length > 0 && !(s.Contains("-")))
{
if (s.Contains("ED5."))
{
cmd1.CommandText = "insert into yashomati_demo values('" + s + "')";
cmd1.ExecuteNonQuery();
s = "";
count++;
}
cmd1.Dispose();
conn1.Close();
conn1.Dispose();
}
}
catch (Exception ex) { throw ex; }
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
MessageBox.Show("You've cancelled the backgroundworker!");
}
else
{
progressBar1.Value = 100;
MessageBox.Show("Done");
}
}
private void backgroundWorker1_ProgressChanged_1(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void button2_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
}
But the lines are getting inserted twice.For example if there are 3 lines,all three lines get inserted and again the same three lines get inserted.I mean to say after file is completely read and inserted, again the process of reading and inserting is done once more.
I am not able to find where exactly I am going wrong.
BR,
Arjun
|
|
|
|
|
Two things spring to mind:
Either
1) Your text file contains repeated data
Or
2) You are running the background worker twice.
The second is easy to check, just add a couple of lines to your button1 click event:
if (backgroundWorker1.IsBusy)
{
MessageBox.Show("Already running");
return;
}
I'd start with the first one: Create a dummy file that contains just a dozen lines, and run it into an empty DB. Check the lines it should have against the actual DB table content: if it doesn't duplicate, then you need to look at your actual data and check it for duplicates. (Or modify your code to check for existing values before you insert a new row)
Never underestimate the power of stupid things in large numbers
--- Serious Sam
|
|
|
|
|
OriginalGriff wrote: You are running the background worker twice Isn't that impossible?
From the documentation[^]:
If the background operation is already running, calling RunWorkerAsync again will raise an InvalidOperationException.
|
|
|
|
|
It should be impossible, yes. But you clearly trust the documents more than I do!
(I haven't tried it, it's just the only other way of getting into the code I can think of)
Never underestimate the power of stupid things in large numbers
--- Serious Sam
|
|
|
|
|
Hi,
I wanna ask about your experience please..
if ou had an option to choose between XtraReport and RDLC for our business application? which one you'll decide to use?
Thanks,
Jassim
Technology News @ www.JassimRahma.com
|
|
|
|
|
That is easy. I would choose the one that satisfies the needs of the application.
|
|
|
|
|
HTML.
Then again, would also depend on the business, the expectations of the customers, technical limitations, budget.. If you had to create a report by tomorrow-morning, which of the two would you choose?
Start with that one. You can always "add in" a second choice later
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
I'm planning to implement a gridview using c# that is filtered using faced filters(jscript). Can you show me some tutorials or some advises? I google it but nothing helpful.
How to include an option in the gridview to change the layout from grid view to list view? I saw this feature used in many websites.
Thank you
Danny
webdesigner at gotomyprice.com
|
|
|
|
|
I Have Created a Simple form in which i have added Adobe Reader from toolbox using steps
1. right click in toolbox - Choose Items
2. choose COM Components tab and there "Adobe PDF Reader"
3. Now Drag&Drop the Adobe PDF Reader Control into an UserControl
I have successfully added this, opened up a pdf file also. Now it automatically provides with vertical scrollbars for scrolling through the pdf document.
What i want to achieve is instead of using the given scrollbars or mouse to scroll, i want to use a button to scroll scroll the pdf, So there will be two buttons, One for Scroll Up And the other for scroll down.
I have gone through many forums, pages, etc. Havnt found anythn that i could use.
I have Tried Simulating key presses with
SendKeys.Send("{DOWN}");
But as i press the button, the focus is lost on the adobe reader so it doesnt work
Pls help me... I have spent almost half a day searchin for a solution
|
|
|
|
|
You might try using this with SendKeys: "+^H" ... see what happens. [^].
“I'm an artist: it's self evident that word implies looking for something all the time without ever finding it in full. It is the opposite of saying : 'I know all about it. I've already found it.'
As far as I'm concerned, the word means: 'I am looking. I am hunting for it. I am deeply involved.'”
Vincent Van Gogh
|
|
|
|
|
I tried it, but it did nothing, What was it supposed to do???
|
|
|
|
|
Hi All.
public enum tipologie_oggetti { moda,modb,modc}
i would like to add "modd" using a combobox or a textbox or any other controls so to have as a result :
public enum tipologie_oggetti { moda,modb,modc,modd}
I don t know if it is possible.thanks
|
|
|
|
|
What is the context of this question?
Controls usually don't change the source code.
|
|
|
|
|
It is exactly what i nedeed.Controls can not change code.So my next question is: what if i wanted to enable the user to add a new item to my enumerator?
|
|
|
|