插入NULL [英] insert NULL

查看:86
本文介绍了插入NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在更新记录的SQL查询,如果屏幕上的字段为空,如何在数据库中的字段中添加NULL?


示例:

我有5个文本框,用户可以留空,从文本中删除数据

框然后点击更新,我该怎么办将NULL插入到

数据库的字段中,而不是在数据库中有空白记录

I have a sql query that is doing an update of records, how can I add NULL to
the field in the database if the field on my screen is blank?

example:
I have 5 textboxes, and a user can leave some blank, delete data from a text
box then hit update, how can I have NULL inserted into the field on the
database instead of having a blank record in the db

推荐答案

NuB,


最简单的方法就是不要包含那个参数,除非它有数据




所以如果你在insert语句中添加参数(使用sql命令

object),它将如下所示:


SqlCommand1.Parameters.Add(" @ MiddleName",SqlDbType.NvarChar,50).Value

= MiddleNameTextBox.Text.Trim

结束如果


如果sql数据库中的字段可以为空,那么不设置参数

将使其为空。


-

此致,


S. Justin Gengo,MCP

Web开发人员/程序员
www.aboutfortunate.com


混乱出现顺序。

Nietzsche

" NuB" < Nu*@discussions.microsoft.com>在消息中写道

news:95 ********************************** @ microsof t.com ...
NuB,

The simplest way is to not include that parameter at all unless it has data
in it.

So if you''re adding parameters to your insert statement (using a sql command
object) it would look like this:

If MiddleNameTextBox.Text.Trim.Length > 0 Then
SqlCommand1.Parameters.Add("@MiddleName", SqlDbType.NvarChar, 50).Value
= MiddleNameTextBox.Text.Trim
End If

If a field in the sql database is nullable then not setting the parameter
will leave it null.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"NuB" <Nu*@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
我有一个sql查询,正在对记录进行更新,如果字段在上,如何在数据库中的字段中添加NULL
我的屏幕是空白的?

例如:
我有5个文本框,用户可以留空,从
文本框中删除数据然后点击更新,如何将NULL插入到
数据库的字段中,而不是在db中有空白记录
I have a sql query that is doing an update of records, how can I add NULL
to
the field in the database if the field on my screen is blank?

example:
I have 5 textboxes, and a user can leave some blank, delete data from a
text
box then hit update, how can I have NULL inserted into the field on the
database instead of having a blank record in the db



cmd.Parameters.Add( " @ @ Blah",SqlDbType.String,128).Value =(firstName ==

null || firstName.Length == 0)? DBNull.Value:firstName;

你可以在2.0中做得更清洁

cmd.Parameters.Add(....)。Value =(string。 IsNullOrEmpty(firstName)??

DBNull.Value;


karl


-

我的ASP.Net教程
http://www.openmymind.net/

" NuB" Nu *@discussions.microsoft.com>写在留言中

news:95 ********** ************************ @ microsof t.com ...
cmd.Parameters.Add("@Blah", SqlDbType.String, 128).Value = (firstName ==
null || firstName.Length == 0) ? DBNull.Value : firstName;
You can do it cleaner in 2.0

cmd.Parameters.Add(....).Value = (string.IsNullOrEmpty(firstName) ??
DBNull.Value;

karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"NuB" <Nu*@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
我有一个正在进行更新的SQL查询记录,如果我的屏幕上的字段是空白的,如何在数据库中的字段中添加NULL

例如:
我有5个文本框,并且用户可以留下一些空白,从
文本框中删除数据然后点击更新,如何在
数据库的字段中插入NULL而不是在空白记录中db
I have a sql query that is doing an update of records, how can I add NULL
to
the field in the database if the field on my screen is blank?

example:
I have 5 textboxes, and a user can leave some blank, delete data from a
text
box then hit update, how can I have NULL inserted into the field on the
database instead of having a blank record in the db



如果是我们的话在VB中,你只需要if / else


dim databaseFirstName作为对象

如果firstName为nothing OrElse firstName.Length = 0则

databaseFirstName = DBNull.Value

否则如果

databaseFirstName = firstName

结束如果


Karl


-

我的ASP.Net教程
http://www.openmymind.net/

" NuB" < Nu*@discussions.microsoft.com>在消息中写道

news:95 ********************************** @ microsof t.com ...
And if ur using VB, you''ll simply need to if/else

dim databaseFirstName as object
if firstName is nothing OrElse firstName.Length = 0 then
databaseFirstName = DBNull.Value
else if
databaseFirstName = firstName
end if

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"NuB" <Nu*@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
我有一个sql查询,正在对记录进行更新,如果字段在上,如何在数据库中的字段中添加NULL
我的屏幕是空白的?

例如:
我有5个文本框,用户可以留空,从
文本框中删除数据然后点击更新,如何在
数据库的字段中插入NULL而不是在db中有空白记录
I have a sql query that is doing an update of records, how can I add NULL
to
the field in the database if the field on my screen is blank?

example:
I have 5 textboxes, and a user can leave some blank, delete data from a
text
box then hit update, how can I have NULL inserted into the field on the
database instead of having a blank record in the db



这篇关于插入NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆