mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-19 01:58:13 +08:00
-
This commit is contained in:
15
SqlServerTest/Demo/DbFirst.cs
Normal file
15
SqlServerTest/Demo/DbFirst.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest.Demo
|
||||
{
|
||||
public class DbFirst
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -8,7 +8,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.Demo
|
||||
{
|
||||
public class Delete
|
||||
public class Delete:DemoBase
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
@@ -29,17 +29,5 @@ namespace OrmTest.Demo
|
||||
//by expression
|
||||
var t5 = db.Deleteable<Student>().Where(it => it.Id == 1).ExecuteCommand();
|
||||
}
|
||||
|
||||
public static SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
|
||||
db.Ado.IsEnableLogEvent = true;
|
||||
db.Ado.LogEventStarting = (sql, pars) =>
|
||||
{
|
||||
Console.WriteLine(sql + "\r\n" + db.RewritableMethods.SerializeObject(pars));
|
||||
Console.WriteLine();
|
||||
};
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
23
SqlServerTest/Demo/DemoBase.cs
Normal file
23
SqlServerTest/Demo/DemoBase.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest.Demo
|
||||
{
|
||||
public class DemoBase
|
||||
{
|
||||
public static SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
|
||||
db.Ado.IsEnableLogEvent = true;
|
||||
db.Ado.LogEventStarting = (sql, pars) =>
|
||||
{
|
||||
Console.WriteLine(sql + "\r\n" + db.RewritableMethods.SerializeObject(pars));
|
||||
Console.WriteLine();
|
||||
};
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
@@ -8,13 +8,14 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.Demo
|
||||
{
|
||||
public class Insert
|
||||
public class Insert:DemoBase
|
||||
{
|
||||
public static void Init() {
|
||||
public static void Init()
|
||||
{
|
||||
var db = GetInstance();
|
||||
db.IgnoreColumns.Add("TestId", "Student");
|
||||
|
||||
var insertObj = new Student() { Name = "jack", CreateTime = Convert.ToDateTime("2010-1-1") ,SchoolId=1};
|
||||
var insertObj = new Student() { Name = "jack", CreateTime = Convert.ToDateTime("2010-1-1"), SchoolId = 1 };
|
||||
|
||||
//Insert reutrn Insert Count
|
||||
var t2 = db.Insertable(insertObj).ExecuteCommand();
|
||||
@@ -24,20 +25,20 @@ namespace OrmTest.Demo
|
||||
|
||||
|
||||
//Only insert Name
|
||||
var t4 = db.Insertable(insertObj).InsertColumns(it => new { it.Name,it.SchoolId }).ExecuteReutrnIdentity();
|
||||
var t4 = db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.SchoolId }).ExecuteReutrnIdentity();
|
||||
|
||||
|
||||
//Ignore TestId
|
||||
var t5 = db.Insertable(insertObj).IgnoreColumns(it => new { it.Name, it.TestId }).ExecuteReutrnIdentity();
|
||||
|
||||
|
||||
|
||||
|
||||
//Ignore TestId
|
||||
var t6 = db.Insertable(insertObj).IgnoreColumns(it => it == "Name" || it == "TestId").ExecuteReutrnIdentity();
|
||||
|
||||
|
||||
|
||||
//Use Lock
|
||||
var t8 = db.Insertable(insertObj).With(SqlWith.UpdLock).ExecuteCommand();
|
||||
|
||||
|
||||
|
||||
var insertObj2 = new Student() { Name = null, CreateTime = Convert.ToDateTime("2010-1-1") };
|
||||
var t9 = db.Insertable(insertObj2).Where(true/* Is insert null */, true/*off identity*/).ExecuteCommand();
|
||||
@@ -50,17 +51,5 @@ namespace OrmTest.Demo
|
||||
}
|
||||
var s9 = db.Insertable(insertObjs.ToArray()).InsertColumns(it => new { it.Name }).ExecuteCommand();
|
||||
}
|
||||
|
||||
public static SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
|
||||
db.Ado.IsEnableLogEvent = true;
|
||||
db.Ado.LogEventStarting = (sql, pars) =>
|
||||
{
|
||||
Console.WriteLine(sql + "\r\n" + db.RewritableMethods.SerializeObject(pars));
|
||||
Console.WriteLine();
|
||||
};
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -8,7 +8,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.Demo
|
||||
{
|
||||
public class Query
|
||||
public class Query:DemoBase
|
||||
{
|
||||
|
||||
public static void Init()
|
||||
@@ -273,18 +273,5 @@ namespace OrmTest.Demo
|
||||
.Select("st.*").ToList();
|
||||
//SELECT st.* FROM [Student] st Left JOIN School sh ON sh.id=st.schoolid WHERE st.id>@id
|
||||
}
|
||||
|
||||
|
||||
public static SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig{ ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
|
||||
db.Ado.IsEnableLogEvent = true;
|
||||
db.Ado.LogEventStarting = (sql, pars) =>
|
||||
{
|
||||
Console.WriteLine(sql + "\r\n" + db.RewritableMethods.SerializeObject(pars));
|
||||
Console.WriteLine();
|
||||
};
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -8,9 +8,10 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.Demo
|
||||
{
|
||||
public class Update
|
||||
public class Update : DemoBase
|
||||
{
|
||||
public static void Init() {
|
||||
public static void Init()
|
||||
{
|
||||
var db = GetInstance();
|
||||
var updateObj = new Student() { Id = 1, Name = "jack", SchoolId = 0, CreateTime = Convert.ToDateTime("2017-05-21 09:56:12.610") };
|
||||
var updateObjs = new List<Student>() { updateObj, new Student() { Id = 2, Name = "sun", SchoolId = 0 } }.ToArray();
|
||||
@@ -19,7 +20,7 @@ namespace OrmTest.Demo
|
||||
|
||||
|
||||
//update reutrn Update Count
|
||||
var t1= db.Updateable(updateObj).ExecuteCommand();
|
||||
var t1 = db.Updateable(updateObj).ExecuteCommand();
|
||||
|
||||
//Only update Name
|
||||
var t3 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name }).ExecuteCommand();
|
||||
@@ -47,23 +48,12 @@ namespace OrmTest.Demo
|
||||
|
||||
//Update By Expression Where By Expression
|
||||
var t10 = db.Updateable<Student>()
|
||||
.UpdateColumns(it => new Student() { Name="a",CreateTime=DateTime.Now })
|
||||
.UpdateColumns(it => new Student() { Name = "a", CreateTime = DateTime.Now })
|
||||
.Where(it => it.Id == 11).ExecuteCommand();
|
||||
|
||||
//Rename
|
||||
db.Updateable<School>().AS("Student").UpdateColumns(it => new School() { Name = "jack" }).Where(it => it.Id == 1).ExecuteCommand();
|
||||
//Update Student set Name='jack' Where Id=1
|
||||
}
|
||||
public static SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
|
||||
db.Ado.IsEnableLogEvent = true;
|
||||
db.Ado.LogEventStarting = (sql, pars) =>
|
||||
{
|
||||
Console.WriteLine(sql + "\r\n" + db.RewritableMethods.SerializeObject(pars));
|
||||
Console.WriteLine();
|
||||
};
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -48,7 +48,9 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Config.cs" />
|
||||
<Compile Include="Demo\DbFirst.cs" />
|
||||
<Compile Include="Demo\Delete.cs" />
|
||||
<Compile Include="Demo\DemoBase.cs" />
|
||||
<Compile Include="Demo\Insert.cs" />
|
||||
<Compile Include="Demo\Query.cs" />
|
||||
<Compile Include="Demo\Update.cs" />
|
||||
|
Reference in New Issue
Block a user