不幸的是,我不能再進一步了。使用 vs2019 和 vb。創建了級聯 DropDownList(國家、州、地區)。它非常適合我不必使用 AddWithValue (where) 的國家/地區。在我必須使用 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
請幫忙!
uj5u.com熱心網友回復:
這個查詢是錯誤的:
Dim strQuery As [String] = "Select Bundesland, idBundesland, idLand from Campingplatz ORDER BY Bundesland ASC where idLand = @LandID Group BY Bundesland"
您必須更改WHERE、ORDER BY和GROUP BY的順序,例如:
Dim strQuery As [String] = "Select Bundesland, idBundesland, idLand from Campingplatz WHERE idLand = @LandID Group BY Bundesland ORDER BY Bundesland ASC"
在這里你可以看到 Ssyntax:https ://mariadb.com/kb/en/select/
uj5u.com熱心網友回復:
我不喜歡將用戶界面代碼與資料庫代碼混合在一起。資料庫代碼應該獨立于使用它的應用程式型別。
Connections、Commands 和DataReaders 需要Dispose呼叫它們的方法,以便它們可以釋放非托管資源。您可以使用Using...End Using塊來完成此操作并關閉連接。
您正在混合蘋果和橙子來構建引數集合。我在代碼中同時展示了Add和AddWithValue。
請參閱我在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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/327056.html
