gpt4 book ai didi

c# - 带有空字符串的 ASP.Net C# CreateParameter

转载 作者:行者123 更新时间:2023-11-30 16:30:55 26 4
gpt4 key购买 nike

在我的 ASP.net C# web 项目中,我有一个带有参数的查询命令对象。我使用下面的代码来填充参数:

DbCommand command = conn.CreateCommand();
command.CommandText = query;
DbParameter param = command.CreateParameter();
param.ParameterName = parameter;
param.DbType = DbType.String;
param.Value = value;

此代码适用于除空字符串之外的所有字符串。如果我将输入字段留空,它会将值作为“”传递。如果发生这种情况,我会收到以下异常:

ORA-01400: cannot insert NULL into (string)

有没有一种方法可以让我将空白字符串插入数据库?

我使用 Oracle 数据库并使用 System.Data.OracleClient 作为提供者。

最佳答案

如果要插入空字符串,则必须允许 NULL 值。否则 Oracle 会默默地将空字符串转换为 NULL,您将得到异常。

另一种选择是插入一个带有空格的空字符串 ' ' 但我认为这会很痛苦。

以下是有关 Oracle 为何采用这种(非标准)方式的更多信息: Why does Oracle 9i treat an empty string as NULL?

关于c# - 带有空字符串的 ASP.Net C# CreateParameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5266005/

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