gpt4 book ai didi

c# - 当没有从 ddl 中选择值时插入空值

转载 作者:太空宇宙 更新时间:2023-11-03 14:26:53 24 4
gpt4 key购买 nike

我有 ddl 确定性别,用户不能从 ddl 中选择任何值,所以我试图检查用户是否没有从 ddl 插入空值或数据库中的任何值中选择任何值,但出现错误(过程或函数 ' InsertRegisteration”需要参数“@Gender_Id”,但未提供)。任何人都可以帮助我

(我的代码)

 if (DDLGender.SelectedItem.Value[0]!= null )
{
command.Parameters.Add("@Gender_Id",SqlDbType.Int).Value=null;
}
else
{
command.Parameters.Add(Parameter.NewInt("@Gender_Id", DDLGender.SelectedValue));
}

最佳答案

试试这个:

if (DDLGender.SelectedItem.Value[0]!= null ) 
{
command.Parameters.Add("@Gender_Id",SqlDbType.Int).Value= DBNull.Value;
}
else
{
command.Parameters.Add(Parameter.NewInt("@Gender_Id", DDLGender.SelectedValue));
}

已添加:nullSystem.DbNull.Value

之间的区别

嗯,null 不是任何类型的实例。相反,它是一个无效的引用。

但是,System.DbNull.Value 是对 System.DbNull 实例的有效引用(System.DbNull 是单例和 System.DbNull.Value 为您提供对该类的单个实例的引用)表示数据库中不存在的 * 值。

*我们通常会说 null,但我不想混淆这个问题。

因此,两者在概念上存在很大差异。关键字 null 表示无效引用。 System.DbNull 类表示数据库字段中不存在的值。一般来说,我们应该尽量避免使用相同的东西(在本例中为 null)来表示两个截然不同的概念(在本例中是无效引用与数据库字段中不存在的值)。

请记住,这就是为什么很多人提倡使用 null object pattern通常,这正是 System.DbNull 的示例。

关于c# - 当没有从 ddl 中选择值时插入空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3711004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com