关于数据库NULL的艰难问题。 [英] Tough question on database NULLs.

查看:78
本文介绍了关于数据库NULL的艰难问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个textBox控件。它是空的,您将

this.textBox1.Text.ToString()值发送到数据库中。控件确实

不发送NULL而是发送&;


什么是_CORRECT_方式(例如MS的C#语言设计者的方式)

会这样做来处理这种情况。我知道你可以测试

a""然后有代码插入这样的NULL值:


if(this.textBox1.Text.ToString()=='''')

{

//伪代码!我知道你应该使用参数!

插入tableFOO(NULL);


}否则

{

插入tableFOO(this.textBox1.Text.ToString();


}

Suppose you have a textBox control. It is empty and you send the
this.textBox1.Text.ToString() value into a database. The control does
not send a NULL but instead a "".

What is the _CORRECT_ way (e.g. the way the C# language designers at MS
would do it) to handle this situation. I know you could just test for
a "" and then have code that would insert a NULL value like this:

if (this.textBox1.Text.ToString() == '''')
{
//pseudo code ! I know you should use parameters!
insert into tableFOO(NULL);

}else
{
insert into tableFOO(this.textBox1.Text.ToString();

}

推荐答案

Hello Karen,


你在这段代码中不喜欢什么?


PS:use == String。清空==''''


KHS假设你有一个textBox控件。它是空的,你发送了

KHthis.textBox1.Text。将ToString()值转换为数据库。控件

KH不会发送NULL而是发送"

KH>

KHWhat是_CORRECT_方式(例如,#b $ b KHMS的C#语言设计师会这样做)来处理这种情况。我知道你可以只用

KHtest来表示" ;然后有代码插入一个NULL值

KH喜欢这个:

KH>

KHif(this.textBox1.Text.ToString( )=='''')

KH {

KH //伪代码!我知道你应该使用参数!

KHinsert到tableFOO(NULL);

KH}否则

KH {

KHinsert到tableFOO(this.textBox1.Text.ToString();

KH}

KH>

---

WBR,

Michael Nemtsev ::博客: http: //spaces.msn.com/laflour


有时一个人仍然忠实于一个事业只因为它的对手没有

停止(c)Friedrich Nietzsche
Hello Karen,

What you don''t like in this code?

PS: use == String.Empty in lue of == ''''

KHSuppose you have a textBox control. It is empty and you send the
KHthis.textBox1.Text.ToString() value into a database. The control
KHdoes not send a NULL but instead a "".
KH>
KHWhat is the _CORRECT_ way (e.g. the way the C# language designers at
KHMS would do it) to handle this situation. I know you could just
KHtest for a "" and then have code that would insert a NULL value
KHlike this:
KH>
KHif (this.textBox1.Text.ToString() == '''')
KH{
KH//pseudo code ! I know you should use parameters!
KHinsert into tableFOO(NULL);
KH}else
KH{
KHinsert into tableFOO(this.textBox1.Text.ToString();
KH}
KH>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche




Michael Nemtsev写道:

Michael Nemtsev wrote:

你好凯伦,


这段代码你不喜欢什么?


PS:use == String.Empty在l = =''''

Hello Karen,

What you don''t like in this code?

PS: use == String.Empty in lue of == ''''



我认为如果没有为textBox.Text分配值,它应该是

为NULL!


所以理论上这两个应该相等_if_ textBox.Text ha未被

赋值!


插入tableFOO(this.textBox.Text.ToString());


应该等于:


插入tableFOO(null);

I think that if textBox.Text has not been assigned a value, it should
be NULL!

So in theory these two should be equal _if_ textBox.Text has not been
assigned a value!

insert into tableFOO ( this.textBox.Text.ToString());

should be equal to :

insert into tableFOO ( null );




" Karen Hill" < ka ********** @ yahoo.comwrote in message

news:11 ******************** **@75g2000cwc.googlegro ups.com ...

"Karen Hill" <ka**********@yahoo.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...

假设您有一个textBox控件。它是空的,您将

this.textBox1.Text.ToString()值发送到数据库中。控件确实

不发送NULL而是发送&;


什么是_CORRECT_方式(例如MS的C#语言设计者的方式)

会这样做来处理这种情况。我知道你可以测试

a""然后有代码插入这样的NULL值:


if(this.textBox1.Text.ToString()=='''')

{

//伪代码!我知道你应该使用参数!

插入tableFOO(NULL);


}否则

{

插入tableFOO(this.textBox1.Text.ToString();


}
Suppose you have a textBox control. It is empty and you send the
this.textBox1.Text.ToString() value into a database. The control does
not send a NULL but instead a "".

What is the _CORRECT_ way (e.g. the way the C# language designers at MS
would do it) to handle this situation. I know you could just test for
a "" and then have code that would insert a NULL value like this:

if (this.textBox1.Text.ToString() == '''')
{
//pseudo code ! I know you should use parameters!
insert into tableFOO(NULL);

}else
{
insert into tableFOO(this.textBox1.Text.ToString();

}



好​​吧,我们在哪里工作,我们在实用程序类上有方法执行

检查...

public static void TrimColumn(DataRow Row,DataColumn Column)

{

if(!Row.IsNull(Column)&& Row [Column] .ToString()== string.Empty&&

Column.AllowDBNull){

行[Column] = DBNull.Value;

}

}


或类似的东西...然后我们可以使用我们的类型化数据集来修剪我们的ui层中的






Row.LastName = txtLastName.Text;


在我们的业务逻辑层:


TrimColumn (Row,Row.TypedTable.LastNameColumn);


注意,我们使用生成TypedTa的自定义数据集生成器ble

属性以及键入的列。你需要修改上面的代码

(这不是我们的代码,直接输入我的头脑)到

套装...比如通过专栏名称而不是实际列。


HTH,

Mythran

Well, where we work, we have methods on a utility class that perform the
checks...

public static void TrimColumn(DataRow Row, DataColumn Column)
{
if (!Row.IsNull(Column) && Row[Column].ToString() == string.Empty &&
Column.AllowDBNull) {
Row[Column] = DBNull.Value;
}
}

or something to that effect...then we can use our typed datasets to trim our
columns:

in our ui layer:

Row.LastName = txtLastName.Text;

in our business logic layer:

TrimColumn(Row, Row.TypedTable.LastNameColumn);

Note, we use a custom data set generator that generates a TypedTable
property as well as typed columns. You would need to modify the above code
(which is not our code directly, just typed off top of my head) to
suit...such as passing the column name instead of the actual column.

HTH,
Mythran


这篇关于关于数据库NULL的艰难问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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