当 SQL Select 不返回任何内容时如何避免抛出错误
我在某处读到,永远不应该将错误条件用作正常的程序流程。 对我来说非常有意义...但是
C# 应用程序位于 MySQL 数据库之上。 我需要将字符串值解析为两部分:ID 和值。 (原始数据来自泥盆纪数据库),然后根据查找表验证该值。 因此,几个原始字符串可能如下所示:
“6776 Purple People Eater”
“BIK Yellow Polka-Dot Bikini(当前正在使用)”
“DCP Deuce Coup”
因此,我的小实用程序将每个字符串解析为 ID 和描述基于第一个空间的索引(幸运的是,一致)。 然后,我将 ID 传递给查找,获取新值,然后我们就可以开始了。
不幸的是,TPTB 还决定我们不再需要臭黄色圆点比基尼(目前正在使用)。 因此,BIK 不返回行。 这是一个代码片段:
foreach (string product in productTokens) {
tempProduct = product.Trim();
if (tempProduct.Length > 0) {
if (tempProduct.Length < 10) {
product_id = tempProduct;
}
else {
int charPosition = tempProduct.IndexOf(" ");
product_id = tempProduct.Substring(0, charPosition);
}
try {
s_product = productAdapter.GetProductName(product_id).ToString();
}
catch (Exception e) {
if (e.Message.ToString() == "Object reference not set to an instance of an object.") {
s_product = "";
}
else {
errLog.WriteLine("Invalid product ID " + e.Message.ToString());
Console.WriteLine("Invalid product ID " + e.Message.ToString());
throw;
} //else
} //catch
if (s_product.Length > 0) {
sTemp = sTemp + s_product + "; ";
}
} //if product.length > 0
} //foreach product in productTokens
真的,真的很难看! 特别是我在 catch 块中测试无效 ID 的部分。 一定有更好的方法来处理这个问题。
如果有人能帮助我,我将非常感激。
谢谢。
I read somewhere that one should never use error conditions as normal program flow. Makes excellent sense to me... But
C# application sitting on top of a MySQL db. I need to parse a string value into two parts, an ID, and a value. (The original data come from a Devonian database), then validate the value against a lookup table. So, a couple of original strings might look like this:
"6776 Purple People Eater"
"BIK Yellow Polka-Dot Bikini (currently in use)"
"DCP Deuce Coup"
So, my little utility parses each string into the ID and the description based on the index of the first space (fortunately, consistent). I then pass the ID to the lookup, get the new value and away we go.
Unfortunately, TPTB also decided that we no longer need no stinkin' Yellow Polka-Dot Bikinis (currently in use). So, BIK does not return a row. Here's a code snippet:
foreach (string product in productTokens) {
tempProduct = product.Trim();
if (tempProduct.Length > 0) {
if (tempProduct.Length < 10) {
product_id = tempProduct;
}
else {
int charPosition = tempProduct.IndexOf(" ");
product_id = tempProduct.Substring(0, charPosition);
}
try {
s_product = productAdapter.GetProductName(product_id).ToString();
}
catch (Exception e) {
if (e.Message.ToString() == "Object reference not set to an instance of an object.") {
s_product = "";
}
else {
errLog.WriteLine("Invalid product ID " + e.Message.ToString());
Console.WriteLine("Invalid product ID " + e.Message.ToString());
throw;
} //else
} //catch
if (s_product.Length > 0) {
sTemp = sTemp + s_product + "; ";
}
} //if product.length > 0
} //foreach product in productTokens
Really, really ugly! Particularly the part where I test for an invalid IDin the catch block. There simply must be a better way to handle this.
If anyone can help me out, I'd really appreciate it.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此时您不应该调用 ToString(),首先您应该检查返回的值是否为 null;
You shouldn't be calling ToString() at that point, first you should check if the value returned is null;
除了 Rob 和 Marc 的建议之外,根据上面的代码,我建议进行另一个调整; 如果
productAdapter.GetProductName()
没有返回String
,在这种情况下调用 ToString() 的话,我会感到非常惊讶完全是多余的。 如果它确实已经返回了一个String
,那么你的整个try/catch块就变成了一行:另外,我认为提到已经返回一个的其他内容可能会很有用。
字符串
-Exception.Message
。 因此,您在代码中调用 ToString() 的所有不同位置也是完全多余的。另外,我建议使用实例方法
String.Split()
而不是IndexOf
和SubString
的组合:最后,决定使用哪种方法通过检查
Message
属性发现的Exception
应该是最后的方案。 即使您确实需要在这里捕获NullReferenceException
,您也应该明确地这样做:PS:我也不太确定这个问题与 MySQL 有什么关系,因为在您的代码中没有任何 DB API 的证据。
In addition to the recommendations of Rob and Marc, and based on your code above, I'd suggest another tweak; I'd be really surprised if
productAdapter.GetProductName()
doesn't already return aString
, in which case calling ToString() on it is utterly redundant. If it does indeed already return aString
, then your whole try/catch block becomes one line:Also, I think it might be useful to mention something else that already returns a
String
-Exception.Message
. So all of the different places where you're calling ToString() in your code are also utterly redundant.Also, I would suggest using the instance method
String.Split()
rather than the combination ofIndexOf
andSubString
:Finally, deciding what kind of
Exception
you've caught by examining theMessage
property should be a totally last-ditch scenario. Even if you really did need to catchNullReferenceException
here, you should do so explicitly:P.S.: I'm also not really sure what at all this question has to do with MySQL, since there's no evidence of any DB API in your code.
不能简单地检查 GetProductName 是否返回 null 吗?
Can't you simply check whether GetProductName returns null?