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.

75 lines
2.5 KiB

using OrmTest.Demo;
using OrmTest.Models;
7 years ago
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest.Demo
{
/// <summary>
/// Secure string operations
/// </summary>
public class JoinSql : DemoBase
{
public static void Init()
8 years ago
{
Where();
OrderBy();
7 years ago
SelectMerge();
ConditionalModel();
8 years ago
}
private static void ConditionalModel()
{
var db = GetInstance();
List<ConditionalModel> conModels = new List<ConditionalModel>();
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" });
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Like, FieldValue = "1" });
var student = db.Queryable<Student>().Where(conModels).ToList();
}
7 years ago
private static void SelectMerge()
{
var db = GetInstance();
//page join
var pageJoin = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.SchoolId==sc.Id
})
.Select((st, sc) => new { id = st.Id, name = sc.Name })
7 years ago
.MergeTable().Where(XXX => XXX.id == 1).OrderBy("name asc").ToList();// Prefix, is, not, necessary, and take the columns in select
8 years ago
7 years ago
}
8 years ago
private static void Where()
{
var db = GetInstance();
//Parameterized processing
8 years ago
string value = "'jack';drop table Student";
var list = db.Queryable<Student>().Where("name=@name", new { name = value }).ToList();
//Nothing happened
8 years ago
}
private static void OrderBy()
{
var db = GetInstance();
//propertyName is valid
string propertyName = "Id";
string dbColumnName = db.EntityMaintenance.GetDbColumnName<Student>(propertyName);
var list = db.Queryable<Student>().OrderBy(dbColumnName).ToList();
//propertyName is invalid
try
{
propertyName = "Id'";
dbColumnName = db.EntityMaintenance.GetDbColumnName<Student>(propertyName);
var list2 = db.Queryable<Student>().OrderBy(dbColumnName).ToList();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}