是否可以传递一个参数 DB.null 并在数据库中设置一个等于该值的值?

发布于 2024-12-21 00:28:29 字数 423 浏览 5 评论 0原文

我从数据库中提取数据(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我不是你的备胎 2024-12-28 00:28:29

对于 SQL Server,我相信您可以互换使用 nullDBNull.Value 将值设置为 NULL

我还将您的代码更改为:

myCommand.Parameters.AddWithValue(
    "@ProductName",
    // this requires a cast as ?: must return the same type
    String.IsNullOrWhiteSpace(productName)
        ? (object)DBNull.Value
        : (object)productName
);

或者使用 null 并且没有显式强制转换:

myCommand.Parameters.AddWithValue(
    "@ProductName",
    ! String.IsNullOrWhiteSpace(productName) ? productName : null
);

With SQL Server I believe you can interchangably use null or DBNull.Value to set a value to NULL.

I'd also change your code to this:

myCommand.Parameters.AddWithValue(
    "@ProductName",
    // this requires a cast as ?: must return the same type
    String.IsNullOrWhiteSpace(productName)
        ? (object)DBNull.Value
        : (object)productName
);

Or using null and without an explicit cast:

myCommand.Parameters.AddWithValue(
    "@ProductName",
    ! String.IsNullOrWhiteSpace(productName) ? productName : null
);
不一样的天空 2024-12-28 00:28:29

检查空字符串是有效的,然后使用:

myCommand.Parameters.AddWithValue("@ProductName", DBNull.Value)

如果是,例如:

if (string.IsNullOrEmpty(ProductName)) {

    myCommand.Parameters.AddWithValue("@ProductName", DBNull.Value);

} else {

    myCommand.Parameters.AddWithValue("@ProductName", ProductName);

}

It is valid to check for empty string and then use:

myCommand.Parameters.AddWithValue("@ProductName", DBNull.Value)

if it is, like:

if (string.IsNullOrEmpty(ProductName)) {

    myCommand.Parameters.AddWithValue("@ProductName", DBNull.Value);

} else {

    myCommand.Parameters.AddWithValue("@ProductName", ProductName);

}
听风念你 2024-12-28 00:28:29

如果您使用 .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.

榕城若虚 2024-12-28 00:28:29

稍微短一点的版本:

myCommand.Parameters.AddWithValue(
    "@ProductName", 
    productName == null ? DBNull.Value : (object)productName
);

A bit shorter version:

myCommand.Parameters.AddWithValue(
    "@ProductName", 
    productName == null ? DBNull.Value : (object)productName
);
掀纱窥君容 2024-12-28 00:28:29

最好不要将以数据为中心的类型(例如 DBNull)与 TextBox 绑定在一起。虽然它可能会使您的代码在数据方面看起来更干净,但没有内置方法可以自动将空字符串与 DBNull 相互转换。

数据库端的一种干净方法是

myCommand.Parameters.AddWithValue(
    "@ProductName",
    productName.IsNullOrEmpty() ? (object)(DBNull.Value) : productName
);

Best not to tie data-centric types (like DBNull) with your TextBox. 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 from DBNull.

A clean way on your DB side would be

myCommand.Parameters.AddWithValue(
    "@ProductName",
    productName.IsNullOrEmpty() ? (object)(DBNull.Value) : productName
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文