使用SqlHelper的一个小技巧
2012-03-10 12:53
246 查看
在数据库设计时,也许会有一些约定,说一下我自己的三点基本硬性规定:
1、所有字段都为可空设定(主键、布尔类型字段除外)
2、字符串类型不允许有前后空格(可能特殊情况时除外)
3、如果是空字符串,则应存储Null(为了防止不同数据库类型对字符串为空和NULL的不同对待)
第一点是设计时的事情,在这里可以略过,而后两点则是归程序判断处理的,也许细心的你发现了,第二点和第三点是衔接的。
接下来我们来看一下后两点是如何实现的,以TextBox为例,我是这样进行的:
这段代码并不难理解,不过需要说明的是,必填的字段为了验证是否有值Trim是得有的,而不必填的字段实质上则只需要赋值的那一行,Trim和Null则可以交由底层SqlHelper里进行处理。
首先我们定义一个配置类来进行控制:
前三项正是我们要实现的功能的开关,而最后一项NullValueToDBNull则需要另外说明一下了:
在实体类中,值类型我都是用Nullable(Of T)来存储的,这当中就包含了Null的情况,而在传递至数据库时,Null是作为默认值还是DBNull呢?这是不确定的,所以这个开关就是用于约定Null作为DBNull处理。
接下来就是对SqlHelper的改造了,需要改动的只有一个方法:PrepareCommand
可以看到根据Parameter的DbType作了相应的处理,这样处理后,非必填的字段,就只以只用一句赋值语句,剩下的去空白字符和Null判断就交由底层处理了,省心省力!~~~
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判断就交由底层处理了,省心省力!~~~
相关文章推荐
- Python:通过自定义系统级快捷键来控制程序开始或停止记录日志(使用小技巧解决一个貌似无解的问题)
- Python中AND、OR的一个使用小技巧
- 关于NSTimer的一个使用小技巧
- Mac下使用自带终端SSH功能的一个小技巧
- Python中AND、OR的一个使用小技巧
- LotusScript中使用ArrayAppend的一个小技巧
- 在VS2010调试过程中使用条件断点的一个小技巧
- 使用WINDOWS上的命令解释程序(cmd)的一个小技巧
- 使用WinEdt 6 避免跳出注册对话框的一个小技巧
- Java中数组或集合声明使用的一个小技巧
- 在母版页(MasterPage)中使用 jQuery 的一个小技巧
- 使用dwr时动态生成table的一个小技巧
- 开发中关于正则表达式使用的一个小技巧
- 关于如何使用VS高亮显示无扩展名源码文件的一个小技巧
- TextView使用小技巧——一个TextView多重属性设置(SpannableStringBuilder)
- 使用控件的一个小技巧
- 如何使用MSDN搜索STL相关知识的一个小技巧
- VS2015使用小技巧 给一个项目添加类
- 使用GridLayout布局时的一个小技巧(margin)
- 一个使用Linq的小技巧