Click here to Skip to main content
15,886,795 members
Home / Discussions / C#
   

C#

 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
Arjun Mourya24-Dec-13 2:07
Arjun Mourya24-Dec-13 2:07 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
Sampath Sridhar24-Dec-13 6:32
Sampath Sridhar24-Dec-13 6:32 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
OriginalGriff24-Dec-13 7:35
mveOriginalGriff24-Dec-13 7:35 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
Sampath Sridhar24-Dec-13 19:18
Sampath Sridhar24-Dec-13 19:18 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
Arjun Mourya27-Dec-13 1:54
Arjun Mourya27-Dec-13 1:54 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
OriginalGriff27-Dec-13 2:12
mveOriginalGriff27-Dec-13 2:12 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
Arjun Mourya29-Dec-13 18:10
Arjun Mourya29-Dec-13 18:10 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
Arjun Mourya24-Dec-13 0:23
Arjun Mourya24-Dec-13 0:23 
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:

C#
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();//300 gives a total of 3 seconds pause
        }
 
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            
            int count = 0;
            string prev = "";
            foreach (string line in File.ReadLines(dataFile))
            {
                
                
                if (backgroundWorker1.CancellationPending)//checks for cancel request
                {
                    //e.Cancel = true;
                    break;
                }
                backgroundWorker1.ReportProgress(count);//reports a percentage between 0 and 100
                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)//it doesn't matter if the BG worker ends normally, or gets cancelled,
	            {              //both cases RunWorkerCompleted is invoked, so we need to check what has happened
	                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();//makes the backgroundworker stop
        }
            
    }


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
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
OriginalGriff24-Dec-13 0:33
mveOriginalGriff24-Dec-13 0:33 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
harold aptroot24-Dec-13 1:01
harold aptroot24-Dec-13 1:01 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
OriginalGriff24-Dec-13 1:12
mveOriginalGriff24-Dec-13 1:12 
QuestionXtraReport vs RDLC Pin
Jassim Rahma21-Dec-13 23:43
Jassim Rahma21-Dec-13 23:43 
AnswerRe: XtraReport vs RDLC Pin
Pete O'Hanlon22-Dec-13 1:31
mvePete O'Hanlon22-Dec-13 1:31 
AnswerRe: XtraReport vs RDLC Pin
Eddy Vluggen23-Dec-13 7:30
professionalEddy Vluggen23-Dec-13 7:30 
QuestionGridview with Faced filters Pin
lucian Gore21-Dec-13 14:35
lucian Gore21-Dec-13 14:35 
QuestionScroll into a Adobe Reader using buttons c# Pin
alfie.max1521-Dec-13 8:26
alfie.max1521-Dec-13 8:26 
AnswerRe: Scroll into a Adobe Reader using buttons c# Pin
BillWoodruff21-Dec-13 14:22
professionalBillWoodruff21-Dec-13 14:22 
GeneralRe: Scroll into a Adobe Reader using buttons c# Pin
alfie.max1521-Dec-13 17:52
alfie.max1521-Dec-13 17:52 
Questionenumerators Pin
gianfrancoguzzo21-Dec-13 4:08
gianfrancoguzzo21-Dec-13 4:08 
GeneralRe: enumerators Pin
harold aptroot21-Dec-13 4:09
harold aptroot21-Dec-13 4:09 
GeneralRe: enumerators Pin
gianfrancoguzzo21-Dec-13 4:17
gianfrancoguzzo21-Dec-13 4:17 
GeneralRe: enumerators Pin
harold aptroot21-Dec-13 4:29
harold aptroot21-Dec-13 4:29 
GeneralRe: enumerators Pin
gianfrancoguzzo21-Dec-13 4:41
gianfrancoguzzo21-Dec-13 4:41 
GeneralRe: enumerators Pin
Dave Kreskowiak21-Dec-13 6:42
mveDave Kreskowiak21-Dec-13 6:42 
GeneralRe: enumerators Pin
gianfrancoguzzo22-Dec-13 4:24
gianfrancoguzzo22-Dec-13 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.