您在从连接到本地SQL数据库的SQL语句填充DataGridViewer时遇到问题。这应该很简单,但是我不知道为什么它没有填充。
我基本上有3个表,如下所示-
在我的表单中,我有一个组合框连接到我的播放列表表,该菜单框允许用户选择要显示的播放列表-
Private Sub Playlist_Control_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim con As New SqlConnection With {.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\coolj\OneDrive\Documents\Loop\Loop\Songs_SQL.mdf;Integrated Security=True"}
Dim cmd As New SqlCommand("SELECT * FROM Playlist", con)
Dim Ada As New SqlDataAdapter(cmd)
Dim Table As New DataTable()
Ada.Fill(Table)
ComboBox1.DataSource = Table
ComboBox1.DisplayMember = "PLAYLIST_NAME"
ComboBox1.ValueMember = "PLAYLIST_NAME"
End Sub
然后,当组合框值更改时,我的datagridviewer被触发,该操作旨在选择并显示指定播放列表中的所有歌曲,并执行以下代码-
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim con As New SqlConnection With {.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\coolj\OneDrive\Documents\Loop\Loop\Songs_SQL.mdf;Integrated Security=True"}
con.Open()
Dim cmd As New SqlCommand("SELECT SONG_ID, SONG_NAME FROM SONGS_DB WHERE SONG_ID IN (SELECT SONG_ID FROM SONG_PLAYLIST WHERE PLAYLIST_ID IN (SELECT PLAYLIST_ID FROM Playlist WHERE PLAYLIST_NAME = (@NAME)))", con)
cmd.Parameters.AddWithValue("@NAME", ComboBox1.SelectedItem.ToString())
Dim Ada As New SqlDataAdapter(cmd)
Dim Table As New DataTable()
Ada.Fill(Table)
DataGridView1.DataSource = Table
con.Close()
End Sub
为什么不填充?网格显示“ SONG_ID”和“ SONG_NAME”的列标题,但不显示值。感谢所有帮助。