mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-20 02:29:39 +08:00
Code optimization
This commit is contained in:
@@ -77,6 +77,8 @@
|
|||||||
<Compile Include="Models\OrderItem.cs" />
|
<Compile Include="Models\OrderItem.cs" />
|
||||||
<Compile Include="Demo\Demo0_SqlSugarClient.cs" />
|
<Compile Include="Demo\Demo0_SqlSugarClient.cs" />
|
||||||
<Compile Include="Models\ViewOrder.cs" />
|
<Compile Include="Models\ViewOrder.cs" />
|
||||||
|
<Compile Include="UnitTest\UInsert.cs" />
|
||||||
|
<Compile Include="UnitTest\UQueryable2.cs" />
|
||||||
<Compile Include="UnitTest\UQueue.cs" />
|
<Compile Include="UnitTest\UQueue.cs" />
|
||||||
<Compile Include="_OldTest\BugTest\Bug2.cs" />
|
<Compile Include="_OldTest\BugTest\Bug2.cs" />
|
||||||
<Compile Include="_OldTest\BugTest\Bug5.cs" />
|
<Compile Include="_OldTest\BugTest\Bug5.cs" />
|
||||||
|
@@ -31,6 +31,7 @@ namespace OrmTest
|
|||||||
}
|
}
|
||||||
public static void Init()
|
public static void Init()
|
||||||
{
|
{
|
||||||
|
Insert();
|
||||||
Queue();
|
Queue();
|
||||||
CodeFirst();
|
CodeFirst();
|
||||||
Updateable();
|
Updateable();
|
||||||
|
69
Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs
Normal file
69
Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace OrmTest
|
||||||
|
{
|
||||||
|
public partial class NewUnitTest
|
||||||
|
{
|
||||||
|
public static void Insert()
|
||||||
|
{
|
||||||
|
var db = Db;
|
||||||
|
db.CodeFirst.InitTables<UinitBlukTable>();
|
||||||
|
db.Insertable(new List<UinitBlukTable>
|
||||||
|
{
|
||||||
|
new UinitBlukTable(){ Id=1,Create=DateTime.Now, Name="00" },
|
||||||
|
new UinitBlukTable(){ Id=2,Create=DateTime.Now, Name="11" }
|
||||||
|
|
||||||
|
}).UseSqlServer().ExecuteBlueCopy();
|
||||||
|
var dt = db.Queryable<UinitBlukTable>().ToDataTable();
|
||||||
|
dt.Rows[0][0] = 3;
|
||||||
|
dt.Rows[1][0] = 4;
|
||||||
|
dt.TableName = "[UinitBlukTable]";
|
||||||
|
db.Insertable(dt).UseSqlServer().ExecuteBlueCopy();
|
||||||
|
db.Insertable(new List<UinitBlukTable2>
|
||||||
|
{
|
||||||
|
new UinitBlukTable2(){ Id=5, Name="55" },
|
||||||
|
new UinitBlukTable2(){ Id=6, Name="66" }
|
||||||
|
|
||||||
|
}).UseSqlServer().ExecuteBlueCopy();
|
||||||
|
db.Ado.BeginTran();
|
||||||
|
db.Insertable(new List<UinitBlukTable2>
|
||||||
|
{
|
||||||
|
new UinitBlukTable2(){ Id=7, Name="77" },
|
||||||
|
new UinitBlukTable2(){ Id=8, Name="88" }
|
||||||
|
|
||||||
|
}).UseSqlServer().ExecuteBlueCopy();
|
||||||
|
var task= db.Insertable(new List<UinitBlukTable2>
|
||||||
|
{
|
||||||
|
new UinitBlukTable2(){ Id=9, Name="9" },
|
||||||
|
new UinitBlukTable2(){ Id=10, Name="10" }
|
||||||
|
|
||||||
|
}).UseSqlServer().ExecuteBlueCopyAsync();
|
||||||
|
task.Wait();
|
||||||
|
db.Ado.CommitTran();
|
||||||
|
var list = db.Queryable<UinitBlukTable>().ToList();
|
||||||
|
db.DbMaintenance.TruncateTable<UinitBlukTable>();
|
||||||
|
if (string.Join("", list.Select(it => it.Id)) != "12345678910")
|
||||||
|
{
|
||||||
|
throw new Exception("Unit Insert");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class UinitBlukTable
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
[SqlSugar.SugarColumn(IsNullable =true)]
|
||||||
|
public DateTime? Create { get; set; }
|
||||||
|
}
|
||||||
|
[SqlSugar.SugarTable("UinitBlukTable")]
|
||||||
|
public class UinitBlukTable2
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public int Id { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
12
Src/Asp.Net/SqlServerTest/UnitTest/UQueryable2.cs
Normal file
12
Src/Asp.Net/SqlServerTest/UnitTest/UQueryable2.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace OrmTest.UnitTest
|
||||||
|
{
|
||||||
|
public partial class NewUnitTest
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
@@ -36,12 +36,10 @@ namespace SqlSugar
|
|||||||
this.Context.Ado.Connection.Close();
|
this.Context.Ado.Connection.Close();
|
||||||
throw ex;
|
throw ex;
|
||||||
}
|
}
|
||||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
|
CloseDb();
|
||||||
{
|
|
||||||
this.Context.Ado.Connection.Close();
|
|
||||||
}
|
|
||||||
return DbColumnInfoList.Count;
|
return DbColumnInfoList.Count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> ExecuteBlueCopyAsync()
|
public async Task<int> ExecuteBlueCopyAsync()
|
||||||
{
|
{
|
||||||
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
|
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
|
||||||
@@ -79,6 +77,7 @@ namespace SqlSugar
|
|||||||
SqlBulkCopy copy = GetBulkCopyInstance();
|
SqlBulkCopy copy = GetBulkCopyInstance();
|
||||||
copy.DestinationTableName = this.Builder.GetTranslationColumnName(dt.TableName);
|
copy.DestinationTableName = this.Builder.GetTranslationColumnName(dt.TableName);
|
||||||
copy.WriteToServer(dt);
|
copy.WriteToServer(dt);
|
||||||
|
CloseDb();
|
||||||
return dt.Rows.Count;
|
return dt.Rows.Count;
|
||||||
}
|
}
|
||||||
private DataTable GetCopyWriteDataTable(DataTable dt)
|
private DataTable GetCopyWriteDataTable(DataTable dt)
|
||||||
@@ -154,11 +153,12 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
return dt;
|
return dt;
|
||||||
}
|
}
|
||||||
private object AddParameter(int i,string dbColumnName, object value)
|
private void CloseDb()
|
||||||
{
|
{
|
||||||
var name =Builder.SqlParameterKeyWord+dbColumnName+i;
|
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
|
||||||
InsertBuilder.Parameters.Add(new SugarParameter(name,value));
|
{
|
||||||
return name;
|
this.Context.Ado.Connection.Close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user