怎么把reader读到得DBnull数据类型转换double类型
''' <summary>''' 根据用户标示符获取用户记录
''' </summary>
''' <param name="orderld ">用户标示符</param>
''' <returns>返回Userinfo对象。如果用户不存在就返回Nothing</returns>
''' <remarks></remarks>
Public Function GetExpense(ByVal orderld As String) As AllexpenseInfo
Dim engin As New MyDbEngine
Dim cmdText As String
cmdText = "select*from 消费总单 where 手工单号=" & orderld
engin.OpenConnect() '打开数据库连接
Dim reader As Common.DbDataReader = engin.ExecuteReader(cmdText)
'判断是否存在记录
If IsNothing(reader) Then
engin.CloseConnect()
Return Nothing
End If
If reader.Read Then
Dim aExpense As New AllexpenseInfo
aExpense.Orderld = reader("手工单号").ToString.Trim()
aExpense.UserName = reader("操作员").ToString.Trim()
aExpense.Memname = reader("会员姓名").ToString.Trim()
aExpense.DishDiscount = reader("折扣")
aExpense.HotDish = reader("热菜总额")
aExpense.CodeDish = reader("凉菜总额")
aExpense.DrinksDish = reader("酒水总额")
aExpense.CakeDish = reader("糕点总额")
aExpense.AllDish = reader("消费总额")
reader.Close()
engin.CloseConnect()
Return aExpense
End If
Return Nothing
End Function
在这里面的“折扣”等都是列名 在SQL里面设置的double类型 aExpense.HotDish aExpense.AllDish aExpense.DrinksDish 等都是在VB里的double类型的
默认值是0.0 读出来之后就变string类型了
然后赋给那些类的double变量就说无法强制转化
CDbl函数也不行的 1.发错版块了。
2.先判断读回的记录不等于DBNull.Value再进行转换。 Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.IO
''' <summary>
''' 数据库操作类。提供了执行查询等数据库操作方法。
''' </summary>
''' <remarks></remarks>
Public Class MyDbEngine
Private Shared ReadOnly DB_CONNECTION As String = " Data Source=WWW-9942C74A67E\SQL;Initial Catalog=ShanDong;Persist Security Info=True;User ID=sa;Password=19880706"
Private conn As SqlConnection = Nothing
''' <summary>
''' 判断数据库是否已经打开
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function lsConnectionOpened() As Boolean
If IsNothing(conn) Then
Return False
End If
Return True
End Function
''' <summary>
''' 打开数据库链接
''' </summary>
''' <remarks></remarks>
Public Sub OpenConnect()
If lsConnectionOpened() = False Then
conn = New SqlConnection(DB_CONNECTION)
conn.Open()
End If
End Sub
''' <summary>
''' 关闭数据库链接
''' </summary>
''' <remarks></remarks>
Public Sub CloseConnect()
If lsConnectionOpened() Then
conn.Close()
conn = Nothing
End If
End Sub
''' <summary>
''' 执行sql语句,并且返回影响的行数
''' </summary>
''' <param name="cmdText"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function ExecuteNonQuery(ByVal cmdText As String) As Integer
OpenConnect()
Dim cmd As SqlCommand = New SqlCommand(cmdText, conn)
Return cmd.ExecuteNonQuery()
End Function
''' <summary>
''' 执行sql存储过程,并且返回影响的行数
''' </summary>
''' <param name="orlder "></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function ExecuteNonQueryProc(ByVal orlder As String) As Integer
OpenConnect()
Dim cmdText As String = "up_Adishsum()"
Dim cmd As SqlCommand = New SqlCommand(cmdText, conn)
cmd.CommandType = CommandType.StoredProcedure
Dim pid = New SqlParameter("@All", SqlDbType.VarChar)
pid.Value = orlder.ToString()
cmd.Parameters.Add(pid)
Return cmd.ExecuteNonQuery()
End Function
''' <summary>
''' 执行sql语句 并且返回结果集
''' </summary>
''' <param name="cmdText">SQL 语句。</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function ExecuteReader(ByVal cmdText As String) As DbDataReader
OpenConnect()
Dim cmd As SqlCommand = New SqlCommand(cmdText, conn)
Return cmd.ExecuteReader()
End Function
''' <summary>
''' 判断数据表中某个字段是否包含指定值。
''' </summary>
''' <param name="tableName">表名。</param>
''' <param name="fieldName">字段名。</param>
''' <param name="fieldValue">字段对应的内容,必须是字符串型。</param>
''' <returns>如果表中已经含有该值的几录就返回true,否则就返回false。</returns>
''' <remarks>
''' 实例:
''' 判断表中是否含有编是'0001'的会员
''' Dim engin As New MyDbEngine
''' Dim isExists As Boolean
''' isExists=engin.lsValueExists
''' </remarks>
Public Function lsValueExists(ByVal tableName As String, ByVal fieldName As String, ByVal fieldValue As String) As Boolean
Dim engin As New MyDbEngine
Dim cmdText As String
cmdText = "select from" + tableName + "where" + fieldName + " ='" + fieldValue + " ' "
Dim reader As DbDataReader = Me.ExecuteReader(cmdText)
reader.Read()
Dim isExists As Boolean = reader.HasRows
reader.Close()
Return isExists
End Function
''' <summary>
''' 判断数据表中某个字段是否包含指定值。
''' </summary>
''' <param name="tableName">表名。</param>
''' <returns>如果表中已经含有该值的几录就返回true,否则就返回false。</returns>
''' <remarks>
''' 实例:
''' 判断表中是否含有编是'0001'的会员
''' Dim engin As New MyDbEngine
''' Dim isExists As Boolean
''' isExists=engin.lsValueExists
''' </remarks>
Public Function lsAllExists(ByVal tableName As String) As Boolean
Dim engin As New MyDbEngine
Dim cmdText As String
cmdText = "select* from " + tableName + ""
Dim reader As DbDataReader = Me.ExecuteReader(cmdText)
reader.Read()
Dim isExists As Boolean = reader.HasRows
reader.Close()
Return isExists
End Function
End Class
这是一个类
Public Class AllexpenseInfo
'手工单号
Private strOrderld As String
Public Property Orderld() As String
Get
Return Me.strOrderld
End Get
Set(ByVal value As String)
Me.strOrderld = value
End Set
End Property
'操作员
Private strUserName As String
Public Property UserName() As String
Get
Return Me.strUserName
End Get
Set(ByVal value As String)
Me.strUserName = value
End Set
End Property
'会员姓名
Private strMemname As String
Public Property Memname() As String
Get
Return Me.strMemname
End Get
Set(ByVal value As String)
Me.strMemname = value
End Set
End Property
'折扣
Private strDishDiscount As Double
Public Property DishDiscount() As Double
Get
Return Me.strDishDiscount
End Get
Set(ByVal value As Double)
Me.strDishDiscount = value
End Set
End Property
'热菜总额
Private strHotDish As Double
Public Property HotDish() As Double
Get
Return Me.strHotDish
End Get
Set(ByVal value As Double)
Me.strHotDish = value
End Set
End Property
'热菜总额
Private strCodeDish As Double
Public Property CodeDish() As Double
Get
Return Me.strCodeDish
End Get
Set(ByVal value As Double)
Me.strCodeDish = value
End Set
End Property
'酒水总额
Private strDrinksDish As Double
Public Property DrinksDish() As Double
Get
Return Me.strDrinksDish
End Get
Set(ByVal value As Double)
Me.strDrinksDish = value
End Set
End Property
'糕点总额
Private strCakeDish As Double
Public Property CakeDish() As Double
Get
Return Me.strCakeDish
End Get
Set(ByVal value As Double)
Me.strCakeDish = value
End Set
End Property
'消费总额
Private strAllDish As Double
Public Property AllDish() As Double
Get
Return Me.strAllDish
End Get
Set(ByVal value As Double)
Me.strAllDish = value
End Set
End Property
Public Overrides Function ToString() As String
Return Me.strOrderld
End Function
End Class
这俩类是那个函数用到的
那怎么写那个判断语句
if rader.(....)= dbnull.value
还是怎么滴 我是新手 对于VB里的东西不是很熟悉...
补充:VB , 数据库(包含打印,安装,报表)