OLEDBException:没有给出vb.net中一个或多个必需参数的值

时间:2013-04-04 21:40:19

标签: asp.net vb.net parameters oledbexception

我是编程的新手,我正在使用VB.net编写的Web应用程序出现问题。我的项目中有一个Access数据库,并使用参数化查询从中检索信息。奇怪的是,我在另一个工作良好的页面上使用几乎相同的查询,但是这个给了我“没有给出一个或多个必需参数的值”错误。我一直在搜索大部分时间并且意识到问题已经被问到但是找不到我的问题的解决方案。我想我正在使用“?”正确地,以正确的顺序拥有正确数量的参数,并且所有内容拼写正确。这是我的代码:

 Dim strWthStn As String
    strWthStn = cboRainProb2.Text
    Dim strWthStnParm As New OleDb.OleDbParameter()
    strWthStnParm.Value = strWthStn

    Dim strMonth As String
    strMonth = cboRainProb.Text
    Dim strMonthParm As New OleDb.OleDbParameter()
    strMonthParm.Value = strMonth

    Dim sql As String 'Query to select the station for rainfall probability values

    sql = "SELECT 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 " &
    "FROM Rainfall Probability " &
    "WHERE Station = ?" &
    "AND Mth = ?"

    'Connects to the Access database and selects data based on query
    Dim AccessConnect As String
    AccessConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Megan\Visual Studio Projects\DroughtCalculatorND_Web\DroughtCalculatorND_Web\NDStations.mdb"
    Dim conn As New OleDb.OleDbConnection
    Dim selectCommand As New OleDbCommand(sql, conn)
    selectCommand.Parameters.Add(strWthStnParm)
    selectCommand.Parameters.Add(strMonthParm)
    conn.ConnectionString = AccessConnect
    conn.Open()

    'Reads the data into a list
    Dim reader As OleDbDataReader =
        selectCommand.ExecuteReader(CommandBehavior.CloseConnection)
    Dim RainByYear As New List(Of Double)
    reader.Read()
    RainByYear.Add(reader("1999"))
    RainByYear.Add(reader("2000"))
    RainByYear.Add(reader("2001"))
    RainByYear.Add(reader("2002"))
    RainByYear.Add(reader("2003"))
    RainByYear.Add(reader("2004"))
    RainByYear.Add(reader("2005"))
    RainByYear.Add(reader("2006"))
    RainByYear.Add(reader("2007"))
    RainByYear.Add(reader("2008"))
    RainByYear.Add(reader("2009"))
    RainByYear.Add(reader("2010"))

    RainByYear.Sort()

OleDBDataReader系列Dim阅读器的程序错误。我在那里使用断点检查所有内容,两个参数都有正确的值。感谢您的帮助,谢谢。

1 个答案:

答案 0 :(得分:3)

包含空格的名称需要括号:

"FROM [Rainfall Probability] "

您的编号字段名称也可能是:

SELECT [1999], [2000] etc...