C# SQL null 相互转换,有什么问题吗?
这似乎对我有用,但想知道它是否有任何问题
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为一件小事,
is
可能会更有效:作为第二的想法,我不喜欢 if
T=int
如果为 null,您将得到0
;我想要一个错误。我很想使用:它应该正确地适用于引用类型和
Nullable
,并为int
等引发错误。在
ConvertToDb 中,
return (T)value;
是多余的,因为您实际上返回的是object
;你也可以:(在这两种情况下)
所以:
As a minor thing,
is
would probably be more efficient:as a second thought, I don't like the fact that if
T=int
you will get0
for a null; I'd want an error. I'd be tempted to use:which should work for reference types and
Nullable<T>
correctly, and raise an error forint
etc.In
ConvertToDb
, thereturn (T)value;
is redundant, since you are actually returningobject
; you may as well just:(in both cases)
So: