我有一个GUID (在SQL中是varchar(50,notnull) ),它是我在VB中通过ExecuteScalar()从SQL返回的。在VB中,我将该值存储在一个字符串(最大2 VB)中,然后运行select where GUID =存储的GUID
当我运行程序时,它看起来像是传递了GUID示例{3F2504E0-4F89-11D3-9A0C-0305E82C3301}
Sampele错误浮点值'3F2504E0‘超出计算机表示形式的范围(8字节)
我的executeScalar是否正在截断此信息?
代码:
Dim sqlquery As String
Dim ConnectionString As String
If cmboxDatabaseName.Text <> "" Then
ConnectionString = "Server=" + ServerName + "\" + InstanceName + "; Database=" + Control + "; User Id=" + UserId + ";Password=" + Password + ";"
sqlquery = "Select top 1 GUID from dbo.Databases with(Nolock) where dbName = '" + cmboxDatabaseName.Text + "'"
'Connect
Using conn As SqlConnection = New SqlConnection(ConnectionString)
conn.Open()
Using comm As SqlCommand = New SqlCommand(sqlquery, conn)
Hosted_GUID = comm.ExecuteScalar()
conn.Close()
End Using 'comm
conn.Close()
End Using 'conn 发布于 2013-07-09 05:02:05
有几件事不太对劲:
对于GUID,请使用UniqueIdentifier
Hosted_GUID被声明为GUID,那么你显然不能像那样隐式转换!Hosted_GUID = comm.ExecuteScalar()
如果您将GUID- =>定义为UniqueIdentifier,您的问题将突然消失。但是一定要检查comm.ExecuteScalar是否为Nothing。
关于ExecuteScalar的MSDN
结果集中第一行的第一列,或空引用。
EDIT:如果您不能将当前列更改为UniqueIdentifier,最后一个选项是将代码中的字符串转换为GUID:
Hosted_GUID = new Guid(comm.ExecuteScalar())Sidenote:还确保使用参数化查询。
https://stackoverflow.com/questions/17535847
复制相似问题