问题描述
在Web API应用程序中查询MS Access数据库时,如果我尝试为变量分配一个空字符串,我会得到一个指定的转换无效异常。 IOW,当这个代码:
In querying an MS Access database in a Web API app, I get a "Specified cast is not valid" exception if I'm trying to assign an empty string to a variable. IOW, when this code:
var accountID = oleDbD8aReader.GetInt16(0);
var platypusName = oleDbD8aReader.GetString(1);
...达到一个记录,其中结果集中的第二列包含空/
...reaches a record where the second column in the result set contains an empty/null string, it bombs.
所以,我想我可以在传球这样的头像:
So, I thought I could head that off at the pass like this:
string platypusName;
var accountID = oleDbD8aReader.GetInt16(0);
if (!string.IsNullOrEmpty(oleDbD8aReader.GetString(1)))
{
platypusName = oleDbD8aReader.GetString(1);
}
else
{
platypusName = string.Empty;
}
...但它不工作 - 我还是得到一个
...but it doesn't work - I still get a "Specified cast is not valid" exception.
如何安全地检查空字符串/空值,并忽略通过结果集的通过以便它将获得后续记录?
How can I safely check for an empty string/null value there and ignore that pass through the result set so that it will get subsequent records?
或者可以通过更改SQL语句以排除结果集中的空/空字符串来排除这种情况?如果是,如何?查询采用以下格式:
Or can I preclude this by changing the SQL statement to exclude empty/null strings from the result set? If so, how? The query is in this format:
SELECT td_duckbill_accounts.platypus_no, t_accounts.name
FROM t_accounts
INNER JOIN td_duckbill_accounts ON t_accounts.account_no = td_duckbill_accounts.account_no
ORDER BY td_duckbill_accounts.platypus_no
推荐答案
我认为查询返回空字符串很容易解决:
I think having the query return empty string is easy solution:
SELECT td_duckbill_accounts.platypus_no,
IIF(ISNULL(t_accounts.name),'',t_accounts.name) AS name
FROM t_accounts
INNER JOIN td_duckbill_accounts ON t_accounts.account_no = td_duckbill_accounts.account_no
ORDER BY td_duckbill_accounts.platypus_no
这应该也可以,但我现在不能测试:
This should also work but I can't test it right now:
SELECT td_duckbill_accounts.platypus_no,
Nz(t_accounts.name,'') AS name
FROM t_accounts
INNER JOIN td_duckbill_accounts ON t_accounts.account_no = td_duckbill_accounts.account_no
ORDER BY td_duckbill_accounts.platypus_no
这篇关于如何抢占“指定的转换无效”例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!