简体   繁体   English

将Session字符串传递给sql作为uniqueidentifier

[英]pass Session string to sql as uniqueidentifier

I have a session variable which holds a guid string: 我有一个会话变量,其中包含一个guid字符串:

Session["Company_Id"] = "4B5E95A7-745A-462F-AE53-709A8583700A"; 

I want to pass this as a parameter to SQL Server: 我想将此作为参数传递给SQL Server:

So add the parameter: 因此,添加参数:

cmd.Parameters.Add("@Company_Id", System.Data.SqlDbType.UniqueIdentifier).Value = Session["Company_Id"];

When the Stored Procedure gets called I get: 当调用存储过程时,我得到:

System.InvalidCastException: 'Failed to convert parameter value from a String to a Guid.' System.InvalidCastException:'无法将参数值从字符串转换为Guid。

Appreciate any guidance ;-) 感谢任何指导;-)

您可以执行以下操作:

cmd.Parameters.Add("@Company_Id", System.Data.SqlDbType.UniqueIdentifier).Value = Guid.Parse(Session["Company_Id"]);

You can pass it as Guid by using Guid.Parse method which would be convert your string representation of your Guid to Guid : 您可以使用Guid.Parse方法将其作为Guid传递,该方法会将Guid string表示形式转换为Guid

Guid.Parse(Session["Company_Id"].ToString());

Your command code would become: 您的命令代码将变为:

cmd.Parameters.Add("@Company_Id", System.Data.SqlDbType.UniqueIdentifier).Value = Guid.Parse(Session["Company_Id"].ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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