ExecuteReader时“参数?_1没有默认值”错误

时间:2022-09-27 10:41:34

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

4 个解决方案

#1


4  

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

令人遗憾的是,前两个*目前在谷歌搜索涉及的搜索结果

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:

这个错误的解释(正常情况下遇到)由Marc Gravell提供:

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

不传递.Value为null的参数。完全没有。

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

您需要传递DBNull.Value来传递语义null。例如:

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

#2


0  

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()

#3


0  

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.

感谢大家的投入。

#4


-1  

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

#1


4  

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

令人遗憾的是,前两个*目前在谷歌搜索涉及的搜索结果

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:

这个错误的解释(正常情况下遇到)由Marc Gravell提供:

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

不传递.Value为null的参数。完全没有。

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

您需要传递DBNull.Value来传递语义null。例如:

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

#2


0  

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()

#3


0  

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.

感谢大家的投入。

#4


-1  

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