DataReader 最佳实践

发布于 2024-08-13 09:35:43 字数 353 浏览 4 评论 0原文

类似于这个问题,但答案从来没有真正抽出时间来了解我想知道的事情。从 DataReader 获取值是否有任何标准?即,这

dataReader.GetString(dataReader.GetOrdinal("ColumnName"));

被认为更好/更差/与此相同吗?

(string) dataReader["ColumnName"];

Similar to this question, but the answers never really got around to what I want to know. Is there any standards around getting values from a DataReader? I.e., is this

dataReader.GetString(dataReader.GetOrdinal("ColumnName"));

considered better/worse/the same as this?

(string) dataReader["ColumnName"];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿嘿嘿 2024-08-20 09:35:43

这是我的做法:

Int32 ordinal = dataReader.GetOrdinal("ColumnName");

if (!dataReader.IsDBNull(ordinal))
    yourString = dataReader.GetString(ordinal);

像上面所示的那样检查 DBNull 非常重要,因为如果 DataReader 中的字段为空,那么它会在以下情况下抛出异常:你尝试找回它。

Here is the way that I do it:

Int32 ordinal = dataReader.GetOrdinal("ColumnName");

if (!dataReader.IsDBNull(ordinal))
    yourString = dataReader.GetString(ordinal);

It is important to check for DBNull like I have shown above because if the field is null in the DataReader it will throw an exception when you try to retrieve it.

雅心素梦 2024-08-20 09:35:43

我创建了一些扩展方法,让我将 IDataReader 视为可枚举,并通过返回可空整数等来处理 DbNull。这让我可以检查 null 并应用使用 C# ?? 运算符的默认值。

/// <summary>
/// Returns an IEnumerable view of the data reader.
/// WARNING: Does not support readers with multiple result sets.
/// The reader will be closed after the first result set is read.
/// </summary>
public static IEnumerable<IDataRecord> AsEnumerable(this IDataReader reader)
{
    if (reader == null)
        throw new ArgumentNullException("reader");

    using (reader)
    {
        while (reader.Read())
        {
            yield return reader;
        }
    }
}

public static int? GetNullableInt32(this IDataRecord dr, string fieldName)
{
    return GetNullableInt32(dr, dr.GetOrdinal(fieldName));
}

public static int? GetNullableInt32(this IDataRecord dr, int ordinal)
{
    return dr.IsDBNull(ordinal) ? null : (int?)dr.GetInt32(ordinal);
}

...IDataReader 上的其他 GetDataType() 方法依此类推。

I made some extension methods to let me treat an IDataReader as an enumerable, and to deal with DbNull by returning nullable ints, etc. This lets me check for null and apply a default value with the C# ?? operator.

/// <summary>
/// Returns an IEnumerable view of the data reader.
/// WARNING: Does not support readers with multiple result sets.
/// The reader will be closed after the first result set is read.
/// </summary>
public static IEnumerable<IDataRecord> AsEnumerable(this IDataReader reader)
{
    if (reader == null)
        throw new ArgumentNullException("reader");

    using (reader)
    {
        while (reader.Read())
        {
            yield return reader;
        }
    }
}

public static int? GetNullableInt32(this IDataRecord dr, string fieldName)
{
    return GetNullableInt32(dr, dr.GetOrdinal(fieldName));
}

public static int? GetNullableInt32(this IDataRecord dr, int ordinal)
{
    return dr.IsDBNull(ordinal) ? null : (int?)dr.GetInt32(ordinal);
}

...and so on for the other GetDataType() methods on IDataReader.

下壹個目標 2024-08-20 09:35:43

这是一个扩展类;对我来说效果很好。它处理 int 和 boolean 的空值:

   public static class Utility
        {
            public static string ValidatorDataReaderString(this SqlDataReader pSqlDataReader, string pCampo)
            {
                var wValorReader = pSqlDataReader[pCampo];
                if (wValorReader != null)
                {
                    string wValorRetorno = Convert.ToString(pSqlDataReader[pCampo]);
                    return _ = wValorRetorno == string.Empty ? null : wValorRetorno;
                }
                else
                {
                    return null;
                }
            }
    
            public static int ValidatorDataReaderInt(this SqlDataReader pSqlDataReader, string pCampo)
            {
                var wValorReader = pSqlDataReader[pCampo];
                if (wValorReader != null)
                {
                    bool wValorParse = int.TryParse(Convert.ToString(pSqlDataReader[pCampo]), out int oValorRetorno);
                    return wValorParse ? oValorRetorno : 0;
                }
                else
                {
                    return 0;
                }
            }
    
            public static bool ValidatorDataReaderBool(this SqlDataReader pSqlDataReader, string pCampo)
            {
                if (pSqlDataReader[pCampo] != null)
                {
                    var wValor = Convert.ToString(pSqlDataReader[pCampo]);
    
                    if (wValor == "1" || wValor == "True")
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
        }
    }

This an extension class; it works pretty ok for me. It handles nulls for int and boolean:

   public static class Utility
        {
            public static string ValidatorDataReaderString(this SqlDataReader pSqlDataReader, string pCampo)
            {
                var wValorReader = pSqlDataReader[pCampo];
                if (wValorReader != null)
                {
                    string wValorRetorno = Convert.ToString(pSqlDataReader[pCampo]);
                    return _ = wValorRetorno == string.Empty ? null : wValorRetorno;
                }
                else
                {
                    return null;
                }
            }
    
            public static int ValidatorDataReaderInt(this SqlDataReader pSqlDataReader, string pCampo)
            {
                var wValorReader = pSqlDataReader[pCampo];
                if (wValorReader != null)
                {
                    bool wValorParse = int.TryParse(Convert.ToString(pSqlDataReader[pCampo]), out int oValorRetorno);
                    return wValorParse ? oValorRetorno : 0;
                }
                else
                {
                    return 0;
                }
            }
    
            public static bool ValidatorDataReaderBool(this SqlDataReader pSqlDataReader, string pCampo)
            {
                if (pSqlDataReader[pCampo] != null)
                {
                    var wValor = Convert.ToString(pSqlDataReader[pCampo]);
    
                    if (wValor == "1" || wValor == "True")
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文