Created
April 2, 2014 15:04
-
-
Save cloudflying/9936009 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Function ActualCommandTextByNames( | |
ByVal sender As IDbCommand) As String | |
Dim sb As New System.Text.StringBuilder(sender.CommandText) | |
Dim EmptyParameterNames = | |
( | |
From T In sender.Parameters.Cast(Of IDataParameter)() | |
Where String.IsNullOrWhiteSpace(T.ParameterName) | |
).FirstOrDefault | |
If EmptyParameterNames IsNot Nothing Then | |
Return sender.CommandText | |
End If | |
For Each p As IDataParameter In sender.Parameters | |
Select Case p.DbType | |
Case _ | |
DbType.AnsiString, | |
DbType.AnsiStringFixedLength, | |
DbType.Date, DbType.DateTime, | |
DbType.DateTime2, DbType.Guid, | |
DbType.String, | |
DbType.StringFixedLength, | |
DbType.Time, | |
DbType.Xml | |
If p.ParameterName(0) = "@" Then | |
If p.Value Is Nothing Then | |
Throw New Exception("no value given for parameter '" & | |
p.ParameterName & "'") | |
End If | |
sb = sb.Replace(p.ParameterName, String.Format("'{0}'", | |
p.Value.ToString.Replace("'", "''"))) | |
Else | |
sb = sb.Replace(String.Concat("@", p.ParameterName), | |
String.Format("'{0}'", p.Value.ToString.Replace("'", "''"))) | |
End If | |
Case Else | |
sb = sb.Replace(p.ParameterName, p.Value.ToString) | |
End Select | |
Next | |
Return sb.ToString | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment