Nulls和Database对象 [英] Nulls and Database objects

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

问题描述

我有人告诉我,我应该为我的桌子设置对象,但是我发现Null问题使得这很困难。


它如果你没有更新表是不是一个大问题,但如果你是 -

如果你有Nullable字段,这是一个真正的问题。你不能总是使用0

或-1来判断一个字段是否为空,而日期是另一个问题。我想b / b
不想在将来使用一些模糊的日期,例如

" EndDate"或DateTerminated或DateTerminated没有结束日期或员工的情况

还没有被终止。


如果使用字段你没有问题在asp.net屏幕上,因为

空字段将被视为空。但是在一个对象中,这是不一样的

,你必须为你的字段定义一个值。


我可以看到使用对象的一些真正优势我的表,但他们似乎无法处理vb.net或c#中的空值而超过



我会很好奇知道其他人如何处理这个问题(不使用0

或1或模糊数据)。


问题不是从表中读取数据,但是当更改为
时你需要把它写回来,你需要知道一个字段是什么时候

应该是空的。


谢谢,


Tom

解决方案



" tshad" < TS ********** @ ftsolutions.com>在留言中写道

news:ea ************** @ TK2MSFTNGP10.phx.gbl ...

你如果你在asp.net屏幕上使用一个字段没有问题,因为
空字段将被视为null。但是在一个对象中,这是不一样的,你必须为你的字段定义一个值。


你这样做吗?

我可以看到在我的桌子上使用对象有一些真正的好处,但他们看起来因为无法获得b $ b处理vb.net或
c#中的空值。
我很想知道其他人如何处理这个问题(不使用
0或1或模糊数据)。问题不是从表中读取数据,但是当进行更改并且需要将其写回时,您需要知道何时字段应该为空。




我认为你使用的是字段的简单数据类型。在你的对象上。

如果那是你真正想做的事情,你可以将每个私有字段定义为

" object"然后把它放在getter / setter中。我不知道对于拳击而言,

的表现惩罚有多大。所有这些价值观,它肯定会取决于你的申请表上的
。我们使用对象来保存有关我们的

字段的各种信息。我们发现能够比当前值保存更多信息更有利 - 例如旧等。值(从

数据库加载),是否加载字段(某些字段是延迟加载的),

以及字段是否为空等等。


所以,我们的代码看起来像这样(方法名称显示我的Delphi背景):


myObject.FirstName.AsString =" Scott" ;;

myObject.LastName.AsString =" Roberts";

myObject.Salary.AsFloat = 250000.00; //哈哈!


我也可以这样做:


myObject.Salary.AsString = SalaryEdit.Text; //转换由

" AsString"处理"


"等

myObject.Salary.IsNull = true;


只是一些想法。不要以为你只限于:


private decimal _salary;

公共十进制薪水

{

get {return _salary;}

set {_salary = value;}

}


< blockquote>" Scott Roberts" < SC *********** @ no-spam.intelebill.com>在消息中写道

新闻:ea ************** @ TK2MSFTNGP14.phx.gbl ...


" ; tshad" < TS ********** @ ftsolutions.com>在消息中写道
新闻:ea ************** @ TK2MSFTNGP10.phx.gbl ...

你不要如果你在asp.net屏幕上使用一个字段有一个问题,因为
空字段将被视为null。但是在
对象中你不一定要为你的字段定义一个值。
你做到了吗?




实际上,我所做的是检查来自数据库的null或检查

如果进入数据库,看看我的字段(文本框或标签)是否为空白。


对于非字符串(int,十进制,金钱,日期)这是我必须处理的方式

它。


如果数据即将来临从数据库是Null,我将该字段留空。然后

当我把数据写回来并且字段为空时,我发送DBNull.Value就像

所以:


如果salaryDesired.text =""

.Add(" @ salaryDesired",SqlDbType.Money).value = DBNull.Value

else

.Add(" @ salaryDesired",SqlDbType.Money).value =

Regex.Replace(salaryDesired.text," \


I have people telling me that I should set up objects for my tables, but I
am finding the Null problem makes that difficult.

It isn''t a big problem if you are not updating the table, but if you are -
it is a real problem if you have Nullable fields. You can''t always use a 0
or -1 to tell whether a field is null and dates are another problem. I
don''t want to use some obscure date in the future for something like
"EndDate" or "DateTerminated" where there is no ending date or an employee
hasn''t been terminated yet.

You don''t have a problem if you use a field on an asp.net screen, as an
empty field would be taken as null. But that is not the same in an object
where you have to define a value for your fields.

I can see some real advantages to using objects for my tables, but they seem
to be outweighed by being unable to handle the nulls in either vb.net or c#.

I''d be curious to know how others are handling this problem (without using 0
or 1 or obscure data).

The problem is not reading the data from the table, but when changes are
made and you need to write it back, you need to know when a field is
supposed to be Null.

Thanks,

Tom

解决方案


"tshad" <ts**********@ftsolutions.com> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl...

You don''t have a problem if you use a field on an asp.net screen, as an
empty field would be taken as null. But that is not the same in an object
where you have to define a value for your fields.
You do?
I can see some real advantages to using objects for my tables, but they seem to be outweighed by being unable to handle the nulls in either vb.net or c#.
I''d be curious to know how others are handling this problem (without using 0 or 1 or obscure data).

The problem is not reading the data from the table, but when changes are
made and you need to write it back, you need to know when a field is
supposed to be Null.



I take it you are using simple data types for the "fields" on your objects.
If that''s what you really want to do, you can define each private field as
"object" then cast it in the getter/setter. I don''t know how big the
performance penalty is for "boxing" all those values, it''ll certainly depend
on your application. We use objects to hold various information about our
"fields". We find it advantagous to be able to hold more information than
just the current value - for example the "old" value (as loaded from the
database), whether the field is loaded or not (some fields are lazy loaded),
and whether the field is null, among other things.

So, our code looks like this (method names reveal my Delphi background):

myObject.FirstName.AsString = "Scott";
myObject.LastName.AsString = "Roberts";
myObject.Salary.AsFloat = 250000.00; // haha!

I could also do:

myObject.Salary.AsString = SalaryEdit.Text; // conversion handled by
"AsString", handles "


", etc.
myObject.Salary.IsNull = true;

Just some ideas. Don''t think that you''re limited to:

private decimal _salary;
public decimal Salary
{
get {return _salary;}
set {_salary = value;}
}


"Scott Roberts" <sc***********@no-spam.intelebill.com> wrote in message
news:ea**************@TK2MSFTNGP14.phx.gbl...


"tshad" <ts**********@ftsolutions.com> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl...

You don''t have a problem if you use a field on an asp.net screen, as an
empty field would be taken as null. But that is not the same in an
object
where you have to define a value for your fields.
You do?



Actually, what I do is check for null coming from the database or check to
see if my field (textbox or label) is blank if going to the database.

For non-strings (int, decimal, money, dates) this is how I have to handle
it.

If the data coming from the database is Null, I leave the field blank. Then
when I write the data back and the field is blank, I send DBNull.Value like
so:

if salaryDesired.text = ""
.Add("@salaryDesired",SqlDbType.Money).value = DBNull.Value
else
.Add("@salaryDesired",SqlDbType.Money).value =
Regex.Replace(salaryDesired.text,"\


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

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