从多个表中选择而不会出现参数错误

时间:2018-02-15 15:29:10

标签: sql vb.net ms-access

我目前正在尝试为音乐家记录日志编写数据库项目。我一直在努力在一次搜索数据库时加入3个表,我收到了这个错误:

没有给出一个或多个必需参数的值。

我一直在网上搜索为什么会这样,但找不到任何可以修复我的代码的东西。我试过几乎逐字复制其他语法,但仍然得到这个错误。任何帮助将不胜感激,我的数据库如下:

Program(ProgramID(PK), LengthOfProgram, UserName, NameOfProgram) 
ProgramList(PieceListID(PK), PieceID(FK), ProgramID(FK))
Repertoire(PieceID(PK), NameOfPiece, Composer) 

这些是保留曲目所需的字段。

这是我从列表框中选择程序时的代码。

Public dtbConnecter As New OleDbConnection
Dim fileLocator As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source = " + Application.StartupPath + "\Musician Record Log.accdb"

Private Sub lstPro_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstPro.SelectedIndexChanged

    Dim programID As String
    Dim programIDLength As Integer
    Dim selectedString As String = lstPro.GetItemText(lstPro.SelectedItem)
    programIDLength = selectedString.IndexOf(":")
    programID = selectedString.Substring(0, programIDLength)
    MsgBox(programID)

    Dim SQL As String = "Select Program.ProgramName, Program.ProgramLength, Repertoire.NameOfPiece, Repertoire.Composer From ((ProgramList INNER JOIN Repertoire ON ProgramList.PieceID = Repertoire.PieceID) INNER JOIN Program ON ProgramList.ProgramID = Program.ProgramID) WHERE (Program.ProgramID = @Param1)"
    Dim da As New OleDbDataAdapter
    Dim dtbTable As New DataTable
    Dim ds As New DataSet
    Dim dtbCommand As New OleDbCommand
    dtbCommand.Parameters.Add("@Param1", OleDbType.VarChar).Value = programID
    dtbCommand.Connection = dtbConnecter
    dtbCommand.CommandType = CommandType.Text

    dtbCommand.CommandText = SQL
    da.SelectCommand = dtbCommand
    da.Fill(dtbTable)

    'Here i would then use the table's information to update my form, however first i'm trying to get this to work. 

    da.Dispose()
    da = Nothing

End Sub

我还会在顶部导入System.DataSystem.Data.OleDbSystem.Drawing.Imaging

1 个答案:

答案 0 :(得分:0)

这只是一个愚蠢的错误,数据库中字段的名称是NameOfProgram和LengthOfProgram,但在SQL查询中我使用了ProgramName和ProgramLength,感谢所有帮助确保SQL查询正确并且我确实需要更正数据类型,但主要问题是错误的标识符。