自动转换。net null到DBNull.Value?

时间:2021-12-04 13:11:53

I've written a utility to handle adding values to data parameters for hand-rolling sql queries. Consumption looks like

我编写了一个实用程序,用于处理为手工滚动sql查询向数据参数添加值。消费的样子

utility.Add("SELECT * FROM MyTable WHERE MyColumn = {0}", myVariable, DBType.TheType);

or

utility.Add("UPDATE MyTable SET MyColumn = {0}", myVariable, DBType.TheType);

How should a null value for myVariable be handled?

如何处理myVariable的空值?

  1. All values should be fully trusted. The responsibility is solely upon the consumer. How would the consumer know that null would be handled anyway?
  2. 所有的值都应该完全可信。责任完全在于消费者。消费者怎么知道null会被处理?
  3. null can never work, so it should throw a NullArgumentException. Why go any further?
  4. null不能工作,所以它应该抛出一个NullArgumentException。为什么再进一步呢?
  5. null should be automatically interpreted as DBNull.Value since it's the only viable solution. This is a utility, right? Make it utilizable and dry up some code!
  6. null应该被自动解释为DBNull。因为它是唯一可行的解决方案。这是一个效用,对吧?使它可使用和干燥一些代码!

Optional bonus question: If these arguments were made by three political candidates, what would their parties be? (Please state the home country of such parties)

可选附加问题:如果这些争论是由三位政治候选人提出的,他们的政党会是什么?(请说明这些政党的原籍国)

2 个解决方案

#1


1  

Rock the Null Coalescing Op:

Rock the Null合并Op:

In your add put another parm for the =... utility.Add("SELECT * FROM MyTable WHERE MyColumn {0}{1}", myVariable, DBType.TheType);

在您的add put另一个parm为=…实用程序。添加(“从MyTable中选择*,其中MyColumn{0}{1}”、myVariable、DBType.TheType);

When you format the string in the Add method: ... = String.Format(sql,myVariable is null ? " IS " : " = ", myVariable ?? "Null"

当您将字符串格式化为Add method:…=字符串。格式(sql,myVariable是零?“IS”:“=”,myVariable ??“零”

You'll probably want to expound on that a bit, e.g., stick with on parameter and toss in a temp stringbuilder; might have to use a the ?/: construct for the type, too, wherever that's being used. Good luck.

您可能想要详细说明一下,例如,在参数上粘贴,并在temp stringbuilder中抛出;可能也必须为类型使用?/:构造,无论使用的是什么类型。祝你好运。

#2


0  

Null is a valid query parameter, you don't need to convert it to DBNull to use it in a query.

Null是一个有效的查询参数,您不需要将它转换为DBNull就可以在查询中使用它。

#1


1  

Rock the Null Coalescing Op:

Rock the Null合并Op:

In your add put another parm for the =... utility.Add("SELECT * FROM MyTable WHERE MyColumn {0}{1}", myVariable, DBType.TheType);

在您的add put另一个parm为=…实用程序。添加(“从MyTable中选择*,其中MyColumn{0}{1}”、myVariable、DBType.TheType);

When you format the string in the Add method: ... = String.Format(sql,myVariable is null ? " IS " : " = ", myVariable ?? "Null"

当您将字符串格式化为Add method:…=字符串。格式(sql,myVariable是零?“IS”:“=”,myVariable ??“零”

You'll probably want to expound on that a bit, e.g., stick with on parameter and toss in a temp stringbuilder; might have to use a the ?/: construct for the type, too, wherever that's being used. Good luck.

您可能想要详细说明一下,例如,在参数上粘贴,并在temp stringbuilder中抛出;可能也必须为类型使用?/:构造,无论使用的是什么类型。祝你好运。

#2


0  

Null is a valid query parameter, you don't need to convert it to DBNull to use it in a query.

Null是一个有效的查询参数,您不需要将它转换为DBNull就可以在查询中使用它。