static void Main(string[] args)
{
ConnectionConfig connectionConfig = new ConnectionConfig()
{
DbType = DbType.SqlServer,
ConnectionString = "Server=.;Initial Catalog=Test;User ID=Test;Password=Test",
IsAutoCloseConnection = true
};
using (SqlSugarClient db = new SqlSugarClient(connectionConfig))
{
//調式程式碼 用來列印SQL
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
};
//新增返回ID
var entity = db.Insertable<EmailEntity>(new EmailEntity()
{
EmailArr = "test@test.com",
EmailContent = "測試郵件,請忽略"
}).ExecuteReturnEntity();
//根據ID更新
db.Updateable<EmailEntity>(new EmailEntity()
{
Id = entity.Id,
EmailArr = "test@test.com",
EmailContent = "測試郵件,請忽略111"
}).UpdateColumns(it => new { it.EmailArr, it.EmailContent }).ExecuteCommandAsync();
//刪除
db.Deleteable<EmailEntity>(new EmailEntity()
{
Id = entity.Id
}).ExecuteCommandAsync();
//按查詢條件查詢
string querParam = "測試";
var exp = Expressionable.Create<EmailEntity>();
exp.AndIF(!string.IsNullOrWhiteSpace(querParam), it => it.EmailContent.Contains(querParam));
//exp.OrIF(true, it => it.EmailContent.Contains(querParam));
db.Queryable<EmailEntity>().Where(exp.ToExpression()).ForEach(it =>
{
Console.WriteLine(it.EmailContent);
});
}
Console.ReadKey();
}