【问题标题】:AddWithValue MySqlType.DecimalAddWithValue MySqlType.Decimal
【发布时间】:2023-04-08 01:03:02
【问题描述】:

很遗憾,我无法继续前进。使用 vs2019 和 vb。创建了一个级联的 DropDownList(国家、州、地区)。它非常适合我不必使用 AddWithValue (在哪里)的国家。在我必须使用 AddWithValue (where) 的状态下,我没有得到它。所有 ID 都定义为整数。相关代码:

Protected Sub idLand_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    DropDownBundesland.Items.Clear()
    DropDownBundesland.Items.Add(New ListItem("--Select Country--", ""))
    DropDownRegion.Items.Clear()
    DropDownRegion.Items.Add(New ListItem("--Select City--", ""))

    DropDownBundesland.AppendDataBoundItems = True
    Dim strConnString As [String] = ConfigurationManager _
               .ConnectionStrings("conString").ConnectionString
    Dim strQuery As [String] = "Select Bundesland, idBundesland, idLand from Campingplatz ORDER BY Bundesland ASC where idLand = @LandID Group BY Bundesland"
    Dim con As New MySqlConnection(strConnString)
    Dim cmd As New MySqlCommand()
    cmd.Parameters.AddWithValue("@LandID", MySqlDbType.Decimal).Value = DropDownLand.SelectedItem.Value
    cmd.CommandType = CommandType.Text
    cmd.CommandText = strQuery
    cmd.Connection = con
    Try
        con.Open()
        DropDownBundesland.DataSource = cmd.ExecuteReader()
        DropDownBundesland.DataTextField = "Bundesland"
        DropDownBundesland.DataValueField = "idBundesland"
        DropDownBundesland.DataBind()
        If DropDownBundesland.Items.Count > 1 Then
            DropDownBundesland.Enabled = True
        Else
            DropDownBundesland.Enabled = False
            DropDownRegion.Enabled = False
        End If
    Catch ex As Exception
        'Throw ex
    Finally
        con.Close()
        con.Dispose()
    End Try
End Sub

请帮忙!

【问题讨论】:

  • 我相信像这样调用它cmd.Parameters.AddWithValue("@LandID", MySqlDbType.Decimal).Value = DropDownLand.SelectedItem.Value会给你返回一个参数,当你给它赋值时,它是一个本地副本,而不是集合中的参数。但我从来没有见过这样称呼它,所以我不确定。
  • .AddWithValue("@LandID", MySqlDbType.Decimal) 没有做你想让它做的事情 - 第二个参数是值:AddWithValue(String, Object) Method。另外,请注意:AddWithValue is EvilAddWithValue is evil!Can we stop using AddWithValue() already?
  • 数据库中小数列的精度和小数位数是否保留默认值?如果没有,您可能需要在添加参数时指定这些值。
  • 你还遇到这个问题吗?如果您的问题已经解决,请考虑accepting the correct answer

标签: mysql vb.net visual-studio


【解决方案1】:

这个查询是错误的:

Dim strQuery As [String] = "Select Bundesland, idBundesland, idLand from Campingplatz ORDER BY Bundesland ASC where idLand = @LandID Group BY Bundesland"

您必须更改 WHEREORDER BYGROUP BY 的顺序,例如:

Dim strQuery As [String] = "Select Bundesland, idBundesland, idLand from Campingplatz WHERE idLand = @LandID Group BY Bundesland ORDER BY Bundesland ASC"

在这里你可以看到语法:https://mariadb.com/kb/en/select/

【讨论】:

    【解决方案2】:

    我不喜欢将用户界面代码与数据库代码混在一起。数据库代码应该独立于使用它的应用程序类型。

    Connections、Commands 和 DataReaders 需要调用它们的 Dispose 方法,以便它们可以释放非托管资源。为您提供了 Using...End Using 块来完成此操作并关闭连接。

    您正在混合构建参数集合的苹果和橙子。我在代码中同时显示了AddAddWithValue

    请在Catch 块中查看我的笔记。

    如果类中的任何其他方法访问数据库,我会将strConString 设为类级变量。

    Protected Sub idLand_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        DropDownBundesland.Items.Clear()
        DropDownBundesland.Items.Add(New ListItem("--Select Country--", ""))
        DropDownRegion.Items.Clear()
        DropDownRegion.Items.Add(New ListItem("--Select City--", ""))
        DropDownBundesland.AppendDataBoundItems = True
        Dim dt As DataTable = Nothing
        Try
            dt = GetDropDownData(CDec(DropDownLand.SelectedItem.Value))
        Catch ex As Exception
            'Where would you be throwing it to? This is an event procedure so is not "called"
            'Alert the user, maybe log the error
            Exit Sub
        End Try
        DropDownBundesland.DataSource = dt
        DropDownBundesland.DataTextField = "Bundesland"
        DropDownBundesland.DataValueField = "idBundesland"
        DropDownBundesland.DataBind()
        If DropDownBundesland.Items.Count > 1 Then
            DropDownBundesland.Enabled = True
        Else
            DropDownBundesland.Enabled = False
            DropDownRegion.Enabled = False
        End If
    End Sub
    
    Private strConnString As [String] = ConfigurationManager.ConnectionStrings("conString").ConnectionString
    
    Private Function GetDropDownData(LandID As Decimal) As DataTable
        Dim dt As New DataTable
        Dim strQuery As [String] = "Select Bundesland, idBundesland, idLand 
                                   From Campingplatz 
                                   Where idLand = @LandID 
                                   Group BY Bundesland 
                                   ORDER BY Bundesland ASC "
        Using con As New MySqlConnection(strConnString),
                cmd As New MySqlCommand(strQuery, con)
            cmd.Parameters.Add("@LandID", MySqlDbType.Decimal).Value = LandID
            'or cmd.Parameters.AddWithValue("@LandID", LandID)
            con.Open()
            Using reader = cmd.ExecuteReader
                dt.Load(reader)
            End Using
        End Using
        Return dt
    End Function
    

    【讨论】:

      猜你喜欢
      • 2018-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      • 2017-10-12
      • 1970-01-01
      • 2012-11-07
      相关资源
      最近更新 更多