简体   繁体   中英

Passing uniqueidentifier parameter to Stored Procedure

I am trying to pass a uniqueidentifier parameter to a stored procedure using the following code:

myCommand.Parameters.Add("@BlogID", SqlDbType.UniqueIdentifier).Value = "96d5b379-7e1d-4dac-a6ba-1e50db561b04";

I keep getting an error however saying that the program was unable to convert from string to GUID. Am I passing the value incorrectly?

尝试这个

myCommand.Parameters.Add("@BlogID", SqlDbType.UniqueIdentifier).Value = new Guid("96d5b379-7e1d-4dac-a6ba-1e50db561b04");

A unique identifier is a GUID . so it's a different object type to your string.

You need

myCommand.Parameters.Add("@BlogID", SqlDbType.UniqueIdentifier).Value = 
                                        new Guid("96d5b379-7e1d-4dac-a6ba-1e50db561b04");

One thing to check is that you are not comparing the uniqueidentifier to any string in the database.

When I ran into this, I found a section where I had the following line:

IF @MyGuid IS NULL OR @MyGuid = '' BEGIN...

The { @MyGuid = '' } part will cause the error you described.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM