码迷,mamicode.com
首页 > 数据库 > 详细

使用SqlHelper的一个小技巧

时间:2016-07-13 09:14:07      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

在数据库设计时,也许会有一些约定,说一下我自己的三点基本硬性规定:

1、所有字段都为可空设定(主键、布尔类型字段除外)

2、字符串类型不允许有前后空格(可能特殊情况时除外)

3、如果是空字符串,则应存储Null(为了防止不同数据库类型对字符串为空和NULL的不同对待)

 

第一点是设计时的事情,在这里可以略过,而后两点则是归程序判断处理的,也许细心的你发现了,第二点和第三点是衔接的。

 

接下来我们来看一下后两点是如何实现的,以TextBox为例,我是这样进行的:

技术分享
‘在验证数据时进行
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的一个小技巧

标签:

原文地址:http://www.cnblogs.com/androidshouce/p/5665609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!