【问题标题】:Need to change str from sql query to currency需要将str从sql查询改成货币
【发布时间】:2013-09-08 06:07:46
【问题描述】:

如何让下面的 productPrice 显示为 $7.99 等?我已经尝试了我能找到的一切。这是 db 表中的小钱。谢谢。

Protected Sub printMenuBlock(ByVal productName As String) '设置存储产品的变量并从数据库中提取 暗淡产品 = ReadProduct(productName)

    'Add necessary markup to str variable, with products information within

    For i As Integer = 0 To product.Count - 1

        str += "<div class='menuItem'>"
        'str += "    <img alt='Item Picture' class='itemPicture' src='" + product(i).ImagePath.Substring(3).Replace("\", "/") + "' />"
        str += "    <div class='itemInfo'>"
        str += "        <h1 class='itemName'>"
        str += "            " + product(i).Item("ProductName") + "</h1>"
        'str += "        <h3 class='itemDescription'>"
        str += "            " + product(i).Item("ProductDescription")
        str += "        <h1 class ='itemPrice'>"
        str += "            " + String.Format("{0:C}", product(i).Item("ProductPrice")) + "</h1>"
        str += "        "
        str += "        </div>"

        str += "    </div>"

    Next

这是来自交互类的代码:

公众课堂互动 ' Sql 命令对象的新实例 Private cmdSelect As New SqlCommand ' 连接类的实例 私有 conIn 作为新连接

区域“菜单功能和子”

' Set up the SQL statement for finding a Product by ProductCat
Private Sub GetProduct(ByVal CatIn As String)
    ' SQL String

    Dim strSelect As String
    strSelect = "SELECT *  "
    strSelect &= " FROM Menu "
    'strSelect &= " WHERE ProductCat = @CatIn"
    strSelect &= "ORDER BY 'ProductCat'"
    ' Set up the connection to the datebase
    cmdSelect.Connection = conIn.Connect
    ' Add the SQL string to the connection
    cmdSelect.CommandText = strSelect
    ' Add the parameters to the connection
    cmdSelect.Parameters.Add("@CatIn", SqlDbType.NVarChar).Value = CatIn
End Sub



'Function to create list of rows and columns
Public Function ReadProduct(ByVal CatIn As String) As List(Of Dictionary(Of String, Object))
    'Declare variable to hold list
    Dim ReturnProducts As New List(Of Dictionary(Of String, Object))
    Try
        Call GetProduct(CatIn)
        Dim dbr As SqlDataReader
        ' Execute the created SQL command from GetProduct and set to the SqlDataReader object
        dbr = cmdSelect.ExecuteReader
        'Get number of columns in current row
        Dim FieldCount = dbr.FieldCount()
        Dim ColumnList As New List(Of String)
        'Loop through all columns and add to list
        For i As Integer = 0 To FieldCount - 1
            ColumnList.Add(dbr.GetName(i))
        Next
        While dbr.Read()
            'Declare variable to hold list
            Dim ReturnProduct As New Dictionary(Of String, Object)
            'Loop through all rows and add to list
            For i As Integer = 0 To FieldCount - 1
                ReturnProduct.Add(ColumnList(i), dbr.GetValue(i).ToString())
            Next
            'Add to final list
            ReturnProducts.Add(ReturnProduct)
        End While
        cmdSelect.Parameters.Clear()
        'Close connection
        dbr.Close()
    Catch ex As SqlException
        Dim strOut As String
        strOut = ex.Message
        Console.WriteLine(strOut)
    End Try
    ' Return the Product object
    Return ReturnProducts
End Function

【问题讨论】:

    标签: mysql asp.net vb.net


    【解决方案1】:

    尝试使用

    + String.Format("{0:C}", product(i).Item("ProductPrice")) +
    

    附:您可能希望将StringBuilder 用于这些类型的构造。

    【讨论】:

    • 没有语法错误,但没有成功。它仍然显示为 7.9900。我将仔细研究 StringBuilder。谢谢。
    • @Lolo 什么样的对象是产品-数据表?
    • 我编辑了原始代码,以便您可以看到整个相关代码。谢谢!
    • 好吧,那个时候效果更好。对不起我的“新手”。感谢您的帮助。
    • 很好地格式化了代码。好的,如果product(i).Item("ProductPrice").ToString("C") 有效,您可以试试吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    相关资源
    最近更新 更多