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

时间:2022-09-30 02:05:57

I searched the entire web. I wonder why this easy task is so hard to accomplish in C#.

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

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

This returns an error :

这会返回一个错误:

Failed to convert parameter value from string to guid

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

My stored procedure updates a record in the table with the parameters relayed by C#.

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

What I want to do is to update a record and its specific column with null value by using a stored procedure which passes a guid parameter from C# with guid that can sometimes be null in value. Is this possible in C#?

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

2 个解决方案

#1


1  

Could you try:

你能尝试一下:

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

If the casting doesn't work. And in your table make sure if this column allows null. And class.someid has the correct format for the constructor. You can check on this link to have an overview.

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

#2


1  

If I am not wrong, your column ID is of Guid type in your database, but your parameter is string.

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

You can convert your parameter to SqlGuid

您可以将参数转换为SqlGuid

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

Link : http://msdn.microsoft.com/fr-fr/library/system.data.sqltypes.sqlguid.aspx

链接:http://msdn.microsoft.com/fr-fr/library/system.data.sqltypes.sqlguid.aspx

Also try the following in the stored procedure:

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

Declare @someid uniqueidentifier = null

Declaring nullable property:

声明可以为空的属性:

public Guid? column1;

Assigning Null:

分配空:

column1 = null;

#1


1  

Could you try:

你能尝试一下:

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

If the casting doesn't work. And in your table make sure if this column allows null. And class.someid has the correct format for the constructor. You can check on this link to have an overview.

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

#2


1  

If I am not wrong, your column ID is of Guid type in your database, but your parameter is string.

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

You can convert your parameter to SqlGuid

您可以将参数转换为SqlGuid

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

Link : http://msdn.microsoft.com/fr-fr/library/system.data.sqltypes.sqlguid.aspx

链接:http://msdn.microsoft.com/fr-fr/library/system.data.sqltypes.sqlguid.aspx

Also try the following in the stored procedure:

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

Declare @someid uniqueidentifier = null

Declaring nullable property:

声明可以为空的属性:

public Guid? column1;

Assigning Null:

分配空:

column1 = null;