vb.net ExecuteReader 时出现“参数 ?_1 没有默认值”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21263591/
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 16:31:21  来源:igfitidea点击:

"Parameter ?_1 has no default value" error when ExecuteReader

.netvb.netoledboledbcommandoledbexception

提问by wingyip

Having problem with the code below in a web service. Have searched for a solution but nothing that I have seen seems different to what I am doing below.

以下代码在 Web 服务中出现问题。已经搜索了解决方案,但我所看到的似乎与我在下面所做的没有什么不同。

NB: The string variable 'AccountNo' is a passed into a function which includes the code below.

注意:字符串变量“AccountNo”被传递到一个包含以下代码的函数中。

The error is generated on the last line of code - ExecuteReader.

错误是在最后一行代码 - ExecuteReader 上生成的。

    Dim sConnString As String
    Dim rdr As OleDbDataReader
    Dim orderPaid As Decimal
    Dim fbeused As Decimal

    sConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\orders.mdb'"

    Dim conn As New OleDbConnection(sConnString)

    Dim sb As New StringBuilder
    sb.Append("SELECT DISTINCTROW OrderHeaders.Accountno, Sum(([paidqty]*[unitprice])*[orderheaders].[entpercent]/100) AS orderpaid, Sum([freeqty]*[unitprice]) AS fbeused")
    sb.Append(" FROM OrderHeaders INNER JOIN OrderDetails ON OrderHeaders.[OrderNo] = OrderDetails.[OrderNo]")
    sb.Append(" GROUP BY OrderHeaders.Accountno HAVING OrderHeaders.Accountno=?")
    Dim sqlString As String = sb.ToString

    Dim cmd As New OleDbCommand(sqlString, conn)
    cmd.CommandType = CommandType.Text
    'cmd.Parameters.AddWithValue("AccNo", AccountNo)
    cmd.Parameters.Add("AccNo", OleDbType.VarWChar).Value = AccountNo
    conn.Open()

    rdr = cmd.ExecuteReader()

The error I get is (as mentioned above)

我得到的错误是(如上所述)

Parameter ?_1 has no default value

采纳答案by wingyip

The question in fact had a wrong assumption and that was that there was an error in the code.

这个问题实际上有一个错误的假设,那就是代码中有错误。

The syntax of the SQL query was correct and the parameter was being inserted correctly. However the test data contained errors and therefore no result was being returned by a correctly formatted query.

SQL 查询的语法正确,参数插入正确。然而,测试数据包含错误,因此格式正确的查询没有返回任何结果。

Thanks all for input.

感谢大家的投入。

回答by hawbsl

It's a shame that the top two StackOverflow results currently in Google for searches involving

遗憾的是,目前在 Google 中搜索涉及的前两个 StackOverflow 结果

Parameter ?_ has no default value

both have the questioner coming back in and saying there were flaws in their original question or their test data or whatever (though it's great that questioners check back in).

两者都让提问者回来并说他们的原始问题或测试数据或其他任何东西存在缺陷(尽管提问者回来检查很好)。

The explanation for this error (as encountered in normal situations) is supplied by Marc Gravell here:

此错误的解释(如在正常情况下遇到的)由马克Gravell提供在这里

Parameters with a .Value of null are not passed. At all.

You need to pass DBNull.Value instead to pass a semantic null. For example:

com.Parameters.Add("@p7", OleDbType.Char, 255).Value =
         ((object)values7[0]) ?? DBNull.Value; (etc)

不传递 .Value 为 null 的参数。在所有。

您需要传递 DBNull.Value 而不是传递语义空值。例如:

com.Parameters.Add("@p7", OleDbType.Char, 255).Value =
         ((object)values7[0]) ?? DBNull.Value; (etc)

回答by DRapp

What you have looks correct with the "?" as the parameter place-holder for the parameters added to the command. You currently have the parameter identified as OleDbType.VarWChar. Is that intended? Are you dealing with unicode data? I suspect not in this case. Try changing to OleDbType.Char which also is represented to handle System.String values.

你有什么看起来正确的“?” 作为添加到命令的参数的参数占位符。您当前已将参数标识为 OleDbType.VarWChar。这是故意的吗?你在处理 unicode 数据吗?我怀疑在这种情况下不是。尝试更改为 OleDbType.Char,它也表示处理 System.String 值。

You could also make sure you are getting a string by using

您还可以确保使用以下方法获取字符串

AccountNo.ToString()

AccountNo.ToString()

回答by Dubas

Duplicated of: OleDbCommand parameters order and priority

重复: OleDbCommand 参数顺序和优先级

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example:

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

// Create SQL with ? for each parameter
String sql = "SELECT Address FROM Adresses WHERE Country = ? AND City = ?";
OleDbCommand command = new OleDbCommand(sql , connection);

// Add to command each paramater VALUE by position. 
// One parameter value for ?
command.Parameters.Add("My City") ;
command.Parameters.Add("My Country") ;

http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters.aspx

http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters.aspx