如何使用C#将空值插入表中的GUID列?

我搜索了整个网络.我想知道为什么这个简单的任务在C#中很难实现.

sqlcmd.Parameters.Add("@someid", System.Data.SqlDbType.UniqueIdentifier).Value =
string.IsNullOrEmpty(class.someid) ? System.Data.SqlTypes.SqlGuid.Null : new Guid(class.someid);

这会返回一个错误:

Failed to convert parameter value from string to guid

我的存储过程使用C#中继的参数更新表中的记录.

我想要做的是通过使用存储过程来更新记录及其具有空值的特定列,该存储过程使用guid传递guid参数,guid有时可能为null.这可能在C#中吗?

最佳答案 你能尝试一下:

sqlcmd.Parameters.Add("@someid", System.Data.SqlDbType.UniqueIdentifier).Value =
string.IsNullOrEmpty(class.someid) ? System.Guid.Empty : new Guid(class.someid);

如果铸造不起作用.并在您的表中确保此列是否允许null.
并且class.someid具有构造函数的正确格式.
您可以查看此link以获得概述.

点赞