插入數據時指定列,和忽略列對應,未被指定的列將被忽略。html
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).InsertColumns(a => a.Title).ExecuteAffrows();
執行SQL以下:sql
INSERT INTO `tb_topic`(`Title`) VALUES(?Title0), (?Title1), (?Title2), (?Title3), (?Title4), (?Title5), (?Title6), (?Title7), (?Title8), (?Title9)
fsql.Insert<Topic>(items).InsertColumns(a =>new { a.Title, a.Clicks }).ExecuteAffrows();
執行SQL以下:數據庫
INSERT INTO `tb_topic`(`Clicks`, `Title`) VALUES(?Clicks0, ?Title0), (?Clicks1, ?Title1), (?Clicks2, ?Title2), (?Clicks3, ?Title3), (?Clicks4, ?Title4), (?Clicks5, ?Title5), (?Clicks6, ?Title6), (?Clicks7, ?Title7), (?Clicks8, ?Title8), (?Clicks9, ?Title9)
方法 | 返回值 | 參數 | 描述 |
---|---|---|---|
AppendData | <this> | T1 | IEnumerable
|
追加準備插入的實體 |
InsertIdentity | <this> | 無 | 指明插入自增列 |
InsertColumns | <this> | Lambda | 只插入的列 |
IgnoreColumns | <this> | Lambda | 忽略的列 |
WithTransaction | <this> | DbTransaction | 設置事務對象 |
ToSql | string | 返回即將執行的SQL語句 | |
ExecuteAffrows | long | 執行SQL語句,返回影響的行數 | |
ExecuteIdentity | long | 執行SQL語句,返回自增值 | |
ExecuteInserted | List<T1> | 執行SQL語句,返回插入後的記錄 |
(一)入門api
(三)實體特性this
(五)插入數據htm
(七)插入數據時忽略列blog
(八)插入數據時指定列