如何将 SqlDbType.UniqueIdentifier 分配给字符串?
//Retrieve data to display
string queryString2 = "SELECT [ConfidenceLevel], [LoveLevel], [StressLevel] FROM [UserData] WHERE ([UserProfileID] = @UserProfileID) ORDER BY [DateTime] ASC";
SqlCommand CommandSelect = new SqlCommand(queryString2, database.Connection);
//add parameters. used to prevent sql injection
CommandSelect.Parameters.Add("@UserProfileID", SqlDbType.UniqueIdentifier);
CommandSelect.Parameters["@UserProfileID"].Value = Membership.GetUser().ProviderUserKey;
因此,最后,我不想使用“Membership.GetUser().ProviderUserKey”,而是想使用动态成员 ID,该 ID 只能作为 GET 名称值对中的字符串使用。
即 - string id = "a051fc1b-4f51-485b-a07d-0f378528974e "
如何将此字符串分配给分配值的最后一个 SQL 参数行?类型转换吗?
如果我只是将字符串分配给它,我会收到错误:
从“System.String”到的转换无效 '系统.Guid'。
谢谢。
//Retrieve data to display
string queryString2 = "SELECT [ConfidenceLevel], [LoveLevel], [StressLevel] FROM [UserData] WHERE ([UserProfileID] = @UserProfileID) ORDER BY [DateTime] ASC";
SqlCommand CommandSelect = new SqlCommand(queryString2, database.Connection);
//add parameters. used to prevent sql injection
CommandSelect.Parameters.Add("@UserProfileID", SqlDbType.UniqueIdentifier);
CommandSelect.Parameters["@UserProfileID"].Value = Membership.GetUser().ProviderUserKey;
So at the very end, instead of using the "Membership.GetUser().ProviderUserKey" I want to use a dynamic member ID, that I will only have available as a string from the GET name value pair.
i.e. - string id = "a051fc1b-4f51-485b-a07d-0f378528974e "
How do I get this string to be assigned to the last SQL parameter line assigning the value? Type cast it?
If I just assign it the string I recieve the error:
Invalid cast from 'System.String' to
'System.Guid'.
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅 System.Guid 的文档。在那里你会发现一个构造函数,它接受一个字符串值,并创建一个 Guid 结构。
Refer to the documentation for System.Guid. There you will find a constructor which takes a string value, and creates a Guid struct.