如何从SqlDataReader uniqueidentifier获取Guid

时间:2013-07-19 15:12:26

标签: c# sql-server sql-server-2008 data-migration

SQL Server 2008(版本10.50.2550)。

我有一个select查询来返回uniqueidentifier类型的单个列。

我想把它变成Guid类型的C#变量。

以下所有方法都会导致异常。

reader的类型为SqlDataReader。

using (var reader = command.ExecuteReader())
{

    if (reader.Read())
    {       
        Guid guid = reader.GetGuid(reader.GetOrdinal("integ_schemehistoryId")); //1

        Guid guid = Guid.Parse((string)reader["integ_schemehistoryId"]); //2

        Guid guid = (Guid)reader["integ_schemehistoryId"]; //3

        Guid guid = new Guid((string)reader["integ_schemehistoryId"]); //4

        Guid guid = Guid.Parse(reader["integ_schemehistoryId"].ToString()); //5
    }

}

1的错误:

System.Data.SqlClient.SqlException (0x80131904): Conversion failed when converting from a character string to uniqueidentifier.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlDataReader.HasMoreRows()
   at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)
   at System.Data.SqlClient.SqlDataReader.Read()
   at Simego.DataSync.DynamicColumns.DataSourceRowOverride.get_EpochSchemeHistoryID()

2的错误:

System.InvalidCastException: Unable to cast object of type 'System.Guid' to type 'System.String'.
   at Simego.DataSync.DynamicColumns.DataSourceRowOverride.get_EpochSchemeHistoryID()

3的错误:

System.Data.SqlClient.SqlException (0x80131904): Conversion failed when converting from a character string to uniqueidentifier.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlDataReader.HasMoreRows()
   at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)
   at System.Data.SqlClient.SqlDataReader.Read()
   at Simego.DataSync.DynamicColumns.DataSourceRowOverride.get_EpochSchemeHistoryID()

4的错误:

System.InvalidCastException: Unable to cast object of type 'System.Guid' to type 'System.String'.
   at Simego.DataSync.DynamicColumns.DataSourceRowOverride.get_EpochSchemeHistoryID()

5的错误:

System.Data.SqlClient.SqlException (0x80131904): Conversion failed when converting from a character string to uniqueidentifier.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlDataReader.HasMoreRows()
   at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)
   at System.Data.SqlClient.SqlDataReader.Read()
   at Simego.DataSync.DynamicColumns.DataSourceRowOverride.get_EpochSchemeHistoryID()

7 个答案:

答案 0 :(得分:7)

您的数据或SQL存在问题。第一种和第三种方法应该有效 - 我个人会使用第一种形式,因为它是我认为最清晰的形式。

但请看堆栈跟踪:

...    在System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)    在System.Data.SqlClient.SqlDataReader.Read()    在Simego.DataSync.DynamicColumns.DataSourceRowOverride.get_EpochSchemeHistoryID()

请注意,Read()调用失败, GetGuid或索引器。

我的猜测是你的属性被多次提取,有时它有效 - 这就是你在第二种方法中获得强制转换异常的原因 - 但对于一些行,由于数据或SQL的某些问题而失败。由于我们不知道您的数据来自哪里,我们无法帮助您超越诊断范围,但这是您应该在下一步看到的地方。

答案 1 :(得分:2)

从SQL生成该错误。换句话说,SQL命令文本有问题。不是你的c#代码。

答案 2 :(得分:1)

你试过了吗?

Guid guid = new Guid((string)reader["integ_schemehistoryId"]);

答案 3 :(得分:1)

尝试使用Guid.Parse method

Guid guid = return Guid.Parse((string)reader["integ_schemehistoryId"]);

答案 4 :(得分:0)

正如其他答案所示,你应该尝试:

Guid guid = Guid.Parse(reader["integ_schemehistoryId"].ToString())

您可能还想查看存储过程和表格,可能存在问题。

答案 5 :(得分:0)

    object val = reader[0];
    Guid g;
    if ( Guid.TryParse(val, out g) )
    {
        return g;
    } 
    else 
    {
        Trace.WriteLine("Error: " + g == null ? string.Empty : g.ToString());   
    }

答案 6 :(得分:0)

在 Oracle 数据库中:

"ID" RAW(16) DEFAULT SYS_GUID() NOT NULL ENABLE, 

此代码对我有用:

using (IDataReader dr = ... )
    {
        while (dr.Read())
        {
            var Id = new Guid();
            if (!(dr["ID"] is DBNull)){
                Id = new Guid((byte[])dr.GetValue(dr.GetOrdinal("ID")));
            }                   
        }
        dr.Close();
    }