Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
Dapper
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
Dapper
Commits
c9569d3e
Commit
c9569d3e
authored
Jun 02, 2014
by
Marc Gravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Proposal for TVP support; one concern is that it won't play nicely with things like mini-profiler
parent
12d7617f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
1 deletion
+57
-1
SqlMapper.cs
Dapper NET40/SqlMapper.cs
+42
-1
Tests.cs
Tests/Tests.cs
+15
-0
No files found.
Dapper NET40/SqlMapper.cs
View file @
c9569d3e
...
@@ -558,7 +558,7 @@ static SqlMapper()
...
@@ -558,7 +558,7 @@ static SqlMapper()
typeMap
[
typeof
(
DateTime
?)]
=
DbType
.
DateTime
;
typeMap
[
typeof
(
DateTime
?)]
=
DbType
.
DateTime
;
typeMap
[
typeof
(
DateTimeOffset
?)]
=
DbType
.
DateTimeOffset
;
typeMap
[
typeof
(
DateTimeOffset
?)]
=
DbType
.
DateTimeOffset
;
typeMap
[
typeof
(
TimeSpan
?)]
=
DbType
.
Time
;
typeMap
[
typeof
(
TimeSpan
?)]
=
DbType
.
Time
;
typeMap
[
typeof
(
O
bject
)]
=
DbType
.
Object
;
typeMap
[
typeof
(
o
bject
)]
=
DbType
.
Object
;
}
}
/// <summary>
/// <summary>
/// Configire the specified type to be mapped to a given db-type
/// Configire the specified type to be mapped to a given db-type
...
@@ -3502,6 +3502,18 @@ public void Dispose()
...
@@ -3502,6 +3502,18 @@ public void Dispose()
}
}
}
}
}
}
/// <summary>
/// Used to pass a DataTable as a TableValuedParameter
/// </summary>
public
static
ICustomQueryParameter
AsTableValuedParameter
(
this
DataTable
table
,
string
typeName
#if !CSHARP30
=
null
#endif
)
{
return
new
TableValuedParameter
(
table
,
typeName
);
}
}
}
/// <summary>
/// <summary>
...
@@ -3779,6 +3791,34 @@ public T Get<T>(string name)
...
@@ -3779,6 +3791,34 @@ public T Get<T>(string name)
}
}
}
}
/// <summary>
/// Used to pass a DataTable as a TableValuedParameter
/// </summary>
sealed
partial
class
TableValuedParameter
:
Dapper
.
SqlMapper
.
ICustomQueryParameter
{
private
readonly
DataTable
table
;
private
readonly
string
typeName
;
/// <summary>
/// Create a new instance of TableValuedParameter
/// </summary>
public
TableValuedParameter
(
DataTable
table
)
:
this
(
table
,
null
)
{
}
/// <summary>
/// Create a new instance of TableValuedParameter
/// </summary>
public
TableValuedParameter
(
DataTable
table
,
string
typeName
)
{
this
.
table
=
table
;
this
.
typeName
=
typeName
;
}
void
SqlMapper
.
ICustomQueryParameter
.
AddParameter
(
IDbCommand
command
,
string
name
)
{
var
param
=
new
System
.
Data
.
SqlClient
.
SqlParameter
(
name
,
SqlDbType
.
Structured
);
param
.
Value
=
(
object
)
table
??
DBNull
.
Value
;
if
(!
string
.
IsNullOrEmpty
(
typeName
))
param
.
TypeName
=
typeName
;
command
.
Parameters
.
Add
(
param
);
}
}
/// <summary>
/// <summary>
/// This class represents a SQL string, it can be used if you need to denote your parameter is a Char vs VarChar vs nVarChar vs nChar
/// This class represents a SQL string, it can be used if you need to denote your parameter is a Char vs VarChar vs nVarChar vs nChar
/// </summary>
/// </summary>
...
@@ -4170,6 +4210,7 @@ public partial class DbString
...
@@ -4170,6 +4210,7 @@ public partial class DbString
}
}
public
partial
class
SimpleMemberMap
public
partial
class
SimpleMemberMap
{
{
...
...
Tests/Tests.cs
View file @
c9569d3e
...
@@ -2822,6 +2822,21 @@ class HasDoubleDecimal
...
@@ -2822,6 +2822,21 @@ class HasDoubleDecimal
public
decimal
?
D
{
get
;
set
;
}
public
decimal
?
D
{
get
;
set
;
}
}
}
public
void
DataTableParameters
()
{
try
{
connection
.
Execute
(
"drop type MyTVPType"
);
}
catch
{
}
connection
.
Execute
(
"create type MyTVPType as table (id int)"
);
connection
.
Execute
(
"create proc #DataTableParameters @ids MyTVPType readonly as select count(1) from @ids"
);
var
table
=
new
DataTable
{
Columns
=
{
{
"id"
,
typeof
(
int
)
}
},
Rows
=
{
{
1
},
{
2
},
{
3
}
}
};
int
count
=
connection
.
Query
<
int
>(
"#DataTableParameters"
,
new
{
ids
=
table
.
AsTableValuedParameter
()
},
commandType
:
CommandType
.
StoredProcedure
).
First
();
count
.
IsEqualTo
(
3
);
count
=
connection
.
Query
<
int
>(
"select count(1) from @ids"
,
new
{
ids
=
table
.
AsTableValuedParameter
(
"MyTVPType"
)
}).
First
();
count
.
IsEqualTo
(
3
);
}
#if POSTGRESQL
#if POSTGRESQL
class
Cat
class
Cat
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment