0

我正在尝试在 vb.net 中执行此选择查询,但它向我抛出了 No Value Given For One or More Required Parameters 错误。我一直在重新检查我的代码,但看不出有什么问题。

SELECT使用表格提取 houseno、housename、street、productname、sequenceno 和数量DailyDelivery的语句,其中 roundID = selectedID

Command.CommandText = 
"SELECT customerid, 
       productid, 
       quantity, 
       (SELECT houseno 
        FROM   customer 
        WHERE  customer.customerid = dailydelivery.customerid) AS HouseNo, 
       (SELECT housename 
        FROM   customer 
        WHERE  customer.customerid = dailydelivery.customerid) AS HouseName, 
       (SELECT street 
        FROM   customer 
        WHERE  customer.customerid = dailydelivery.customerid) AS Street, 
       (SELECT sequenceno 
        FROM   customer 
        WHERE  customer.customerid = dailydelivery.customerid) AS SequenceNo, 
       (SELECT roundid 
        FROM   customer 
        WHERE  customer.customerid = dailydelivery.customerid) AS RoundID, 
       (SELECT productname 
        FROM   product 
        WHERE  product.productid = dailydelivery.productid)    AS ProductName 
FROM   dailydelivery 
WHERE  issuedate = @TodaysDate 
       AND roundid = @SelectedID 
ORDER  BY sequenceno, 
          street, 
          houseno, 
          housename"

'Add parameter for command (TodaysDate being today's date)
    `Command.Parameters.AddWithValue("@TodaysDate", Today.Date)`

'Add parameter for command (SelectedID being the current roundID)
    `Command.Parameters.AddWithValue("@SelectedID", SelectedID)`

在此之后,使用数据适配器调用命令以填充数据集中的表。

'Open connection to the database
dbConnection.Open()

'Set command's connection as dbconnection
Command.Connection = dbConnection

'Set the data adapter's select command as command
DataAdpt.SelectCommand = Command

'Fill ActualDelivery table in selectedDataset with the results of query, using the data adapter
DataAdpt.Fill(SelectedDataset, "ActualDelivery")

'Close connection to the database
dbConnection.Close()

错误被抛出就DataAdapt.Fill行了。我正在使用 VB Express 2008 和 Access 2013。

4

2 回答 2

0

可能我错了,但我无法测试查询。我认为您可以使用 JOIN 重构该查询

"SELECT d.customerid, d.productid, d.quantity, c.HouseNo, c.HouseName, " + 
"c.Street, c.SequenceNo, c.RoundID, p.ProductName " + 
"FROM (customer AS c INNER JOIN dailydelivery AS d ON c.customerid = d.customerid) " + 
"INNER JOIN product AS p ON p.productid = d.productid " + 
"WHERE  d.issuedate = @TodaysDate AND c.roundid = @SelectedID " + 
"ORDER  BY c.sequenceno,  c.street, c.houseno, c.housename"

再次查看您的查询。您是否roundid在dailydelivery 表上命名了一个字段,或者它是roundidcustomer 表的字段?就目前而言,您需要将该字段放在dailydelivery表上,否则它将被解释为参数的名称

于 2013-04-05T14:22:40.880 回答
0

您需要仅使用?问号来指定参数,并确保以Parameters正确的顺序将参数添加到集合中。或者只是引用文档:

OLE DB .NET 提供程序不支持将参数传递给 SQL 语句或由OleDbCommandwhenCommandType设置为Text调用的存储过程的命名参数。在这种情况下,必须使用问号 (?) 占位符。例如:

 SELECT * FROM Customers WHERE CustomerID = ?

OleDbParameter因此,添加对象的顺序OleDbParameterCollection必须直接对应于命令文本中参数的问号占位符的位置。

于 2013-04-05T14:38:39.780 回答