【问题标题】:How to increase speed in sql string when insert to ms access datebase插入到ms access数据库时如何提高sql字符串的速度
【发布时间】:2016-09-07 11:29:05
【问题描述】:

我有一个文本文件,它的大小约为 3mb(15,000 个字符串),我是从文本文件中插入的。处理需要 30 分钟。如何提高 ms 访问数据库的速度。如果处理时间过长,我想要进度条。

        OpenFileDialog ofd = new OpenFileDialog();         
        if (ofd.ShowDialog() == DialogResult.OK)
        {                
            FileStream fs = File.OpenRead(ofd.FileName);
            BufferedStream bs = new BufferedStream(fs);
            StreamReader sr = new StreamReader(bs);
            {
                metroProgressBar1.Maximum = Convert.ToInt32(sr.Length);
                for (int i = 0; i < sr.Length; i++)
                {                        
                    metroProgressBar1.Value++;
                }              
                string s;
                while ((s = sr.ReadLine()) != null)
                {                        
                        try
                        {
                            string pattern = @"[\d]{1,8}([.,][\d]{1,4})?";

                            Regex r = new Regex(pattern);
                            Match m = r.Match(s.Substring(77, 41));
                            while (m.Success)
                            {
                                //Console.WriteLine(m);
                                m = m.NextMatch();
                            }
                            string[] row1 = { 
                                        s.Substring(46, 31), 
                                        s.Substring(77, 41), 
                                        s.Substring(118, 8), 
                                        s.Substring(127, 10), 
                                        s.Substring(138, 10) 
                                    };
                            metroListView1.Items.Add(s.Substring(0, 45)).SubItems.AddRange(row1);
                            string cmdstr = "INSERT into GGG (event_id, device_id, parameter_id, parameter_int_id, time_id, clock_id, user_id) VALUES ('" + s.Substring(0, 45) + "', '" + s.Substring(46, 31) + "', '" + s.Substring(77, 41) + "', '" + r.Match(s.Substring(77, 41)) + "',  '" + s.Substring(118, 19) + "', '" + s.Substring(118, 8) + "',  '" + s.Substring(138, 10) + "')";
                            OleDbConnection con = new OleDbConnection(constr);
                            OleDbCommand com = new OleDbCommand(cmdstr, con);
                            try
                            {
                                con.Open();
                                com.ExecuteNonQuery();
                                con.Close();
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Error", ex.Message);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Содержимое файла не соответствует формату таблицы");
                        }
                }
                sr.Close();
            }
        }
    }

【问题讨论】:

  • 您正在执行 15000 次插入,除非您在 8086 处理器上运行,否则这不会超过几分钟。您可以尝试的第一件事是在每次调用中插入几条记录。试试这个,看看有什么影响。
  • 附带说明,您也应该将插入的字符串放入参数中。
  • 您可以使用文本驱动程序并在一条 sql 语句中插入全部内容。
  • @Fionnuala 是对的!试试这个:social.msdn.microsoft.com/Forums/en-US/…

标签: c# sql .net ms-access progress-bar


【解决方案1】:

为您的数据库操作使用单一连接。在您的代码 sn-p 中,您为文件中的每一行打开 OleDbConnection con = new OleDbConnection(constr);

应该是这样的:

OleDbConnection con = new OleDbConnection(constr);
while ((s = sr.ReadLine()) != null)
{
  // Do the job here
}
con.Close();

【讨论】:

  • 他也可以在整个过程中保持连接打开。
  • @jdl134679 当然。上面的代码sn-p只是说明思路,制作质量代码当然要根据提问者的任务。
猜你喜欢
  • 2016-01-05
  • 2019-03-04
  • 2021-11-15
  • 2016-09-14
  • 2020-11-13
  • 1970-01-01
  • 2019-09-15
  • 2017-03-05
  • 1970-01-01
相关资源
最近更新 更多