当前位置:数据库 > SQLServer >>

使用SqlHelper的一个小技巧

在数据库设计时,也许会有一些约定,说一下我自己的三点基本硬性规定:
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

              &nbs

补充:Web开发 , ASP.Net ,
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,