var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;" + "Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10"; IFreeSql fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.MySql, connstr) .UseAutoSyncStructure(true) //自動同步實體結構到數據庫 .Build(); [Table(Name = "tb_topic")] class Topic { [Column(IsIdentity = true, IsPrimary = true)] public int Id { get; set; } public int Clicks { get; set; } public string Title { get; set; } public DateTime CreateTime { get; set; } }
var items = new List<Topic>(); for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 }); fsql.Insert<Topic>(items).ExecuteAffrows();
執行SQL以下:html
INSERT INTO `tb_topic`(`Clicks`, `Title`, `CreateTime`) VALUES(?Clicks0, ?Title0, ?CreateTime0), (?Clicks1, ?Title1, ?CreateTime1), (?Clicks2, ?Title2, ?CreateTime2), (?Clicks3, ?Title3, ?CreateTime3), (?Clicks4, ?Title4, ?CreateTime4), (?Clicks5, ?Title5, ?CreateTime5), (?Clicks6, ?Title6, ?CreateTime6), (?Clicks7, ?Title7, ?CreateTime7), (?Clicks8, ?Title8, ?CreateTime8), (?Clicks9, ?Title9, ?CreateTime9)
當插入大批量數據時,內部採用分割分批執行的邏輯進行。分割規則以下:mysql
數量 | 參數量 | |
---|---|---|
MySql | 5000 | 3000 |
PostgreSQL | 5000 | 3000 |
SqlServer | 1000 | 2100 |
Oracle | 500 | 999 |
Sqlite | 5000 | 999 |
數據:爲每批分割的大小,如批量插入 10000 條數據,在 mysql 執行時會分割爲兩批。
參數量:爲每批分割的參數量大小,如批量插入 10000 條數據,每行須要使用 5 個參數化,在 mysql 執行時會分割爲每批 3000 / 5。sql
分割執行後,當外部未提供事務時,內部自開事務,實現插入完整性。數據庫
FreeSql 適配了每一種數據類型參數化,和不參數化的使用。批量插入建議關閉參數化功能,使用 .NonoParameter() 進行執行(有關 NoneParameter 在後續文章介紹)。api
方法 | 返回值 | 參數 | 描述 |
---|---|---|---|
AppendData | <this> | T1 | IEnumerable
|
追加準備插入的實體 |
ToSql | string | 返回即將執行的SQL語句 | |
ExecuteAffrows | long | 執行SQL語句,返回影響的行數 | |
ExecuteIdentity | long | 執行SQL語句,返回自增值 | |
ExecuteInserted | List<T1> | 執行SQL語句,返回插入後的記錄 |