在做的時候
If Me.Id_Padre Is Nothing Then
comandoSQL.Parameters.Add("@Id_Padre", SqlDbType.UniqueIdentifier).Value = DBNull.Value
Else
comandoSQL.Parameters.Add("@Id_Padre", SqlDbType.UniqueIdentifier).Value = New Guid(Me.Id_Padre)
End If
沒有給我錯誤,而是這個
comandoSQL.Parameters.Add("@Id_Padre", SqlDbType.UniqueIdentifier).Value = IIf(Me.Id_Padre Is Nothing, DbNull.Value, New Guid(Me.Id_Padre))
給我一個錯誤,說該值不能為空。
任何想法?
先感謝您
uj5u.com熱心網友回復:
正在發生的事情是 Iif 將始終評估(運行)這兩個部分。所以最后一部分會拋出例外,因為 Id_Padre 什么都不是。從檔案(https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/iif-function):
IIf 總是同時計算 truepart 和 falsepart,即使它只回傳其中一個。因此,您應該注意不良副作用。例如,如果計算 falsepart 導致除以零錯誤,即使 expr 為 True,也會發生錯誤。
我建議使用 If 來避免這種情況(https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/if-operator)所以:
comandoSQL.Parameters.Add("@Id_Padre", SqlDbType.UniqueIdentifier).Value = If(Me.Id_Padre Is Nothing, DbNull.Value, New Guid(Me.Id_Padre))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/492041.html
標籤:VB.net
上一篇:(VB.NET)如何使標簽閃爍,但為了便于閱讀,打開時間比關閉時間長?
下一篇:使用集成訪問資料庫的專案部署問題
