sqlite批量插入

 

public static int ExecuteNonQuery<T>(string commandText, string dbName,List<T> ts)
        {
            int r = 0;
            Type t = typeof(T);
            var propertyList = t.GetProperties();
            using (SQLiteConnection cn = new SQLiteConnection())
            {
                string path = _basePath + "data\\" + dbName;
                cn.ConnectionString = GetConnectionString(path);
                cn.Open();
                var trans = cn.BeginTransaction();
                var cmd = new SQLiteCommand(commandText, cn);
                try
                {
                    foreach (var _ts in ts)
                    {
                        List<object> paramList = new List<object>();
                        foreach (var _p in propertyList)
                        {
                            cmd.Parameters.AddWithValue("@" + _p.Name, _p.GetValue(_ts, null));
                        }
                        cmd.ExecuteNonQuery();
                    }
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                }
            }
              
            return r;
        }

 

static void InsertBatch()
        {
            var binList = new List<Bin> { };
            string sql = @"insert into T_Bin (ProjectId,OrderNo,DeviceNum,DeviceName,DeviceModel,Price,Amount,CreateTime) 
values (@ProjectId,@OrderNo,@DeviceNum,@DeviceName,@DeviceModel,@Price,@Amount,@CreateTime)";
            for(var i = 0; i < 1000000; i++)
            {
                binList.Add(new Bin {
                    ProjectId=1,
                    OrderNo= i+1,
                    DeviceNum= "13AL1",
                    DeviceName= "配电箱",
                    DeviceModel= "PXT",
                    Price= 3914.73,
                    Amount= 3914.73,
                    Remark= "南一通道配电系统图一"
                });
            }
            SqlData.ExecuteNonQuery<Bin>(sql, "Project_1.db", binList);
        }

 

posted @ 2018-09-10 16:59  wjl910  阅读(751)  评论(0)    收藏  举报