处理 DBNull.Value
我经常需要处理连接到网格控件的数据表,自定义更新似乎总是产生大量与 DBNull.Value 相关的代码。我在这里看到了类似的问题,但认为必须有更好的答案:
我发现我倾向于将数据库更新封装在方法中,因此我最终得到如下代码,其中我将 DBNull.value 移至可空类型,然后返回更新:
private void UpdateRowEventHandler(object sender, EventArgs e)
{
Boolean? requiresSupport = null;
if (grdMainLevel1.GetFocusedRowCellValue(colASRequiresSupport) != DBNull.Value)
requiresSupport = (bool)grdMainLevel1.GetFocusedRowCellValue(colASRequiresSupport);
AdditionalSupport.UpdateASRecord(year, studentID, requiresSupport)
}
internal static void UpdateASRecord(
string year,
string studentID,
bool? requiresSupport)
{
List<SqlParameter> parameters = new List<SqlParameter>();
parameters.Add(new SqlParameter("@year", SqlDbType.Char, 4) { Value = year });
parameters.Add(new SqlParameter("@student_id", SqlDbType.Char, 11) { Value = studentID });
if (requiresSupport == null)
parameters.Add(new SqlParameter("@requires_support", SqlDbType.Bit) { Value = DBNull.Value });
else
parameters.Add(new SqlParameter("@requires_support", SqlDbType.Bit) { Value = requiresSupport });
//execute sql query here to do update
}
这只是流程的示例,而不是工作代码。我意识到我可以做一些事情,比如传递对象或使用“as type”吞掉潜在的转换问题,使 DBUll 直接为 null,但这对我来说似乎隐藏了潜在的错误,我喜欢具有可为 null 类型的方法的类型安全性。
是否有更干净的方法可以在保持类型安全的同时做到这一点?
I frequently have to deal with DataTables connected to grid controls, custom updating always seems to produce a lot of code related to DBNull.Value. I saw a similar question here but think there must be a better answer:
What is the best way to deal with DBNull's
The thing I find is I tend to encapsulate my database updates in methods so I end up with code like below where I move the DBNull.value to a nullable type and then back for the update:
private void UpdateRowEventHandler(object sender, EventArgs e)
{
Boolean? requiresSupport = null;
if (grdMainLevel1.GetFocusedRowCellValue(colASRequiresSupport) != DBNull.Value)
requiresSupport = (bool)grdMainLevel1.GetFocusedRowCellValue(colASRequiresSupport);
AdditionalSupport.UpdateASRecord(year, studentID, requiresSupport)
}
internal static void UpdateASRecord(
string year,
string studentID,
bool? requiresSupport)
{
List<SqlParameter> parameters = new List<SqlParameter>();
parameters.Add(new SqlParameter("@year", SqlDbType.Char, 4) { Value = year });
parameters.Add(new SqlParameter("@student_id", SqlDbType.Char, 11) { Value = studentID });
if (requiresSupport == null)
parameters.Add(new SqlParameter("@requires_support", SqlDbType.Bit) { Value = DBNull.Value });
else
parameters.Add(new SqlParameter("@requires_support", SqlDbType.Bit) { Value = requiresSupport });
//execute sql query here to do update
}
That was just an example of the flow and not working code. I realize I could do things like pass objects or swallow potential casting problems using "as type" to get DBUll straight to null but both of these to me appear to hide potential errors, I like the type safety of the method with nullable types.
Is there a cleaner method to do this while maintaining type safety?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
几个(非常)简单的通用帮助器方法至少可以将测试集中到一段代码中:
然后可以在适当的地方使用这些方法:
A couple of (very) simple generic helper methods might at least concentrate the test into one piece of code:
These methods can then be used where appropriate:
我不明白
as
转换和null
合并有什么问题。as
-casting 用于读取:null
合并用于写入:这两者都是完全类型安全的,不会“隐藏”错误。
如果你真的愿意,你可以将它们包装到静态方法中,这样你最终会得到这样的阅读:
和写作:
静态方法代码在书写情况下稍微干净一些,但其意图是不太清楚(尤其是在阅读情况下)。
I don't see what's wrong with
as
-casting andnull
coalescing.as
-casting is used for reading:null
coalescing is used for writing:Both of these are completely typesafe and do not "hide" errors.
If you really want, you can wrap these into static methods, so you end up with this for reading:
and this for writing:
The static method code is slightly cleaner in the writing case, but the intent is less clear (especially in the reading case).
相同
这意味着与or
(需要对对象进行额外的转换以消除类型歧义)
which means the same as
or
(additional cast to object is required to remove the type ambiguity)
这是我的 DBNULL 帮助器的实现。用法很简单:
This is my implementation of DBNULL helper. The usage is simple: