Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
szjay
WinFramework
提交
314044c2
W
WinFramework
项目概览
szjay
/
WinFramework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
WinFramework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
314044c2
编写于
10月 18, 2018
作者:
S
szjay
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
no commit message
上级
247bf6fb
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
27 addition
and
85 deletion
+27
-85
DomainBase/Core/DbContextExt.cs
DomainBase/Core/DbContextExt.cs
+1
-2
Utilities/DbUtil.cs
Utilities/DbUtil.cs
+6
-0
Utilities/SqlServerDao.cs
Utilities/SqlServerDao.cs
+20
-83
未找到文件。
DomainBase/Core/DbContextExt.cs
浏览文件 @
314044c2
...
...
@@ -136,8 +136,7 @@ namespace Framework.DomainBase
throw
new
ServiceException
(
"{0}未定义SQL"
,
entityType
.
Name
);
}
TEntity
entity
=
new
TEntity
();
string
sql
=
(
string
)
fi
.
GetValue
(
entity
);
string
sql
=
(
string
)
fi
.
GetValue
(
null
);
_SqlDict
.
Add
(
entityType
.
FullName
,
sql
);
return
sql
;
}
...
...
Utilities/DbUtil.cs
浏览文件 @
314044c2
...
...
@@ -24,6 +24,12 @@ namespace Infrastructure.Utilities
{
public
static
string
GetTableName
(
Type
type
)
{
FieldInfo
fi
=
ReflectHelper
.
GetField
(
type
,
"TableName"
);
if
(
fi
!=
null
)
{
return
fi
.
GetValue
(
null
).
ToString
();
}
string
tableName
=
type
.
Name
;
return
tableName
;
}
...
...
Utilities/SqlServerDao.cs
浏览文件 @
314044c2
...
...
@@ -22,7 +22,8 @@ namespace Infrastructure.Utilities
//有连接池,自动管理事务。
public
class
SqlServerDao
:
IDisposable
{
private
const
DataSourceSetting
.
DbTypes
dbType
=
DataSourceSetting
.
DbTypes
.
SqlServer
;
private
SqlConnection
conn
=
null
;
private
SqlTransaction
tran
=
null
;
public
SqlServerDao
(
DataSourceSetting
setting
)
{
...
...
@@ -48,24 +49,8 @@ namespace Infrastructure.Utilities
private
set
;
}
private
Dictionary
<
Guid
,
SqlConnection
>
connPool
=
new
Dictionary
<
Guid
,
SqlConnection
>();
private
Dictionary
<
Guid
,
SqlTransaction
>
transPool
=
new
Dictionary
<
Guid
,
SqlTransaction
>();
private
SqlConnection
GetConnection
(
bool
autoNewConnection
=
true
)
{
if
(!
connPool
.
ContainsKey
(
Setting
.
Id
))
{
if
(
autoNewConnection
)
{
connPool
.
Add
(
Setting
.
Id
,
new
SqlConnection
(
Setting
.
ConnectionString
));
}
else
{
throw
new
Exception
(
"未发现SqlConnection"
);
}
}
SqlConnection
conn
=
connPool
[
Setting
.
Id
];
if
(
conn
==
null
)
{
conn
=
new
SqlConnection
(
Setting
.
ConnectionString
);
...
...
@@ -79,63 +64,25 @@ namespace Infrastructure.Utilities
return
conn
;
}
public
bool
BeginTransaction
()
public
void
BeginTransaction
()
{
if
(
transPool
.
ContainsKey
(
Setting
.
Id
))
{
return
true
;
}
if
(!
transPool
.
ContainsKey
(
Setting
.
Id
))
{
SqlConnection
conn
=
connPool
[
Setting
.
Id
];
SqlTransaction
trans
=
conn
.
BeginTransaction
();
transPool
.
Add
(
Setting
.
Id
,
trans
);
}
return
true
;
tran
=
conn
.
BeginTransaction
();
}
public
bool
CommitTransaction
()
public
void
CommitTransaction
()
{
if
(!
connPool
.
ContainsKey
(
Setting
.
Id
))
{
throw
new
Exception
(
"未发现连接"
);
}
if
(!
transPool
.
ContainsKey
(
Setting
.
Id
))
if
(
tran
!=
null
)
{
t
hrow
new
Exception
(
"未发现事务"
);
t
ran
.
Commit
(
);
}
SqlTransaction
trans
=
transPool
[
Setting
.
Id
];
trans
.
Commit
();
trans
.
Dispose
();
transPool
.
Remove
(
Setting
.
Id
);
return
true
;
}
public
bool
RollbackTransaction
()
public
void
RollbackTransaction
()
{
if
(
!
connPool
.
ContainsKey
(
Setting
.
Id
)
)
if
(
tran
!=
null
)
{
t
hrow
new
Exception
(
"未发现连接"
);
t
ran
.
Rollback
(
);
}
if
(!
transPool
.
ContainsKey
(
Setting
.
Id
))
{
throw
new
Exception
(
"未发现事务"
);
}
SqlTransaction
trans
=
transPool
[
Setting
.
Id
];
trans
.
Rollback
();
trans
.
Dispose
();
transPool
.
Remove
(
Setting
.
Id
);
return
true
;
}
public
List
<
T
>
Select
<
T
>()
where
T
:
new
()
...
...
@@ -209,21 +156,21 @@ namespace Infrastructure.Utilities
public
int
Insert
<
T
>(
T
entity
)
{
string
sql
=
DbUtil
.
GenerateInsertSql
(
d
bType
,
typeof
(
T
),
entity
);
string
sql
=
DbUtil
.
GenerateInsertSql
(
Setting
.
D
bType
,
typeof
(
T
),
entity
);
int
cnt
=
Execut
(
sql
);
return
cnt
;
}
public
int
Update
<
T
>(
T
entity
)
{
string
sql
=
DbUtil
.
GenerateUpdateSql
(
d
bType
,
typeof
(
T
),
entity
);
string
sql
=
DbUtil
.
GenerateUpdateSql
(
Setting
.
D
bType
,
typeof
(
T
),
entity
);
int
cnt
=
Execut
(
sql
);
return
cnt
;
}
public
int
Delete
<
T
>(
object
id
)
{
string
sql
=
DbUtil
.
GenerateDeleteSql
(
d
bType
,
typeof
(
T
),
id
);
string
sql
=
DbUtil
.
GenerateDeleteSql
(
Setting
.
D
bType
,
typeof
(
T
),
id
);
int
cnt
=
Execut
(
sql
);
return
cnt
;
}
...
...
@@ -235,8 +182,7 @@ namespace Infrastructure.Utilities
SqlConnection
conn
=
GetConnection
();
SqlCommand
cmd
=
null
;
SqlTransaction
tran
;
if
(
transPool
.
TryGetValue
(
Setting
.
Id
,
out
tran
))
if
(
tran
!=
null
)
{
cmd
=
new
SqlCommand
(
sql
,
conn
,
tran
);
}
...
...
@@ -245,7 +191,6 @@ namespace Infrastructure.Utilities
cmd
=
new
SqlCommand
(
sql
,
conn
);
}
int
cnt
=
cmd
.
ExecuteNonQuery
();
return
cnt
;
}
...
...
@@ -266,9 +211,9 @@ namespace Infrastructure.Utilities
cmd
.
CommandText
=
procName
;
cmd
.
Parameters
.
AddRange
(
sqlParameters
.
ToArray
());
if
(
tran
sPool
.
ContainsKey
(
Setting
.
Id
)
)
if
(
tran
!=
null
)
{
cmd
.
Transaction
=
tran
sPool
[
Setting
.
Id
]
;
cmd
.
Transaction
=
tran
;
}
int
cnt
=
cmd
.
ExecuteNonQuery
();
...
...
@@ -312,23 +257,15 @@ namespace Infrastructure.Utilities
public
void
Dispose
()
{
foreach
(
KeyValuePair
<
Guid
,
SqlTransaction
>
kvp
in
transPoo
l
)
if
(
tran
!=
nul
l
)
{
if
(
kvp
.
Value
!=
null
)
{
kvp
.
Value
.
Rollback
();
//事务不提交则自动回滚。
}
tran
.
Dispose
();
}
transPool
.
Clear
();
foreach
(
KeyValuePair
<
Guid
,
SqlConnection
>
kvp
in
connPoo
l
)
if
(
conn
!=
nul
l
)
{
if
(
kvp
.
Value
!=
null
)
{
kvp
.
Value
.
Dispose
();
//释放连接。
}
conn
.
Dispose
();
}
connPool
.
Clear
();
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录