我正在编写一个部署在 SQL Server 2008 R2(所以 .Net 3.5)上的 C# 存储过程,并希望将一个可选参数声明为可为 null 的 guid。这是我首先尝试的:
[Microsoft.SqlServer.Server.SqlProcedure]
public static void spCalcPerc(
SqlGuid pID
, SqlGuid sID = DBNull.Value
)
由于编译时错误而失败:
Default parameter value for 'sID' must be a compile-time constant
这是因为 DBNull.Value
不是常量,这很痛苦。
所以我尝试将声明更改为:
[Microsoft.SqlServer.Server.SqlProcedure]
public static void spCalcPerc(
SqlGuid pID
, Guid? sID = null
)
这编译但现在我得到这个部署错误:
Deploy error : Could not create a type for parameter System.Nullable
尝试:
, SqlGuid sID = null
给出这个编译时错误:
A value of type '< null >' cannot be used as a default parameter because there are no standard conversions to type 'System.Data.SqlTypes.SqlGuid'
所以感觉相当卡住我诉诸于此:
, SqlGuid sID = "00000000-0000-0000-0000-000000000000"
我不想这样做,因为在代码中测试该字符串感觉很麻烦。但是,当我收到此编译错误时,这也不起作用:
A value of type 'string' cannot be used as a default parameter because there are no standard conversions to type 'System.Guid'
啊,有 4 种不同的方法,但没有一种有效。 感叹
请感谢您的想法或插入正确的方向。
最佳答案
尝试
, SqlGuid sID = New Guid()
你可以使用
if (sID == Guid.Empty)
检查是否没有赋值。
关于c# - 如何使用 SQL Server 将可为空的 guid 声明为 C# CLR 存储过程的可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12585675/