How to insert a null value into a GUID column in a table using C #?

I searched the entire network. I wonder why this simple task is so hard to do in 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

My stored procedure updates an entry in a table with parameters passed using C #.

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

+4
source share
2 answers

Could you try:

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

. , null. class.someid . , .

+1

, ID Guid , .

SqlGuid

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

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

:

Declare @someid uniqueidentifier = null

nullable:

public Guid? column1;

Null:

column1 = null;
+1

Source: https://habr.com/ru/post/1539850/


All Articles