SqlSugar源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.5 KiB

8 years ago
using OrmTest.Models;
using SqlSugar;
using System;
8 years ago
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest.Demo
{
8 years ago
public class Insert:DemoBase
8 years ago
{
8 years ago
public static void Init()
{
8 years ago
var db = GetInstance();
6 years ago
db.DbMaintenance.TruncateTable("student");
8 years ago
db.IgnoreColumns.Add("TestId", "Student");
8 years ago
var insertObj = new Student() { Name = "jack", CreateTime = Convert.ToDateTime("2010-1-1"), SchoolId = 1 };
8 years ago
//Insert reutrn Insert Count
var t2 = db.Insertable(insertObj).ExecuteCommand();
//Insert reutrn Identity Value
var t3 = db.Insertable(insertObj).ExecuteReturnIdentity();
8 years ago
//Insert reutrn Identity Value
var t31 = db.Insertable(insertObj).ExecuteReturnEntity();
8 years ago
//Only insert Name and SchoolId
var t4 = db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.SchoolId }).ExecuteReturnIdentity();
var t4_1 = db.Insertable(insertObj).InsertColumns("Name","SchoolId").ExecuteReturnIdentity();
8 years ago
//Ignore TestId
var t5 = db.Insertable(insertObj).IgnoreColumns(it => new { it.Name, it.TestId }).ExecuteReturnIdentity();
8 years ago
8 years ago
//Ignore TestId
var t6 = db.Insertable(insertObj).IgnoreColumns( "Name","TestId").ExecuteReturnIdentity();
8 years ago
8 years ago
//Use Lock
var t8 = db.Insertable(insertObj).With(SqlWith.UpdLock).ExecuteCommand();
8 years ago
8 years ago
var insertObj2 = new Student() { Name = null, CreateTime = Convert.ToDateTime("2010-1-1") };
var t9 = db.Insertable(insertObj2).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand();
8 years ago
//Insert List<T>
var insertObjs = new List<Student>();
for (int i = 0; i < 10; i++)
8 years ago
{
insertObjs.Add(new Student() { Name = "name" + i });
}
8 years ago
var t10 = db.Insertable(insertObjs.ToArray()).InsertColumns(it => new { it.Name }).ExecuteCommand();
var t11 = db.Insertable(insertObjs.ToArray()).ExecuteCommand();
7 years ago
var t12 = db.Insertable(insertObj).IgnoreColumns(it => it == "Name" || it == "TestId").ExecuteReturnIdentityAsync();
t12.Wait();
var dt = new Dictionary<string, object>();
dt.Add("name", "1");
dt.Add("CreateTime", null);
7 years ago
var t66 = db.Insertable(dt).AS("student").ExecuteReturnIdentity();
8 years ago
}
8 years ago
}
}