• SQLite大批量插入性能优化


      SQLite作为轻量级,零安装的数据库,用在小型桌面应用程序上特别合适。

      网上搜了一下,貌似在程序中无法直接从格式化文本或CSV文件导入SQLite,只能逐条insert,这一点比起SQL SERVER就差了一些。

      好在SQLite经过优化后大批量插入速度也还可以,方法就是事务+参数化,直接上代码。

    string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString;
                using (SQLiteConnection con = new SQLiteConnection(strconn))
                {
                    con.Open();
                    using (SQLiteTransaction trans = con.BeginTransaction())
                    {
                        using (SQLiteCommand cmd = new SQLiteCommand(con))
                        {
                            cmd.Transaction = trans;
                            try
                            {
                                using (FileStream fs = File.OpenRead("*.txt"))
                                {
                                    using (StreamReader sr = new StreamReader(fs))
                                    {
                                        string sline;
                                        string []arr;
                                        string str;
                                        rCount = 0;
                                        while(!sr.EndOfStream)
                                        {
                                            sline = sr.ReadLine();
                                            arr = sline.Split(',');
                                            str = "INSERT INTO XXXTABLE (A,B,C,D,E,F,G,H,I,J) VALUES(@A,@B,@C,@D,@E,@F,@G,@H,@I,@J)";
                                            cmd.CommandText = str;
                                            cmd.Parameters.AddWithValue("@A", arr[0]);
                                            cmd.Parameters.AddWithValue("@B", arr[1]);
                                            cmd.Parameters.AddWithValue("@C", arr[2]);
                                            cmd.Parameters.AddWithValue("@D", arr[3]);
                                            cmd.Parameters.AddWithValue("@E", arr[4]);
                                            cmd.Parameters.AddWithValue("@F", arr[5]);
                                            cmd.Parameters.AddWithValue("@G", arr[6]);
                                            cmd.Parameters.AddWithValue("@H", arr[7]);
                                            cmd.Parameters.AddWithValue("@I", arr[8]);
                                            cmd.Parameters.AddWithValue("@J", arr[9]);
                                            cmd.ExecuteNonQuery();
                                            rCount = rCount + 1;
                                        }
                                    }
                                }
                                trans.Commit();
                                //5,清空txt
                                System.IO.File.WriteAllText(path + "*.txt", "", System.Text.Encoding.Default);
                            }
                            catch(Exception ex)
                            {
                                rCount = 0;
                                trans.Rollback();
                            }
                        }
                    }
                }
  • 相关阅读:
    转:yum和aptget用法及区别
    APT upgrade 和 distupgrade 的差別
    flickr api 入门教程
    查看FileZila 快速连接密码
    Freemium模式
    asp.net 开发环境搭建
    flickr api authentication
    转:虚拟机VirtualBox中Ubuntu无法全屏解决方法
    转:网页设计中的默认字体样式详解
    Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  • 原文地址:https://www.cnblogs.com/hryan/p/7988750.html
Copyright © 2020-2023  润新知