OLEDBException: 没有为 vb.net 中的一个或多个必需参数指定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15822240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:07:15  来源:igfitidea点击:

OLEDBException: No value given for one or more required parameters in vb.net

asp.netvb.netparametersoledbexception

提问by Megan

I'm new to programming and am having a problem with a web application I am coding in VB.net. I have an Access database in my project and am using a parameterized query to retrieve information from it. The weird thing is, I use almost the same query on another page that works fine, but this one gives me teh "No value given for one or more required parameters" error. I have been searching most of the day and realize the question has been asked but can't find the solution to my problem. I think I am using the "?" correctly, have the right number of parameters in the right order, and everything is spelled right. Here is my code:

我是编程新手,我在 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()

etc.

等等。

The program errors at the Dim reader at OleDBDataReader line. I used a breakpoint there to check everything and both parameters had the correct value. I appreciate any help, thanks.

OleDBDataReader 行的 Dim 读取器处的程序错误。我在那里使用了一个断点来检查所有内容,并且两个参数都具有正确的值。我感谢任何帮助,谢谢。

回答by LarsTech

Names that have spaces require brackets:

有空格的名称需要括号:

"FROM [Rainfall Probability] "

Your numbered field names might also:

您的编号字段名称还可能:

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