SqlSugar/Src/Asp.Net/SqlServerTest/Demos/8_JoinSql.cs

75 lines
2.5 KiB
C#
Raw Normal View History

2017-06-04 10:12:53 +08:00
using OrmTest.Demo;
using OrmTest.Models;
2017-08-25 13:18:43 +08:00
using SqlSugar;
2017-06-04 10:12:53 +08:00
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()
2017-06-04 10:20:52 +08:00
{
Where();
OrderBy();
2017-08-25 13:18:43 +08:00
SelectMerge();
ConditionalModel();
2017-06-04 10:20:52 +08:00
}
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();
}
2017-08-25 13:18:43 +08:00
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 })
2017-08-25 15:15:13 +08:00
.MergeTable().Where(XXX => XXX.id == 1).OrderBy("name asc").ToList();// Prefix, is, not, necessary, and take the columns in select
2017-06-04 10:20:52 +08:00
2017-08-25 13:18:43 +08:00
}
2017-06-04 10:20:52 +08:00
private static void Where()
{
var db = GetInstance();
//Parameterized processing
2017-06-04 10:24:07 +08:00
string value = "'jack';drop table Student";
var list = db.Queryable<Student>().Where("name=@name", new { name = value }).ToList();
//Nothing happened
2017-06-04 10:20:52 +08:00
}
private static void OrderBy()
2017-06-04 10:12:53 +08:00
{
var db = GetInstance();
//propertyName is valid
string propertyName = "Id";
2017-09-06 16:37:25 +08:00
string dbColumnName = db.EntityMaintenance.GetDbColumnName<Student>(propertyName);
2017-06-04 10:12:53 +08:00
var list = db.Queryable<Student>().OrderBy(dbColumnName).ToList();
//propertyName is invalid
try
{
propertyName = "Id'";
2017-09-06 16:37:25 +08:00
dbColumnName = db.EntityMaintenance.GetDbColumnName<Student>(propertyName);
2017-06-04 10:12:53 +08:00
var list2 = db.Queryable<Student>().OrderBy(dbColumnName).ToList();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}