I'm trying to make this SP run:
我正在尝试使这个SP运行:
myDataContext.InsertEvent(codEvent, dateOfEvent, statusOfEvent);
codEvent is an int; dateOfEvent is a DateTime; statusOfEvent is a char.
codEvent是一个int; dateOfEvent是一个DateTime; statusOfEvent是一个char。
The problem is Visual Studio is throwing an error if I pass a statusOfEvent null. And, my SP accpts nullable char just like my table.
问题是如果我传递statusOfEvent null,Visual Studio会抛出错误。并且,我的SP像我的桌子一样接受可以为空的char。
I've tried to do this too:
我也试过这样做:
char? statusOfEvent= null;
if (!String.IsNullOrEmpty(f["statusOfEvent"]))
statusOfEvent= Convert.ToChar(f["statusOfEvent"]);
But it also throws an error...
但它也会引发错误......
thanks!!
I've tried to do this ternary expression:
我试过这个三元表达式:
!String.IsNullOrEmpty(f["statusOfEvent"]) ? Convert.ToChar(f["statusOfEvent"]) : DBNull.Value
But it is says that 'there is no implicit conversion between char and DBNull'
但据说“char和DBNull之间没有隐式转换”
And I also looked at '*.dbml' designer.cs file and my SP method has this line:
我还查看了'* .dbml'designer.cs文件,我的SP方法有这一行:
[Parameter(DbType="Char(1)")] System.Nullable<char> statusOfEvent
2 个解决方案
#1
I believe you need DBNull.Value rather than null
.
我相信你需要DBNull.Value而不是null。
Update
To use, you would pass it where you might other wise pass null
. So, assuming that statusOfEvent
is nullable, you could do the following.
更新要使用,您可以将其传递给其他可能传递null的位置。因此,假设statusOfEvent可以为空,您可以执行以下操作。
myDataContext.InsertEvent(
codEvent,
dateOfEvent,
statusOfEvent ?? DBNull.Value);
However, InsertEvent
would need to accept it as a valid input for this parameter. If you're using some .NET support like an XSD dataset to describe the stored procedure. You can modify this to change the parameter to accept char?
from char
. Then you should be able to pass null
and the underlying system will convert this to DBNull.Value
for you. In that scenario, you can leave the call as:
但是,InsertEvent需要接受它作为此参数的有效输入。如果您正在使用某些.NET支持(如XSD数据集)来描述存储过程。你可以修改它来改变参数以接受char吗?来自char。然后你应该能够传递null,底层系统会为你转换为DBNull.Value。在这种情况下,您可以将呼叫保留为:
myDataContext.InsertEvent(
codEvent,
dateOfEvent,
statusOfEvent);
This should also work with Linq2Sql. To change the stored procedure's representation in your Linq2Sql objects, you need to open your dbml and edit the properties for the stored procedure.
这也适用于Linq2Sql。要更改Linq2Sql对象中存储过程的表示形式,需要打开dbml并编辑存储过程的属性。
#2
DBNull.Value is what that database expects...
DBNull.Value是数据库期望的......
Here is how I am passing IsActive (which is a nullable boolean) as a parameter...
这是我如何传递IsActive(这是一个可以为空的布尔值)作为参数...
_ExecutedSP = new StoredProcedure("EvaluationType_GetList", base._SPInputOutput);
if (this.IsActive == null)
{
_ExecutedSP.AddParameter("@IsActive", SqlDbType.Bit, DBNull.Value);
}
else
{
_ExecutedSP.AddParameter("@IsActive", SqlDbType.Bit, this.IsActive);
}
#1
I believe you need DBNull.Value rather than null
.
我相信你需要DBNull.Value而不是null。
Update
To use, you would pass it where you might other wise pass null
. So, assuming that statusOfEvent
is nullable, you could do the following.
更新要使用,您可以将其传递给其他可能传递null的位置。因此,假设statusOfEvent可以为空,您可以执行以下操作。
myDataContext.InsertEvent(
codEvent,
dateOfEvent,
statusOfEvent ?? DBNull.Value);
However, InsertEvent
would need to accept it as a valid input for this parameter. If you're using some .NET support like an XSD dataset to describe the stored procedure. You can modify this to change the parameter to accept char?
from char
. Then you should be able to pass null
and the underlying system will convert this to DBNull.Value
for you. In that scenario, you can leave the call as:
但是,InsertEvent需要接受它作为此参数的有效输入。如果您正在使用某些.NET支持(如XSD数据集)来描述存储过程。你可以修改它来改变参数以接受char吗?来自char。然后你应该能够传递null,底层系统会为你转换为DBNull.Value。在这种情况下,您可以将呼叫保留为:
myDataContext.InsertEvent(
codEvent,
dateOfEvent,
statusOfEvent);
This should also work with Linq2Sql. To change the stored procedure's representation in your Linq2Sql objects, you need to open your dbml and edit the properties for the stored procedure.
这也适用于Linq2Sql。要更改Linq2Sql对象中存储过程的表示形式,需要打开dbml并编辑存储过程的属性。
#2
DBNull.Value is what that database expects...
DBNull.Value是数据库期望的......
Here is how I am passing IsActive (which is a nullable boolean) as a parameter...
这是我如何传递IsActive(这是一个可以为空的布尔值)作为参数...
_ExecutedSP = new StoredProcedure("EvaluationType_GetList", base._SPInputOutput);
if (this.IsActive == null)
{
_ExecutedSP.AddParameter("@IsActive", SqlDbType.Bit, DBNull.Value);
}
else
{
_ExecutedSP.AddParameter("@IsActive", SqlDbType.Bit, this.IsActive);
}