【发布时间】: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 Evil、AddWithValue is evil! 和 Can we stop using AddWithValue() already?。 -
数据库中小数列的精度和小数位数是否保留默认值?如果没有,您可能需要在添加参数时指定这些值。
-
你还遇到这个问题吗?如果您的问题已经解决,请考虑accepting the correct answer。
标签: mysql vb.net visual-studio