System.InvalidCastException Int32到GUID [英] System.InvalidCastException Int32 to guid
问题描述
我的SQL数据库(RatingDbSQL)有3个表,人"表包括:
列(0)作为唯一ID;
Column(1)名字为字符串; Column(2)姓氏为字符串.
数据库提供程序是:SQL Server的.NET Framework数据提供程序
使用以下命令从数据库中加载数据:
My SQL database (RatingDbSQL) has 3 tables and the ''Persons'' table comprises:
Column(0) as Unique ID;
Column(1) FirstName as String; Column(2) LastName as String.
The database Provider is: .NET Framework Data Provider for SQL Server
The data loads from the Database OK using:
ConRatingDB.Open()
Me.PersonsTableAdapter.Fill(Me.DsRatingDbSQL.Persons)
daPersons.Fill(DsRatingDbSQL, "Persons")
ConRatingDB.Close()
在我使用以下方法将数据保存到人员表之前,其他两个表是无法使用的:
----------
第一行(更新)返回:
System.InvalidCastException –无法将参数值从Int32转换为Guid
The other two tables are non starters until I resolve saving data to the Persons Table using the following:
- - - - - - - - - - -
The first line (Update) returns with:
System.InvalidCastException – Failed to convert parameter value from a Int32 to a Guid
daPersons.Update(DsRatingDbSQL.Tables("Persons"))
DsRatingDbSQL.Tables("Persons").Clear()
daPersons.Fill(DsRatingDbSQL, "Persons")
ID列的属性为:
数据类型:UniqueIdentifier
长度:16
可为空:False
----------
我已经阅读了许多有关类似问题的Internet文章,但不了解如何实现它们:
以下三个看起来很有趣-但是我对实现方法一无所知:
Guid guid = new Guid(sqlCmd.Parameters ["@ UserID"].Value.ToString())
或者:
昏暗的ID作为字符串= commandArgs(0).ToString()
Dim FirstName as String = commandArgs(1).ToString()
Dim LastName as String = commandArgs(2).ToString()
或:将i设为Integer
可重写的公共重写函数GetGuid(ByVal i Integer)如Guid实现IDataRecord.GetGuid
任何人都可以建议
The Properties of the ID column are:
DataType: UniqueIdentifier
Length: 16
Nullable: False
- - - - - - - - - - -
I have read many Internet articles on similar problems but don’t understand how to implement them:
The following three look interesting – but I’m lost on how to implement:
Guid guid = new Guid(sqlCmd.Parameters["@UserID"].Value.ToString())
Or:
Dim ID as String = commandArgs(0).ToString()
Dim FirstName as String = commandArgs(1).ToString()
Dim LastName as String = commandArgs(2).ToString()
Or:Dim i as Integer
Public Overridable Function GetGuid(ByVal i As Integer) As Guid Implements IDataRecord.GetGuid
Could anyone advise please
推荐答案
您不能使用强制类型转换将值类型从一种转换为另一种.当您的值是整数,字符串和(我猜是)布尔值时,为什么要尝试获取GUID?
You cannot use casts to convert from one value type to another. Why are you trying to get a GUID when your values are integer, string and (I guess) boolean?
您无法真正将GUID类型转换为整数.
如果尝试将GUID转换为int,则至少会丢失部分GUID信息.
GUID本身就是标识符,因此您应该可以直接使用它们.
You cannot really convert a GUID type to an integer.
If you try and convert, GUID to an int, you will lose a part of the GUID information at least.
GUID''s are identifiers in themselves so you should be able to use them directly.
已经看过MSDN ^ ],我看到UniqueIdentifier
元素是GUID值.我只能假设您已经用整数替换了一个,或者表中有一个整数项,这导致Update()
引发此错误.
Having looked at the MSDN documentation[^], I see thatUniqueIdentifier
elements are GUID values. I can only assume that you have replaced one with an integer, or have an integer item in your table, which causesUpdate()
to throw this error.
这篇关于System.InvalidCastException Int32到GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!