vb.net函数错误从字符串转换为double类型

时间:2015-03-07 21:10:46

标签: vb.net

我有这个vb.net功能:

Function CheckBillingRun(customer, type, month, year)
        Dim conn = New MySqlConnection()
        Dim myCommand As New MySqlCommand
        Dim reader As MySqlDataReader
        Dim SQL As String
        Dim result As String

        conn.ConnectionString = "server=" + global_variables.web_server_ip + "; user id=" + global_variables.web_server_username + "; password=" + global_variables.web_server_password + "; database=" + global_variables.web_server_database + "; "

        conn.Open()
        SQL = "SELECT COUNT(sequence) from billing_runs WHERE customer = '" + customer + "' AND type = '" + type + "' AND MONTH(datetime) = '" + month + "' AND YEAR(datetime) = '" + year + "' "
        myCommand.Connection = conn
        myCommand.CommandText = SQL
        reader = myCommand.ExecuteReader
        reader.Read()
        result = reader.GetString(0)
        conn.Close()

        Return result

    End Function

我正在尝试使用此代码在我的应用程序中调用它:

If CheckBillingRun(reader.GetString(0), "Voice Billing", DateTime.Now.ToString("MM"), DateTime.Now.ToString("yyyy") > 0) Then
                Continue While
            End If

reader.getstring(0)等于278

但我收到一个错误说:

Additional information: Conversion from string "SELECT COUNT(sequence) from bill" to type 'Double' is not valid.

2 个答案:

答案 0 :(得分:3)

VB.NET中的字符串连接运算符是&或+,但是如果您使用+运算符并且您为项目设置了Option Strict Off,则可能会出现这样的意外情况。编译器知道传递给此函数的一个或多个参数不是字符串而是数字,在此上下文中,+运算符会尝试将所有内容转换为数字。尝试使用&运算符以连接字符串。

说,不要强制字符串来构建sql命令 使用参数化查询来避免Sql Injection和其他解析问题。

例如,您的代码可能是这样的

Function CheckBillingRun(ByVal customer as String , ByVale type as String, _
                         ByVal month as Integer, ByVal year as Integer) as Integer
    Dim SQL = "SELECT COUNT(sequence) from billing_runs " & _ 
              "WHERE customer = @customer AND type = @type " & _ 
              "AND MONTH(datetime) = @month AND YEAR(datetime) = @year"
    Using conn = New MySqlConnection()
    Using myCommand As New MySqlCommand(SQL, conn)
        Dim result As Integer = 0
        conn.ConnectionString = "......."
        conn.Open()
        myCommand.Parameters.Add("@customer", MySqlDbType.VarChar).Value = customer
        myCommand.Parameters.Add("@type", MySqlDbType.VarChar).Value = type
        myCommand.Parameters.Add("@month", MySqlDbType.Int).Value = month
        myCommand.Parameters.Add("@year", MySqlDbType.Int).Value = year
        Using reader = myCommand.ExecuteReader
            if reader.Read() Then
                result = reader.GetInteger(0)
            End If
        End Using
        Return result
    End Using
    End Using
End Function

调用它
CheckBillingRun(reader.GetString(0), "Voice Billing", _ 
                DateTime.Now.Month, DateTime.Now.Year)

在此版本中,使用的每个变量都指定了其类型,以避免编译器进行任何可能的不需要的自动转换。另请注意,COUNT返回一个数字,而不是字符串。像对待数字一样处理数字是一个很快就要解雇的习惯。查看项目属性,尝试将Option Strict设置为ON

时会发生什么

答案 1 :(得分:0)

如果方法CheckBillingRun的第一个参数接受double,则应使用Convert.ToDouble()方法将值从string转换为double,像这样:

而不是

If CheckBillingRun(reader.GetString(0), .....

执行以下操作:

If CheckBillingRun(Convert.ToDouble(reader.GetString(0)), .....

一切顺利。