C# SQL null 相互转换,有什么问题吗?

发布于 2024-10-03 04:24:53 字数 3118 浏览 2 评论 0原文

这似乎对我有用,但想知道它是否有任何问题

 class Test
    {
        public int PKId { get; set; }
        public string RequiredString { get; set; }
        public int RequiredInt { get; set; }
        public decimal RequiredMoney { get; set; }
        public bool RequiredBool { get; set; }
        public DateTime RequiredDate { get; set; }
        public string NullableString { get; set; }
        public int? NullableInt { get; set; }
        public decimal? NullableMoney { get; set; }
        public bool? NullableBool { get; set; }
        public DateTime? NullableDate { get; set; }
    }

testDatabase 就空值而言与类匹配。

  static internal T NullConvertFromDB<T>(object value) 
        {
            if (value.Equals(DBNull.Value))
            {
                return default(T);
            }
            else
            {
                return (T)value;
            }
        }

        static internal object NullConvertToDB<T>(object value)
        {
            if (value == null)
                return System.DBNull.Value;
            else
                return (T)value;
        }

在检索数据时我使用

 Test test = new Test();
                        test.PKId = dr.GetInt32(0);
                        test.RequiredString = dr.GetString(1);
                        test.RequiredInt = dr.GetInt32(2);
                        test.RequiredMoney = (decimal)dr.GetSqlMoney(3);
                        test.RequiredBool = dr.GetBoolean(4);
                        test.RequiredDate = dr.GetDateTime(5);
                        test.NullableString = NullConvertFromDB<string>(dr[6]);
                        test.NullableInt = NullConvertFromDB<int?>(dr[7]);
                        test.NullableMoney = NullConvertFromDB<decimal?>(dr[8]);
                        test.NullableBool = NullConvertFromDB<bool?>(dr[9]);
                        test.NullableDate = NullConvertFromDB<DateTime?>(dr[10]);

并插入我使用

 cmd.Parameters.AddWithValue("@RequiredString", test.RequiredString);
                    cmd.Parameters.AddWithValue("@RequiredInt", test.RequiredInt);
                    cmd.Parameters.AddWithValue("@RequiredMoney", test.RequiredMoney);
                    cmd.Parameters.AddWithValue("@RequiredBool", test.RequiredBool);
                    cmd.Parameters.AddWithValue("@RequiredDate", test.RequiredDate);
                    cmd.Parameters.AddWithValue("@NullableString", NullConvertToDB<string>(test.NullableString));
                    cmd.Parameters.AddWithValue("@NullableInt", NullConvertToDB<int?>(test.NullableInt));
                    cmd.Parameters.AddWithValue("@NullableMoney", NullConvertToDB<decimal?>(test.NullableMoney));
                    cmd.Parameters.AddWithValue("@NullableBool", NullConvertToDB<bool?>(test.NullableBool));
                    cmd.Parameters.AddWithValue("@NullableDate", NullConvertToDB<DateTime?>(test.NullableDate));

这效果很好,但想知道在我的 DAL 中实现之前是否需要注意什么。

问候

_埃里克

This seems to work for me, but was wondering if there are any issues with it

 class Test
    {
        public int PKId { get; set; }
        public string RequiredString { get; set; }
        public int RequiredInt { get; set; }
        public decimal RequiredMoney { get; set; }
        public bool RequiredBool { get; set; }
        public DateTime RequiredDate { get; set; }
        public string NullableString { get; set; }
        public int? NullableInt { get; set; }
        public decimal? NullableMoney { get; set; }
        public bool? NullableBool { get; set; }
        public DateTime? NullableDate { get; set; }
    }

The testDatabase matches the class as far as nulls are concerned.

  static internal T NullConvertFromDB<T>(object value) 
        {
            if (value.Equals(DBNull.Value))
            {
                return default(T);
            }
            else
            {
                return (T)value;
            }
        }

        static internal object NullConvertToDB<T>(object value)
        {
            if (value == null)
                return System.DBNull.Value;
            else
                return (T)value;
        }

While retrieving data I use

 Test test = new Test();
                        test.PKId = dr.GetInt32(0);
                        test.RequiredString = dr.GetString(1);
                        test.RequiredInt = dr.GetInt32(2);
                        test.RequiredMoney = (decimal)dr.GetSqlMoney(3);
                        test.RequiredBool = dr.GetBoolean(4);
                        test.RequiredDate = dr.GetDateTime(5);
                        test.NullableString = NullConvertFromDB<string>(dr[6]);
                        test.NullableInt = NullConvertFromDB<int?>(dr[7]);
                        test.NullableMoney = NullConvertFromDB<decimal?>(dr[8]);
                        test.NullableBool = NullConvertFromDB<bool?>(dr[9]);
                        test.NullableDate = NullConvertFromDB<DateTime?>(dr[10]);

And inserting I use

 cmd.Parameters.AddWithValue("@RequiredString", test.RequiredString);
                    cmd.Parameters.AddWithValue("@RequiredInt", test.RequiredInt);
                    cmd.Parameters.AddWithValue("@RequiredMoney", test.RequiredMoney);
                    cmd.Parameters.AddWithValue("@RequiredBool", test.RequiredBool);
                    cmd.Parameters.AddWithValue("@RequiredDate", test.RequiredDate);
                    cmd.Parameters.AddWithValue("@NullableString", NullConvertToDB<string>(test.NullableString));
                    cmd.Parameters.AddWithValue("@NullableInt", NullConvertToDB<int?>(test.NullableInt));
                    cmd.Parameters.AddWithValue("@NullableMoney", NullConvertToDB<decimal?>(test.NullableMoney));
                    cmd.Parameters.AddWithValue("@NullableBool", NullConvertToDB<bool?>(test.NullableBool));
                    cmd.Parameters.AddWithValue("@NullableDate", NullConvertToDB<DateTime?>(test.NullableDate));

This works great but was wondering if there is anything I need to be aware of before I implement in in my DAL.

Regards

_Eric

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

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

发布评论

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

评论(1

假装不在乎 2024-10-10 04:24:53

作为一件小事,is 可能会更有效:

if (value == null || value is DBNull) {... null code...}

作为第二的想法,我不喜欢 if T=int如果为 null,您将得到 0;我想要一个错误。我很想使用:

return (T)(object)null;

它应该正确地适用于引用类型和 Nullable ,并为 int 等引发错误。


ConvertToDb 中,return (T)value; 是多余的,因为您实际上返回的是 object;你也可以:(

return value ?? DBNull.Value;

在这两种情况下)


所以:

    static internal T NullConvertFromDB<T>(object value) 
    {
        if (value == null || value is DBNull)
        {
            return (T)(object)null;
        }
        return (T)value;
    }

    static internal object NullConvertToDB<T>(object value)
    {
        return value ?? DBNull.Value;
    }

As a minor thing, is would probably be more efficient:

if (value == null || value is DBNull) {... null code...}

as a second thought, I don't like the fact that if T=int you will get 0 for a null; I'd want an error. I'd be tempted to use:

return (T)(object)null;

which should work for reference types and Nullable<T> correctly, and raise an error for int etc.


In ConvertToDb, the return (T)value; is redundant, since you are actually returning object; you may as well just:

return value ?? DBNull.Value;

(in both cases)


So:

    static internal T NullConvertFromDB<T>(object value) 
    {
        if (value == null || value is DBNull)
        {
            return (T)(object)null;
        }
        return (T)value;
    }

    static internal object NullConvertToDB<T>(object value)
    {
        return value ?? DBNull.Value;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文