是否可以传递一个参数 DB.null 并在数据库中设置一个等于该值的值?
我从数据库中提取数据(1 条记录)并将每个值放入文本框或下拉列表中。其中一些值是 DB.null。如果是,我将它们每个都转换为“”(空白)。但是,一旦用户修改了文本框/下拉列表,他们就可以“保存”新结果。我的问题是这样的:
如果用户将某些内容留空,我希望能够保持该值为 DB.null。有办法做到这一点吗?
另外,如果文本框中有一个值并且用户决定删除它,我是否也可以将其转换为 DB.Null?为了给大家一些想法,我尝试做以下事情:
productName != ""
? myCommand.Parameters.AddWithValue("@ProductName", productName)
: myCommand.Parameters.AddWithValue("@ProductName", DBNull.Value);
I'm pulling data from a database (1 record) and putting each value in a textbox or dropdownlist. Some of these values are DB.null. If they are, I convert each of them to "" (blank). However, once the user has modified the textboxes/dropdown list, they can "Save" the new results. My problem is this:
If the user leaves something blank, I want to be able to maintain that the value is DB.null. Is there a way to do this?
Also, is it possible that if there is a value in a textbox and the user decides to delete it, I can also convert it to DB.Null? Here's what I tried to do to give you guys some idea:
productName != ""
? myCommand.Parameters.AddWithValue("@ProductName", productName)
: myCommand.Parameters.AddWithValue("@ProductName", DBNull.Value);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于 SQL Server,我相信您可以互换使用
null
或DBNull.Value
将值设置为NULL
。我还将您的代码更改为:
或者使用
null
并且没有显式强制转换:With SQL Server I believe you can interchangably use
null
orDBNull.Value
to set a value toNULL
.I'd also change your code to this:
Or using
null
and without an explicit cast:检查空字符串是有效的,然后使用:
如果是,例如:
It is valid to check for empty string and then use:
if it is, like:
如果您使用 .Net 2.0 或更高版本,请考虑利用 Nullable(Of T ) 结构。它使得测试 Null 值变得非常容易,如果对象指定了一个值,您只需将对象的 .Value 属性放入您的 TextBox 中即可。这些对象默认为 null,因此当您去更新数据库中的记录时,如果未指定值,则无需理会该变量,并且 NULL 会将其放入数据库中。
我承认一开始使用起来有点奇怪(你必须记住在绑定到 TextBox 时使用 .Value 属性),但在最初的障碍之后,如果你要处理的话,结构会非常方便许多数据库字段可能具有可为空值。
If you're working with .Net 2.0 or above, consider leveraging the Nullable(Of T) structure. It makes it really easy to test for Null values and you can simply drop the .Value property of the object into your TextBox if they specified a value. These objects default to null, so when you go to update records in the database, if no value was specifed, you leave the variable alone and NULL will make it into your database.
I'll admit there are a little odd to work with at first (you have to remember to use the .Value property when binding to a TextBox) but after that initial hurdle the structure is quite handy if you're going to be dealing with a lot of database fields that can potentially have nullable values.
稍微短一点的版本:
A bit shorter version:
最好不要将以数据为中心的类型(例如
DBNull
)与TextBox
绑定在一起。虽然它可能会使您的代码在数据方面看起来更干净,但没有内置方法可以自动将空字符串与DBNull
相互转换。数据库端的一种干净方法是
Best not to tie data-centric types (like
DBNull
) with yourTextBox
. While it may make your code look cleaner on the data side there's no built-in way to automatically convert an empty string to and fromDBNull
.A clean way on your DB side would be