I have a GUID (varchar(50,notnull) in sql) That I am returning from SQL via ExecuteScalar() in VB I am storing that value in a string (max of 2gb) in VB I then run a select where GUID = Stored GUID

When i run the program it looks like it trucates the GUID Sample {3F2504E0-4F89-11D3-9A0C-0305E82C3301}

Sampele Error The floating point value '3F2504E0' is out of the range of the computer representation (8bytes)

Is my executeScalar truncating this info?

Code:

 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 
有帮助吗?

解决方案

There are a few things not quite right:

  1. Use UniqueIdentifier for GUID. There is a reason why SQL got this data type. http://en.wikipedia.org/wiki/Globally_unique_identifier

  2. If Hosted_GUID is declared as GUID, then you obviously can't implicitly convert like that!

    Hosted_GUID = comm.ExecuteScalar()

=> If you define your GUID-Column as UniqueIdentifier, your problems will suddenly disappear. Though make sure to check if comm.ExecuteScalar is Nothing.

MSDN about ExecuteScalar:

The first column of the first row in the result set, or a null reference.

EDIT: If you can not alter your current Column to UniqueIdentifier, last option would be to convert the string in code to GUID:

Hosted_GUID = new Guid(comm.ExecuteScalar())

Sidenote: Also make sure to use parameterized queries.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top