Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C#向表中的GUID列插入空值?_C#_Sql_Sql Server_Stored Procedures - Fatal编程技术网

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

如何使用C#向表中的GUID列插入空值?,c#,sql,sql-server,stored-procedures,C#,Sql,Sql Server,Stored Procedures,我搜索了整个网络。我想知道为什么这个简单的任务在C#中如此难以完成 这将返回一个错误: 无法将参数值从字符串转换为guid 我的存储过程使用C#传递的参数更新表中的记录 我想做的是使用一个存储过程来更新一个记录及其特定列的空值,该存储过程从C#传递一个guid参数,该参数的值有时可能为空。这在C#中可能吗?您可以试试: sqlcmd.Parameters.Add("@someid", System.Data.SqlDbType.UniqueIdentifier).Value = string.I

我搜索了整个网络。我想知道为什么这个简单的任务在C#中如此难以完成

这将返回一个错误:

无法将参数值从字符串转换为guid

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

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

您可以试试:

sqlcmd.Parameters.Add("@someid", System.Data.SqlDbType.UniqueIdentifier).Value =
string.IsNullOrEmpty(class.someid) ? System.Guid.Empty : new Guid(class.someid);
如果铸件不起作用。在您的表中,确保此列是否允许null。 和
class.someid
具有正确的构造函数格式。
您可以对此进行检查以获得概述。

如果我没有错,您的列
ID
在数据库中是
Guid
类型,但您的参数是string

您可以将参数转换为
SqlGuid

command.Parameters.Add("@GuidParameter", SqlDbType.UniqueIdentifier).Value = new System.Data.SqlTypes.SqlGuid(YourGuid); //The value of @ID
链接:

还可以在存储过程中尝试以下操作:

Declare @someid uniqueidentifier = null
声明可为空的属性:

public Guid? column1;
分配空值:

column1 = null;

尝试使用
DBNull.Value
而不是
System.Data.SqlTypes.SqlGuid.Null
@Wulworine它说System.DBNull和System.Guid之间没有隐式转换,我尝试了(object)DBNull.Value,它成功构建,但给了我相同的错误。DBNull.Value将适用于字符串和GUID。您能在问题中发布完整的代码块吗?请参阅我的新更新主题postit给了我相同的错误。无法将参数值从Guid转换为字符串。谢谢你确定要为
Guid
结构的构造函数提供正确的格式吗?我做得很辛苦。这篇文章给了我这个想法。if(string.IsNullOrWhiteSpace(page.UserId)){userparam=new-SqlParameter(“@UserId”,System.Data.SqlTypes.SqlGuid.Null);sqlcmd.Parameters.Add(userparam);}else{sqlcmd.Parameters.Add(@UserId),System.Data.SqlDbType.UniqueIdentifier).Value=newguid(page.UserId);}
column1 = null;