当前位置:Gxlcms > 数据库问题 > 使用SqlHelper的一个小技巧

使用SqlHelper的一个小技巧

时间:2021-07-01 10:21:17 帮助过:1人阅读

‘在验证数据时进行
Me.txtRemark.Trim()

‘在保存时进行
With info
If (Me.txtRemark.HasValue) Then
.Remark = Me.txtRemark.Text
Else
.Remark = Nothing
End If
End With
技术分享


这段代码并不难理解,不过需要说明的是,必填的字段为了验证是否有值Trim是得有的,而不必填的字段实质上则只需要赋值的那一行,Trim和Null则可以交由底层SqlHelper里进行处理。

 

首先我们定义一个配置类来进行控制:

技术分享
‘‘‘ <summary>
‘‘‘ Database configuration
‘‘‘ </summary>
Friend NotInheritable Class Config

Removes all occurrences of white space characters
Public Shared ReadOnly TrimString As Boolean = True
translate the empty string to null
Public Shared ReadOnly EmptyStringToNull As Boolean = True
translate the null boolean to false
Public Shared ReadOnly NullBooleanToFalse As Boolean = True
translate the null value to dbnull value
Public Shared ReadOnly NullValueToDBNull As Boolean = True

End Class
技术分享


前三项正是我们要实现的功能的开关,而最后一项NullValueToDBNull则需要另外说明一下了:

在实体类中,值类型我都是用Nullable(Of T)来存储的,这当中就包含了Null的情况,而在传递至数据库时,Null是作为默认值还是DBNull呢?这是不确定的,所以这个开关就是用于约定Null作为DBNull处理。

 

接下来就是对SqlHelper的改造了,需要改动的只有一个方法:PrepareCommand

技术分享
‘‘‘ <summary>
‘‘‘ This method opens (if necessary) and assigns a connection, transaction, command type and parameters
‘‘‘ to the provided command.
‘‘‘ </summary>
‘‘‘ <param name="command">the SqlCommand to be prepared</param>
‘‘‘ <param name="connection">a valid SqlConnection, on which to execute this command</param>
‘‘‘ <param name="transaction">a valid SqlTransaction, or ‘null‘</param>
‘‘‘ <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
‘‘‘ <param name="commandText">the stored procedure name or T-SQL command</param>
‘‘‘ <param name="commandParameters">an array of SqlParameters to be associated with the command or ‘null‘ if no parameters are required</param>
Private Shared Sub PrepareCommand(ByVal command As SqlCommand, _
ByVal connection As SqlConnection, _
ByVal transaction As SqlTransaction, _
ByVal commandType As CommandType, _
ByVal commandText As String, _
ByVal commandParameters() As SqlParameter)

if the provided connection is not open, we will open it
If connection.State <> ConnectionState.Open Then
connection.Open()
End If

associate the connection with the command
command.Connection = connection

set the command text (stored procedure name or SQL statement)
command.CommandText = commandText

if we were provided a transaction, assign it.
If Not (transaction Is Nothing) Then
command.Transaction = transaction
End If

set the command type
command.CommandType = commandType

attach the command parameters if they are provided
If Not (commandParameters Is Nothing) Then
For Each p As SqlParameter In commandParameters
If (p.Direction <> ParameterDirection.Output) Then
Select Case p.DbType
Case DbType.String, DbType.StringFixedLength, DbType.AnsiString, DbType.AnsiStringFixedLength
If (Not p.Value Is Nothing AndAlso Not p.Value Is DBNull.Value) Then
Dim str As String = p.Value.ToString()

If (Config.TrimString) Then
str = str.Trim()
End If

If (Config.EmptyStringToNull AndAlso str.Length = 0) Then
str = Nothing
End If

p.Value = str
End If
Case DbType.Boolean
If (Config.NullBooleanToFalse AndAlso p.Value Is Nothing) Then
p.Value = False
End If
End Select

If (Config.NullValueToDBNull AndAlso p.Value Is Nothing) Then
p.Value = DBNull.Value
End If
End If

command.Parameters.Add(p)
Next
End If

End Sub PrepareCommand
技术分享


可以看到根据Parameter的DbType作了相应的处理,这样处理后,非必填的字段,就只以只用一句赋值语句,剩下的去空白字符和Null判断就交由底层处理了,省心省力!~~~

使用SqlHelper的一个小技巧

标签:

人气教程排行