将可空值存储到数据库中 [英] store nullable value into database

查看:161
本文介绍了将可空值存储到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了应用程序,其中我想将组合框值项目存储到数据库中。



问题是如果value成员为null则传递0,-1等在数据库中。但是这个领域有一个外键。所以给出了错误。



所以,如何将空值存储到整数字段?





Thx。

解决方案

列定义 [ ^ ]组成外键约束 [ ^ ]。这样你就可以表示通过这个外键没有记录连接到当前记录。



修改:

在存储过程中,为可空参数提供一个默认值NULL,



in你的C#代码,如果没有传递给这些参数的值,根本就不要将它们附加到你的命令。

请阅读整个帖子,因为有很多有用的信息那里。

结束修改



第二次修改

MSDN [ ^ ]写道:



指定参数顺序



如果以@parameter = value的形式提供参数,则可以按任何顺序提供参数。您还可以省略已提供默认值的参数。如果以@parameter = value的形式提供一个参数,则必须以这种方式提供所有后续参数。如果您不以@parameter = value的形式提供参数,则必须按照CREATE PROCEDURE语句中给出的顺序提供它们。



执行存储过程时,服务器拒绝在过程创建期间未包含在参数列表中的任何参数。如果参数名称不匹配,则不接受通过引用传递的任何参数(显式传递参数名称)。



这解释了如何传递参数通过名称和您喜欢的任何顺序指定它们。因此缺少默认值NULL的参数将设置为NULL。

结束第二次修改



此致,



- Manfred


如何将默认值传递给storedprocedure中的日期参数 - Sqlserver 2008年?

http://stackoverflow.com/questions/15540932/how-to-pass-default-value-to-date-parametere-in-storedprocedure-sqlserver-2008 [ ^ ]

http://stackoverflow.com/questions/15540932/how-to-pass-default-value-to-date-parametere-in-storedprocedure-sqlserver-2008]

I created application, in which I want to store combobox value items into database.

Problem is if value member is null then pass 0, -1, etc in database. But this field has a foreign key. So gives error.

so, how can I store null value into integer field?


Thx.

解决方案

Use NULL in the column definition[^] that makes up the foreign key constraint[^]. That way you can express that there are no records connected to the current record via this foreign key.

Modification:

In the stored proc, give the nullable parameters a default value of NULL,

and in your C# code, if no value is being passed to those parameters, simply don''t attach them to your command.

Please read the whole thread as there is a lot of useful information there.
End Modification

Second Modification

MSDN[^] wrote:


Specifying the Order of Parameters


If you supply parameters in the form @parameter = value, you can supply them in any order. You can also omit parameters for which defaults have been supplied. If you supply one parameter in the form @parameter = value, you must supply all subsequent parameters this way. If you do not supply parameters in the form @parameter = value, you must supply them in the order given in the CREATE PROCEDURE statement.

When executing a stored procedure, the server rejects any parameters that were not included with the parameter list during procedure creation. Any parameter passed by reference (explicitly passing the parameter name) is not accepted if the parameter name does not match.


This explains how to pass the parameters by specifying them by name and in any order you like. Missing parameters that have a default value of NULL will thus be set to NULL.
End Second Modification

Regards,

— Manfred


how to pass default value to date parametere in storedprocedure - Sqlserver 2008?
http://stackoverflow.com/questions/15540932/how-to-pass-default-value-to-date-parametere-in-storedprocedure-sqlserver-2008[^]


http://stackoverflow.com/questions/15540932/how-to-pass-default-value-to-date-parametere-in-storedprocedure-sqlserver-2008]


这篇关于将可空值存储到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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