N
N
Nestratov2014-03-25 09:19:57
MySQL
Nestratov, 2014-03-25 09:19:57

How to implement a streaming addition to the database?

Hello. There is a code that works in one thread, please help with working in multiple threads.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using MySql.Data.MySqlClient;
using System.Threading;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        public MySqlConnection conn()
        {
            string Connect = "Database=codes;Data Source=xxxxxxxxxx;User Id=xxxxxxxx;Password=xxxxxxxx";
            MySqlConnection myConnection = new MySqlConnection(Connect);
            myConnection.Open();
 
            return myConnection;
        }
 
        public Dictionary<string, int> gen()
        {
            string str = "1234567890abcdefghijklmnopqrstuvwxyz";
            string chr = "1234567890abcdefghijklmnopqrstuvwxyz";
            string ints = "1234567890";
 
            if (this.checkBox1.Checked == true && this.checkBox2.Checked == true)
            {
                str = chr + ints;
            }
            else if (this.checkBox1.Checked == true)
            {
                str = chr;
            }
            else if (this.checkBox2.Checked == true)
            {
                str = ints;
            }
 
 
 
            int code_length = Convert.ToInt32(this.numericUpDown1.Value);
 
            var codes_count = this.numericUpDown2.Value;
 
            this.toolStripProgressBar1.Maximum = Convert.ToInt32(codes_count);
 
            byte code_separartor = 0; //$code_separartor=0;
 
 
            var tmp = new Dictionary<string, int>();
            int str_length = str.Length - 1;  //$str_length=strlen($str)-1;
 
            Random random = new Random();
 
            while (tmp.Count < codes_count) 
            {
                
                string code = ""; 
 
                for (int i = 0; i < code_length; i++)
                {
                    if (i > 0 && code_separartor > 0 && i % code_separartor == 0) 
                    {
                        code += '-';
                    }
 
                    code += str.Substring(random.Next(0, str_length), 1); //$code.=substr($str, mt_rand(0,$str_length), 1);
 
                } 
 
                tmp[code] = 1; 
 
              
            } 
 
          
            var codes = tmp.Keys; 
 
 
            return tmp;
        }
 
        public bool send_data(string code_key, MySqlConnection a)
        {
            string CommandText = "INSERT INTO codes (code, active, `delete`) values ('" + code_key + "', 1, 0)";
            MySqlCommand myCommand = new MySqlCommand(CommandText, a);
            myCommand.ExecuteNonQuery();
            
 
            return true;
        }
 
 
      /* */ public void add_to_base(/*Dictionary<string,int> tmp*/)
        {
            var step = 1;
           
          
                    var tmp = gen();
                    var a = conn();
                    var pot = 0;
                    foreach (var pkey in tmp)
                    {
                     
                     
                              send_data(pkey.Key, a);
 
                        toolStripProgressBar1.Value = step;
 
                      
                        step++;
                    }
 
           
          
 
            MessageBox.Show("Операция завершена успешно.");
        }
 
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                richTextBox1.Enabled = true;
                
            }
            else
            {
                richTextBox1.Enabled = false;
            }
        }
 
    
        private void button1_Click_1(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Генерация";
 
        
            add_to_base();
     
            toolStripStatusLabel1.Text = "Завершено";
           }
 
     
     
        
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
ask0generator, 2014-03-25
@ask0generator

My subjective opinion:
mysql - for each connection creates a personal session for each client ...
But in your case, I would still do it in one thread (that is, from one session), simply:
1. generate an array for insertion
2. would start transaction
3.2. I would disable the index
3.3. would do a massive INSERT INTO from the generated data array
3.4. would include indexes
4. would complete the transaction

N
Nikolai Turnaviotov, 2014-03-27
@foxmuldercp

in general, there is also the concept of a connection pool, but the option about insert into and a list of records is the most correct

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question