Click here to Skip to main content
15,879,535 members
Home / Discussions / C#
   

C#

 
QuestionLarge text file(1.5 gb) reading using Background worker Pin
Arjun Mourya22-Dec-13 22:56
Arjun Mourya22-Dec-13 22:56 
AnswerRe: Large text file(1.5 gb) reading using Background worker Pin
BillWoodruff23-Dec-13 0:08
professionalBillWoodruff23-Dec-13 0:08 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
Arjun Mourya23-Dec-13 3:05
Arjun Mourya23-Dec-13 3:05 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
Member 996531123-Dec-13 10:41
Member 996531123-Dec-13 10:41 
AnswerRe: Large text file(1.5 gb) reading using Background worker Pin
Manfred Rudolf Bihy23-Dec-13 0:49
professionalManfred Rudolf Bihy23-Dec-13 0:49 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
Arjun Mourya23-Dec-13 2:57
Arjun Mourya23-Dec-13 2:57 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
Eddy Vluggen23-Dec-13 7:25
professionalEddy Vluggen23-Dec-13 7:25 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
Arjun Mourya24-Dec-13 0:10
Arjun Mourya24-Dec-13 0:10 
Hi,

I was able to read the file and insert each read line into database. So in order to show progress to the user, I used Backgroundworker with progressbar.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=1234;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
        }
            
    }


What I am actually facing the problem is , every line is getting inserted twice.

For example: if you have three lines in the file, all three lines gets inserted first and again same three lines is inserted(mean to say file is read again and all lines are inserted)

BR,
Arjun

modified 24-Dec-13 6:18am.

AnswerRe: Large text file(1.5 gb) reading using Background worker Pin
OriginalGriff23-Dec-13 1:06
mveOriginalGriff23-Dec-13 1:06 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
Arjun Mourya23-Dec-13 2:50
Arjun Mourya23-Dec-13 2:50 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
OriginalGriff23-Dec-13 3:29
mveOriginalGriff23-Dec-13 3:29 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
Arjun Mourya23-Dec-13 4:05
Arjun Mourya23-Dec-13 4:05 
GeneralRe: Large text file(1.5 gb) reading using Background worker Pin
OriginalGriff23-Dec-13 4:12
mveOriginalGriff23-Dec-13 4:12 
QuestionRe: Large text file(1.5 gb) reading using Background worker Pin
Sampath Sridhar23-Dec-13 21:48
Sampath Sridhar23-Dec-13 21:48 
AnswerRe: Large text file(1.5 gb) reading using Background worker Pin
OriginalGriff24-Dec-13 0:58
mveOriginalGriff24-Dec-13 0:58 
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 
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 

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.